From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31913 invoked by alias); 19 Aug 2014 09:48:57 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 31874 invoked by uid 48); 19 Aug 2014 09:48:53 -0000 From: "burnus at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [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 Date: Tue, 19 Aug 2014 09:48:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 5.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: burnus at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status keywords bug_severity priority component assigned_to reporter cc Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2014-08/txt/msg01259.txt.bz2 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.