[clang-tidy] Fix a regression issue introduced by r285239.
authorHaojian Wu <hokein@google.com>
Mon, 7 Nov 2016 21:46:24 +0000 (21:46 +0000)
committerHaojian Wu <hokein@google.com>
Mon, 7 Nov 2016 21:46:24 +0000 (21:46 +0000)
Summary:
r285239 changes the behavior of AST CXXDefaultArgExpr node.

Update `modernize-use-nullptr` to handle CXXDefaultArgExpr correctly.

Reviewers: alexfh

Subscribers: cfe-commits

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

llvm-svn: 286156

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

index 059c0c5..401b10b 100644 (file)
@@ -190,13 +190,21 @@ public:
   // within a cast expression.
   bool VisitStmt(Stmt *S) {
     CastExpr *C = dyn_cast<CastExpr>(S);
+    // Catch the castExpr inside cxxDefaultArgExpr.
+    if (auto *E = dyn_cast<CXXDefaultArgExpr>(S))
+      C = dyn_cast<CastExpr>(E->getExpr());
     if (!C) {
       FirstSubExpr = nullptr;
       return true;
     }
+
     if (!FirstSubExpr)
       FirstSubExpr = C->getSubExpr()->IgnoreParens();
 
+    // Ignore the expr if it is already a nullptr literal expr.
+    if (isa<CXXNullPtrLiteralExpr>(FirstSubExpr))
+      return true;
+
     if (C->getCastKind() != CK_NullToPointer &&
         C->getCastKind() != CK_NullToMemberPointer) {
       return true;
index e1267bf..e1ab843 100644 (file)
@@ -217,3 +217,14 @@ C<bool, F(0)> c;
 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use nullptr
 // CHECK-FIXES: C<bool, F(nullptr)> c;
 #undef F
+
+// Test default argument expression.
+struct D {
+  explicit D(void *t, int *c = NULL) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use nullptr
+  // CHECK-FIXES: explicit D(void *t, int *c = nullptr) {}
+};
+
+void test_default_argument() {
+  D(nullptr);
+}