[cfe-dev] [RFC] Compiler warning -Wparentheses for code : while (c = *str++)

Nico Weber thakis at chromium.org
Wed Aug 12 10:14:36 PDT 2015


`while (a = foo())` is often a typo for `while (a == foo())`. Having a
warning for this is useful, and it's a pretty established pattern to use
duplicate parens if the assignment is in fact intended (`while ((a =
foo()))`). In fact, clang even warns in the reverse case if you use the
double parens with a comparison: `while ((a == foo()))` warns as well.

If you don't like this warning, can't you just build with -Wno-parentheses?

On Tue, Aug 11, 2015 at 11:09 PM, Daniel Marjamäki <
Daniel.Marjamaki at evidente.se> wrote:

>
> Hello!
>
> This is a RFC.
>
> I wonder what you think about trying to avoid a compiler warning for such
> code:
>
>         while (c = *str++) {
>                 *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
>         }
>
> Output:
>
> a.c:3:11: warning: using the result of an assignment as a condition
> without parentheses [-Wparentheses]
>         while (c = *str++) {
>                ~~^~~~~~~~
> a.c:3:11: note: place parentheses around the assignment to silence this
> warning
>         while (c = *str++) {
>                  ^
>                (         )
> a.c:3:11: note: use '==' to turn this assignment into an equality
> comparison
>         while (c = *str++) {
>                  ^
>                  ==
> 1 warning generated.
>
>
>
> I would personally recommend that it is avoided in while if there is just
> an assignment and rhs is a nonconstant expression.
>
> Best regards,
> Daniel Marjamäki
>
>
> ..................................................................................................................
> Daniel Marjamäki Senior Engineer
> Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden
>
> Mobile:                 +46 (0)709 12 42 62
> E-mail:                 Daniel.Marjamaki at evidente.se
>
> www.evidente.se
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150812/951aecd4/attachment.html>


More information about the cfe-dev mailing list