[cfe-dev] Why is if ((x == 1)) a warning?

Ted Kremenek kremenek at apple.com
Thu Jun 2 17:20:15 PDT 2011


On Jun 2, 2011, at 4:30 PM, Cory Nelson wrote:

> On Thu, Jun 2, 2011 at 4:01 PM, Andrew Fish <afish at apple.com> wrote:
>> 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?
>> 
> 
> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110602/8517f8f4/attachment.html>


More information about the cfe-dev mailing list