C++ programs with "if (NULL)" generate an unpleasant warning even with default compiler options. Code with "if (NULL)" comes from of macro expansion or auto-generated code. Warnings are a matter of taste so I won't ask "make all the warnings match my taste", but I do petition you to revisit which of these lines should generate warnings and apply your own taste. This patch looks like the last time these warnings were modified: http://gcc.gnu.org/ml/gcc-patches/2005-01/msg02230.html Mark Mitchell, C++ PATCH: PR 19457 a PR 19349 Test program: int foo() { if (__null) return -1; if (!__null) ; else return -1; if (__null == __null) ; else return -1; if (__null != __null) return -1; if (__null == __null) ; else return -1; if (__null != 0) ; else return -1; return 0; } int main() { return foo() == 0; } Warnings shown: hollerith:~/null-arithmetic$ /home/mec/gcc-4.0.2/install/bin/gcc -c null-arithmetic.cc null-arithmetic.cc: In function 'int foo()': null-arithmetic.cc:2: warning: converting to non-pointer type 'bool' from NULL null-arithmetic.cc:3: warning: converting to non-pointer type 'bool' from NULL null-arithmetic.cc:4: warning: NULL used in arithmetic null-arithmetic.cc:5: warning: NULL used in arithmetic null-arithmetic.cc:6: warning: NULL used in arithmetic null-arithmetic.cc:7: warning: NULL used in arithmetic hollerith:~/null-arithmetic$ /home/mec/gcc-4.1-20051105/install/bin/gcc -c null-arithmetic.cc null-arithmetic.cc: In function 'int foo()': null-arithmetic.cc:2: warning: converting to non-pointer type 'bool' from NULL null-arithmetic.cc:3: warning: converting to non-pointer type 'bool' from NULL null-arithmetic.cc:4: warning: NULL used in arithmetic null-arithmetic.cc:5: warning: NULL used in arithmetic null-arithmetic.cc:6: warning: NULL used in arithmetic null-arithmetic.cc:7: warning: NULL used in arithmetic
Hmm, all of these warnings were present before 4.0.0. The only warning that is a regression is the second: "argument to non-pointer type `bool' from NULL" warning which did not warn in 3.3.3 and before but does in 3.4.0.
Oh and the first "argument to non-pointer type `bool' from NULL" was new in 3.0.x and above and did not show up in 2.95.3.
*** Bug 24746 has been marked as a duplicate of this bug. ***
Confirmed, not a regression.
But still an unpleasant behavior :)
Maybe we shouldn't show any warning when converting NULL to boolean. Perhaps in time for the new Wconversion option...
Subject: Bug number PR c++/24745 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-01/msg01657.html
Subject: Bug 24745 Author: manu Date: Tue Jan 30 22:29:11 2007 New Revision: 121361 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=121361 Log: 2007-01-30 Manuel Lopez-Ibanez <manu@gcc.gnu.org> PR c++/24745 * doc/invoke.texi (Wpointer-arith): Document warning. cp/ * typeck.c (build_binary_op): Fix logic for warning. Move warning to -Wpointer-arith. * call.c (convert_like_real): Don't warn when converting to boolean type. testsuite/ * g++.dg/warn/null4.C: New. Added: trunk/gcc/testsuite/g++.dg/warn/null4.C Modified: trunk/gcc/ChangeLog trunk/gcc/cp/ChangeLog trunk/gcc/cp/call.c trunk/gcc/cp/typeck.c trunk/gcc/doc/invoke.texi trunk/gcc/testsuite/ChangeLog
Fixed for 4.3.