This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: suggest parentheses around assignment used as truth value


Marc Rouaix wrote:

> I'd just like to register my opiniont that code like
>
> if (x = p()) {...}
>
> shouldn't generate a warning under gcc -Wall.

What if you meant to write "if (x == p()) {...}"? Such bugs are
usually very hard to find.

> Even if you feel that code like this is obscure,

It is _not_ obscure, it is in fact used quite often; therefore the
possibility of a typo is large enough to warrant a warning.

> the recommended
>
> if ((x = p())) {...}
>
> is just bizarre.

Can you recommend a better way of telling the compiler that you know
what you're doing? I mean, apart from the obvious

     x = p(); if (x) {...}

    Brane

--
Branko Cibej   <branko.cibej@hermes.si>
HERMES SoftLab, Litijska 51, 1000 Ljubljana, Slovenia
phone: (++386 61) 186 53 49  fax: (++386 61) 186 52 70




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]