public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/62183] New: [C/C++] Warning wished for "!int_val == const" / logical not is only applied to the left hand side of this comparison
@ 2014-08-19  9:48 burnus at gcc dot gnu.org
  0 siblings, 0 replies; only message in thread
From: burnus at gcc dot gnu.org @ 2014-08-19  9:48 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-08-19  9:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-19  9:48 [Bug c/62183] New: [C/C++] Warning wished for "!int_val == const" / logical not is only applied to the left hand side of this comparison burnus at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).