public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dcb314 at hotmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/105164] New: new warning in clang, missing in gcc: -Wbitwise-instead-of-logical
Date: Tue, 05 Apr 2022 16:16:56 +0000	[thread overview]
Message-ID: <bug-105164-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105164

            Bug ID: 105164
           Summary: new warning in clang, missing in gcc:
                    -Wbitwise-instead-of-logical
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dcb314 at hotmail dot com
  Target Milestone: ---

For this C++ code:

extern bool f1( int);
extern void f3( int);

void
f2( int n)
{
        if (!f1(n) & !f1( n + 1))
                f3( n - 1);

        if (!f1(n) | !f1( n + 1))
                f3( n + 1);
}

recent gcc trunk says not a lot:

$ /home/dcb/gcc/results/bin/g++ -g -O2 -Wall -Wextra -pedantic -c jul01a.cc
$

However, new clang-14 says:

$ clang++ -g -O2 -Wall -Wextra -pedantic -c jul01a.cc
jul01a.cc:8:6: warning: use of bitwise '&' with boolean operands
[-Wbitwise-instead-of-logical]
        if (!f1(n) & !f1( n + 1))
            ^~~~~~~~~~~~~~~~~~~~
                   &&
jul01a.cc:8:6: note: cast one or both operands to int to silence this warning
jul01a.cc:11:6: warning: use of bitwise '|' with boolean operands
[-Wbitwise-instead-of-logical]
        if (!f1(n) | !f1( n + 1))
            ^~~~~~~~~~~~~~~~~~~~
                   ||
jul01a.cc:11:6: note: cast one or both operands to int to silence this warning
2 warnings generated.
$ 

Static analyser cppcheck also finds the problem:

$ /home/dcb/cppcheck/trunk.git/cppcheck --enable=all jul01a.cc
jul01a.cc:8:13: style: Boolean result is used in bitwise operation. Clarify
expression with parentheses. [clarifyCondition]
 if (!f1(n) & !f1( n + 1))
            ^
jul01a.cc:11:13: style: Boolean result is used in bitwise operation. Clarify
expression with parentheses. [clarifyCondition]
 if (!f1(n) | !f1( n + 1))
            ^
jul01a.cc:6:0: style: The function 'f2' is never used. [unusedFunction]

^

             reply	other threads:[~2022-04-05 16:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-05 16:16 dcb314 at hotmail dot com [this message]
2022-04-05 21:24 ` [Bug c++/105164] " dcb314 at hotmail dot com
2022-04-06  7:51 ` rguenth at gcc dot gnu.org
2022-04-06  9:08 ` marxin at gcc dot gnu.org
2022-04-06 12:12 ` marxin at gcc dot gnu.org
2022-04-06 16:30 ` dcb314 at hotmail dot com
2022-04-06 16:32 ` dcb314 at hotmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105164-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).