Explain how the assignment can fail.
> (left = right) ... Parenthesis for priority.
>
> then test the result:
>
> if ( (left = right) ) ... exactly as Lint has shown.
Lint is still wrong. if(left = right) and if((left = right)) are
EXACTLY the same statements. now if(left = right == 0) and if((left =
right) == 0) are different statements (I have no idea what the first
would do anyway). In the first statement there is no need for
parenthesis because there are only two operands and = takes exactly two
operands. You can put in ()s up the wazoo and it will make no
difference.
>
> Lint shows that even this is an error because the result is
> not boolean. It really should be.
>
I think you are missing a common point about Lint. It's a STYLE
checker. It reports errors in STYLE. There are lots of things in C
that are legal that should never be done. left = right ? printf("Hi\n")
: printf("Bye\n"); is legal, it's just very ugly and I'm sure Lint would
report it as an error.
> if ( (left = right) == 0 )
>
> The result of '==' is boolean which 'if' tests. It is, however,
> common usage to do:
C doesn't have a boolean type. In "boolean" expressions it evaluates 0
to false, and non-zero to true.
>
> if(value)
>
> because there usually is a functional equivalent.
>
First off Lint isn't the definitive standard for C. The C standard is,
which clearly states that this is 100% legal. Second, -Wall doesn't
report errors, it reports warnings. And the warning reported is a
sugestion for clarity when humans read the code, not for the compiler.
> Not only does Lint pick up this common error, but so does gcc if
> you turn on all the error reporting with -Wall.
>
> xxx.c: In function `main':
> xxx.c:13: warning: suggest parentheses around assignment used as truth value
>
> Cheers,
> Dick Johnson
-matt
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/