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

Daniel Marjamäki Daniel.Marjamaki at evidente.se
Tue Aug 11 23:09:03 PDT 2015


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


More information about the cfe-dev mailing list