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

Justin Bogner mail at justinbogner.com
Tue Aug 11 23:37:51 PDT 2015


Daniel Marjamäki <Daniel.Marjamaki at evidente.se> writes:
> 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++) {
>                  ^
>                (         )

This tells you what to do to avoid the warning, spell this like so:

  while ((c = *str++)) {
    *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
  }

> 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.

The trade off here is between the programmer overhead of adding an extra
set of parentheses vs the likelihood of accidentally typing "=" when you
mean "==". In real code we've seen that this warning finds enough errors
that burdening the programmer to be more explicit when they really want
an assignment is worth it.

To illustrate why a side-effectful RHS isn't sufficient to turn off this
warning, consider:

  char end = lastline ? '\0' : '\n';
  while (end == *str++) { /* do something with *str */ }

This is a bit contrived, but it hints at the complexity of turning off
-Wparentheses for particular cases - it's both easier to get right and
easier to explain if we just say "put extra parens around assignments in
conditions to make it obvious that they're intentional".



More information about the cfe-dev mailing list