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

            Bug ID: 105283
           Summary: Ternary operator and precedence warning
           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 int g( int);

void f( int a, int b)
{
        if (a & 0x03 == 3)
                g( 1);

        if (a + (b > 10) ? g( 1) : g( 2))
                g( 10);
}

gcc finds the first problem, but not the second:

$ /home/dcb/gcc/results/bin/gcc -c -g -O2 -Wall -Wextra -Wparentheses
-Wint-in-bool-context apr15b.cc
apr15b.cc: In function ‘void f(int, int)’:
apr15b.cc:6:22: warning: suggest parentheses around comparison in operand of
‘&’ [-Wparentheses]
    6 |         if (a & 0x03 == 3)
      |                 ~~~~~^~~~
$ 

Here is recent clang on the same code:

$ /home/dcb/llvm/results/bin/clang -c -Wall   apr15b.cc
apr15b.cc:6:8: warning: & has lower precedence than ==; == will be evaluated
first [-Wparentheses]
        if (a & 0x03 == 3)
              ^~~~~~~~~~~
apr15b.cc:6:8: note: place parentheses around the '==' expression to silence
this warning
        if (a & 0x03 == 3)
              ^
                (        )
apr15b.cc:6:8: note: place parentheses around the & expression to evaluate it
first
        if (a & 0x03 == 3)
              ^
            (       )
apr15b.cc:9:19: warning: operator '?:' has lower precedence than '+'; '+' will
be evaluated first [-Wparentheses]
        if (a + (b > 10) ? g( 1) : g( 2))
            ~~~~~~~~~~~~ ^
apr15b.cc:9:19: note: place parentheses around the '+' expression to silence
this warning
        if (a + (b > 10) ? g( 1) : g( 2))
                         ^
            (           )
apr15b.cc:9:19: note: place parentheses around the '?:' expression to evaluate
it first
        if (a + (b > 10) ? g( 1) : g( 2))
                         ^
                (                       )
2 warnings generated.
$ 

Please note I only had to use -Wall on clang to get the second warning
and the warning message itself is IMHO more descriptive.

Reply via email to