[Bug c++/77434] warn about suspicious precedence of ternary operator (?:)
manu at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Fri Sep 2 21:17:00 GMT 2016
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77434
--- Comment #9 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
(In reply to Eric Gallager from comment #8)
> As a user, I'd prefer warning about the missing parentheses instead of the
> boolean context thing, the missing parentheses make a lot more sense to me
> and it'd be easier for me to understand how to fix it, whereas the boolean
> context one is a bit more confusing.
It seems to me that they are two different warnings that could be triggered on
similar code. The one warned by the patch would also warn about:
if (a ? 1 : 2)
but not about
gcc_assert (die_offset > 0
&& die_offset <= (loc->dw_loc_opc == DW_OP_call2)
? 0
: 0xffffffff);
nor:
gcc_assert (die_offset > 0
&& die_offset <= (loc->dw_loc_opc == DW_OP_call2)
? n
: 0xffffffff);
This is what clang emits for a similar case (for which GCC does not warn):
prog.cc:3:26: warning: operator '?:' has lower precedence than '<<'; '<<' will
be evaluated first [-Wparentheses]
int n = a << (a == b) ? 1 :2;
~~~~~~~~~~~~~ ^
prog.cc:3:26: note: place parentheses around the '<<' expression to silence
this warning
int n = a << (a == b) ? 1 :2;
^
( )
prog.cc:3:26: note: place parentheses around the '?:' expression to evaluate it
first
int n = a << (a == b) ? 1 :2;
^
( )
Perhaps a middle-ground would be something like:
warning: operator '?:' has lower precedence than '=='; '==' will be evaluated
first and '?:' will evaluate to integers in boolean context [-Wparentheses]
if (a == b ? 1 : 2)
~~~~~~ ^
note: place parentheses around the '==' expression to silence this warning
if (a == b ? 1 : 2)
~~~~~~
(a == b)
note: place parentheses around the '?:' expression to evaluate it first
if (a == b ? 1 : 2)
~~~~~~~~~
(b ? 1 : 2)
More information about the Gcc-bugs
mailing list