]> granicus.if.org Git - clang/commitdiff
Ignore implicity casts for zero-as-null-pointer-constant warning
authorErich Keane <erich.keane@intel.com>
Wed, 25 Oct 2017 20:23:13 +0000 (20:23 +0000)
committerErich Keane <erich.keane@intel.com>
Wed, 25 Oct 2017 20:23:13 +0000 (20:23 +0000)
The repro in https://bugs.llvm.org/show_bug.cgi?id=34362
caused the left nullptr to be cast to a int* implicitly, which
resulted diagnosing this falsely.

Differential Revision: https://reviews.llvm.org/D39301

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316605 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/Sema.cpp
test/SemaCXX/warn-zero-nullptr.cpp

index 548f336c3b4c4ddaa5a96a2bcc1e0fde1f5aaa19..a9d6cb4c5851d7aa7662221e7ecdcf965c9e2145 100644 (file)
@@ -438,7 +438,7 @@ void Sema::diagnoseNullableToNonnullConversion(QualType DstType,
 void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr* E) {
   if (Kind != CK_NullToPointer && Kind != CK_NullToMemberPointer)
     return;
-  if (E->getType()->isNullPtrType())
+  if (E->IgnoreParenImpCasts()->getType()->isNullPtrType())
     return;
   // nullptr only exists from C++11 on, so don't warn on its absence earlier.
   if (!getLangOpts().CPlusPlus11)
index edd2a759b70f7f49ca0881792611fe76fce32f30..72280405d7ce87619b555d04b00fcc09b4648348 100644 (file)
@@ -25,3 +25,10 @@ void g() {
 // Warn on these too. Matches gcc and arguably makes sense.
 void* pp = (decltype(nullptr))0; // expected-warning{{zero as null pointer constant}}
 void* pp2 = static_cast<decltype(nullptr)>(0); // expected-warning{{zero as null pointer constant}}
+
+// Shouldn't warn.
+namespace pr34362 {
+struct A { operator int*() { return nullptr; } };
+void func() { if (nullptr == A()) {} }
+void func2() { if ((nullptr) == A()) {} }
+}