public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/105283] New: Ternary operator and precedence warning
@ 2022-04-15  8:32 dcb314 at hotmail dot com
  0 siblings, 0 replies; only message in thread
From: dcb314 at hotmail dot com @ 2022-04-15  8:32 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-15  8:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-15  8:32 [Bug c/105283] New: Ternary operator and precedence warning 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).