public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/105164] New: new warning in clang, missing in gcc: -Wbitwise-instead-of-logical
@ 2022-04-05 16:16 dcb314 at hotmail dot com
  2022-04-05 21:24 ` [Bug c++/105164] " dcb314 at hotmail dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: dcb314 at hotmail dot com @ 2022-04-05 16:16 UTC (permalink / raw)
  To: gcc-bugs

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]

^

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-04-06 16:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-05 16:16 [Bug c++/105164] New: new warning in clang, missing in gcc: -Wbitwise-instead-of-logical dcb314 at hotmail dot com
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

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).