[PATCH] D34524: [clang-tidy] Fix a false positive in modernize-use-nullptr.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 23 02:37:38 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL306091: [clang-tidy] Fix a false positive in modernize-use-nullptr. (authored by hokein).

Changed prior to commit:
  https://reviews.llvm.org/D34524?vs=103615&id=103702#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D34524

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


Index: clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp
===================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp
@@ -200,12 +200,14 @@
       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) {
Index: clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp
===================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp
@@ -261,3 +261,17 @@
 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)));
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34524.103702.patch
Type: text/x-patch
Size: 1645 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170623/29a1de4f/attachment.bin>


More information about the cfe-commits mailing list