This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
gcc warnings and [in]valid lvals
- From: Graeme Peterson <gp at qnx dot com>
- To: gcc at sources dot redhat dot com
- Date: Tue, 12 Nov 2002 16:42:32 -0500 (EST)
- Subject: gcc warnings and [in]valid lvals
Hi, all.
Consider the following, where "get_some_string()" may return NULL:
char *str;
if( (str = get_some_string()) = NULL)
In "C", you get the expected failure: "invalid lvalue in assignment".
In "C++", since the type info is kept around (or so I think), then this
works, and the char *str is assigned the return value of the function, and
then NULL. Gcc never complains or warns, regardless of -W levels.
For both "C" and "C++", there is a warning for a situation like this:
if( str = get_some_string() )
If compiled with -Wall, you get "warning: suggest parentheses around
assignment used as truth value". For some reason it doesn't show up in
the other test case.
It seems to me that the first case (which does not generate a warning),
while valid, is more often the result of a typo than intent.
Is this a bug in the warnings code, or expected behavior?
Thanks,
GP