https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62183
Bug ID: 62183
Summary: [C/C++] Warning wished for "!int_val == const" /
logical not is only applied to the left hand side of
this comparison
Product: gcc
Version: 5.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: burnus at gcc dot gnu.org
CC: mpolacek at gcc dot gnu.org
That's a bug I tend to make from time to time (and usually spot it
immediately), it popped up also in our C++ code, and Coverity also had found
some instances in the GCC code.
Namely, using "!a == b" only rarely makes sense and usually should be "a != b".
I didn't manage to get GCC to warn in this case while Clang warns by default.
For the code:
int foo(int i) {
if (!i == 5)
return 0;
else
return 1;
}
Clang prints:
foo.cc:2:7: warning: logical not is only applied to the left hand side of this
comparison [-Wlogical-not-parentheses]
if (!i == 5)
^ ~~
foo.cc:2:7: note: add parentheses after the '!' to evaluate the comparison
first
if (!i == 5)
^
( )
foo.cc:2:7: note: add parentheses around left hand side expression to silence
this warning
if (!i == 5)
^
( )
foo.cc:2:10: warning: comparison of constant 5 with expression of type 'bool'
is always false [-Wtautological-constant-out-of-range-compare]
if (!i == 5)
~~ ^ ~
2 warnings generated.