This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
GCC feature request: warn on "if (function_name)"
- From: Jamie Lokier <jamie at shareable dot org>
- To: gcc at gcc dot gnu dot org
- Cc: Ross Dickson <ross at datscreative dot com dot au>, linux-kernel at vger dot kernel dot org
- Date: Sat, 14 Feb 2004 04:46:56 +0000
- Subject: GCC feature request: warn on "if (function_name)"
- References: <200402120122.06362.ross@datscreative.com.au> <402CB24E.3070105@gmx.de> <200402140041.17584.ross@datscreative.com.au> <200402141124.50880.ross@datscreative.com.au>
Ross Dickson wrote:
> The fix is to put the brackets back on "!need_resched()" so that we call
> the function and test its return value - not just test the function pointer!
[ Ross' bug was writing "if (!need_resched)" instead of
"if (!need_resched())" ]
I'm very surprised GCC doesn't warn about that. A quick test confirms
GCC 3.2.2 at least doesn't.
So, this is a feature request:
- Warn when a function name is tested in a boolean context.
(A function pointer variable or expression should not be warned for).
By boolean context I mean any place where a function name is
used as a value and tested against zero. Some examples:
if (function_name)
if (function_name && ( <some other expression> ))
if (function_name != 0)
if (function_name == 0)
if (!function_name)
x = function_name ? a : b;
- Don't warn if there are two levels of parantheses.
I know it's occasionally useful to test the NULL-ness of a functin
name, of weak symbols. In most cases, though, it's a bug. If you
really want to check a weak symbol, just write "if ((symbol))". That
syntax is already well known for testing the result of an assignment,
as in "if ((x = 1))" does not yield a warning but "if (x = 1)" does.
Perhaps a later GCC than 3.2.2 already has this test; if someone is
able to check, that would be nice.
Thanks muchly :)
-- Jamie