[clang-tools-extra] r306091 - [clang-tidy] Fix a false positive in modernize-use-nullptr.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 23 02:36:50 PDT 2017


Author: hokein
Date: Fri Jun 23 04:36:49 2017
New Revision: 306091

URL: http://llvm.org/viewvc/llvm-project?rev=306091&view=rev
Log:
[clang-tidy] Fix a false positive in modernize-use-nullptr.

Summary:
The FP happens when a casting nullptr expression is used within a NULL-default-arguemnt cxx constructor.

Before the fix, the check will give a warning on nullptr when running
with the test case, which should not happen:

```
G(g(static_cast<char*>(nullptr)));
    ^~~~~~~~~~~
    nullptr
```

Reviewers: alexfh

Reviewed By: alexfh

Subscribers: cfe-commits, xazax.hun

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

Modified:
    clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp?rev=306091&r1=306090&r2=306091&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp Fri Jun 23 04:36:49 2017
@@ -200,12 +200,14 @@ public:
       return true;
     }
 
-    if (!FirstSubExpr)
-      FirstSubExpr = C->getSubExpr()->IgnoreParens();
-
-    // Ignore the expr if it is already a nullptr literal expr.
-    if (isa<CXXNullPtrLiteralExpr>(FirstSubExpr))
+    auto* CastSubExpr = C->getSubExpr()->IgnoreParens();
+    // Ignore cast expressions which cast nullptr literal.
+    if (isa<CXXNullPtrLiteralExpr>(CastSubExpr)) {
       return true;
+    }
+
+    if (!FirstSubExpr)
+      FirstSubExpr = CastSubExpr;
 
     if (C->getCastKind() != CK_NullToPointer &&
         C->getCastKind() != CK_NullToMemberPointer) {

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp?rev=306091&r1=306090&r2=306091&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp Fri Jun 23 04:36:49 2017
@@ -261,3 +261,17 @@ class TemplateClass {
 void IgnoreSubstTemplateType() {
   TemplateClass<int*> a(1);
 }
+
+// Test on casting nullptr.
+struct G {
+  explicit G(bool, const char * = NULL) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use nullptr
+  // CHECK-FIXES: explicit G(bool, const char * = nullptr) {}
+};
+bool g(const char*);
+void test_cast_nullptr() {
+  G(g(nullptr));
+  G(g((nullptr)));
+  G(g(static_cast<char*>(nullptr)));
+  G(g(static_cast<const char*>(nullptr)));
+}




More information about the cfe-commits mailing list