Bug 24745 - unpleasant warning for "if (NULL)"
Summary: unpleasant warning for "if (NULL)"
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.2
: P3 minor
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 24746 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-11-08 23:19 UTC by Michael Elizabeth Chastain
Modified: 2007-01-30 23:13 UTC (History)
4 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2005-11-11 17:49:32


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Elizabeth Chastain 2005-11-08 23:19:31 UTC
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
Comment 1 Andrew Pinski 2005-11-08 23:24:04 UTC
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.
Comment 2 Andrew Pinski 2005-11-08 23:24:44 UTC
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.
Comment 3 Andrew Pinski 2005-11-08 23:40:46 UTC
*** Bug 24746 has been marked as a duplicate of this bug. ***
Comment 4 Andrew Pinski 2005-11-11 17:49:32 UTC
Confirmed, not a regression.
Comment 5 Samuel Thibault 2006-01-24 18:35:35 UTC
But still an unpleasant behavior :)
Comment 6 Manuel López-Ibáñez 2007-01-09 15:15:31 UTC
Maybe we shouldn't show any warning when converting NULL to boolean. Perhaps in time for the new Wconversion option...
Comment 7 patchapp@dberlin.org 2007-01-20 09:25:56 UTC
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
Comment 8 Manuel López-Ibáñez 2007-01-30 22:29:22 UTC
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

Comment 9 Manuel López-Ibáñez 2007-01-30 23:13:31 UTC
Fixed for 4.3.