Why is if ((x == 1)) a warning?

The gcc definition of -Wparentheses does not seem to include the extraneous parentheses case. I also noticed that older versions of clang don't flag this kind of warning.

I could turn off -Wparentheses, but the other checking it does is quite useful.

I could change the code, but the maintainers of the open source site don't think that this is valid warning. Does anyone know the history or reason behind this warning?

Andrew Fish

./clang -S -Wall ~/work/Compiler/w.c
/Users/fish/work/Compiler/w.c:5:9: warning: equality comparison with extraneous parentheses [-Wparentheses]
if ((x == 1)) {
/Users/fish/work/Compiler/w.c:5:9: note: remove extraneous parentheses around the comparison to silence this warning
if ((x == 1)) {
     ~ ^ ~
/Users/fish/work/Compiler/w.c:5:9: note: use '=' to turn this equality comparison into an assignment
if ((x == 1)) {

You get a warning for if(x = 1). to clarify that you actually want =
instead of == and to disable the warning, you use if((x = 1)). I
guess the warning you're seeing is the reverse of that, making sure
you didn't actually want if((x = 1)).

Yes, that’s exactly right. If Clang didn’t warn here, one could defeat the utility of Wparentheses simply by always using ‘((…))’. The point is for the code to articulate what it intends to do.