Summary: | warning about comparison to true/false | ||
---|---|---|---|
Product: | gcc | Reporter: | Frank Heckenbach <f.heckenbach> |
Component: | c++ | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | UNCONFIRMED --- | ||
Severity: | enhancement | CC: | egallager, marxin, webrown.cpp |
Priority: | P3 | Keywords: | diagnostic |
Version: | unknown | ||
Target Milestone: | --- | ||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: | ||
Bug Depends on: | |||
Bug Blocks: | 87403 |
Description
Frank Heckenbach
2022-08-29 23:48:19 UTC
Do you have an example? This should cover all cases mentioned: void t (bool a, int i, float e, double f) { // Boolean literal comparisons if (a == true) // better: if (a) return; if (a == false) // better: if (!a) return; if (a != true) // better: if (!a) return; if (a != false) // better: if (a) return; // also reversed to be sure if (true == a) // better: if (a) return; if (false != a) // better: if (a) return; // Integer comparisons to Boolean literals if (i == true) // better: if (i == 1) return; if (i == false) // better: if (i == 0) or: if (!i) return; if (i != true) // better: if (i != 1) return; if (i != false) // better: if (i != 0) or: if (i) return; // Floating-point comparisons to Boolean literals if (e == true) // very strange at all, if meant so, then better: if (e == 1.0f) return; if (f == false) // very strange at all, if meant so, then better: if (f == 0.0) return; } that's more coding style though? (In reply to Richard Biener from comment #3) > that's more coding style though? Yeah I personally prefer the more explicit way of writing it with both operands myself (In reply to Frank Heckenbach from comment #2) > This should cover all cases mentioned: > > void t (bool a, int i, float e, double f) > { > // Boolean literal comparisons > if (a == true) // better: if (a) > return; > if (a == false) // better: if (!a) > return; > if (a != true) // better: if (!a) > return; > if (a != false) // better: if (a) > return; > // also reversed to be sure > if (true == a) // better: if (a) > return; > if (false != a) // better: if (a) > return; These are definitely coding style, and I don't think a warning is warranted. > > // Integer comparisons to Boolean literals > if (i == true) // better: if (i == 1) Or maybe it was meant to be ((bool)i = true). Warning for these does seem more useful IMO. The arithmetic promotions from bool to int (or float) mean the comparison doesn't do what it appears to do, and there seems no reason to use a true/false literal there. > --- Comment #4 from Eric Gallager <egallager at gcc dot gnu.org> ---
> (In reply to Richard Biener from comment #3)
> > that's more coding style though?
>
> Yeah I personally prefer the more explicit way of writing it with both operands
> myself
It's not really about operands, but the whole operation is
redundant: "mybool == true" is exactly equivalent to just "mybool".
So it's similar to a cast to the original type which GCC can also
warn about (-Wuseless-cast).
Some other existing warnings (-Wredundant-decls, -Wredundant-move)
also seem to warn about things that are no potential problems, just
redundant.
Of course, all of this can be considered a matter of style, that's
why warnings can be selected by options. (As I said, I assume it
would not be enabled by -Wall, and I don't mind whether or not it's
enabled by -Wextra.)
|