Re: (0 == foo), rather than (foo == 0)

From: Linus Torvalds
Date: Wed Mar 10 2004 - 22:14:14 EST




On Thu, 11 Mar 2004, Peter Williams wrote:
>
> As somebody pointed out -Wall will (help) detect most of these errors by
> suggesting () be placed around any expression of the form a = b that
> occurs inside a simple boolean expression which will cause those people
> who care about eliminating warning messages to reevaluate the code and
> make sure they really meant a = b and replace it with (a = b) to get rid
> of the warning error.

Actually, don't just add parenthesis. They get rid of the warning, but
they don't actually make for very pretty reading.

I don't know why the gcc warning suggests adding parentheses, since they
have no semantic meaning, and are _horrible_ from a syntactic standpoint.

The warning should be there whether there are parenthesis or not, and it
should state that you should have an explicit inequality expression. So if
you have

if (a = b)
...

and you really _mean_ that, then the way to write it sanely is to just
write it as

if ((a = b) != 0)
...

which makes it much clearer what you're actually doing.

Linus
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/