From 818cf5bcb372211edd14b7ab2ae28d3116f58a3c Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Wed, 25 Oct 2017 20:23:13 +0000 Subject: [PATCH] Ignore implicity casts for zero-as-null-pointer-constant warning 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 llvm-svn: 316605 --- clang/lib/Sema/Sema.cpp | 2 +- clang/test/SemaCXX/warn-zero-nullptr.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 548f336c3b4c..a9d6cb4c5851 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -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) diff --git a/clang/test/SemaCXX/warn-zero-nullptr.cpp b/clang/test/SemaCXX/warn-zero-nullptr.cpp index edd2a759b70f..72280405d7ce 100644 --- a/clang/test/SemaCXX/warn-zero-nullptr.cpp +++ b/clang/test/SemaCXX/warn-zero-nullptr.cpp @@ -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(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()) {} } +}