[Bug c++/105164] New: new warning in clang, missing in gcc: -Wbitwise-instead-of-logical

dcb314 at hotmail dot com gcc-bugzilla@gcc.gnu.org
Tue Apr 5 16:16:56 GMT 2022


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]

^


More information about the Gcc-bugs mailing list