diff options
-rw-r--r-- | clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp | 8 | ||||
-rw-r--r-- | clang-tools-extra/test/clang-tidy/modernize-use-nullptr.cpp | 11 |
2 files changed, 19 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 059c0c5073f..401b10b7f8c 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -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; diff --git a/clang-tools-extra/test/clang-tidy/modernize-use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/modernize-use-nullptr.cpp index e1267bf9a5a..e1ab843404e 100644 --- a/clang-tools-extra/test/clang-tidy/modernize-use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/modernize-use-nullptr.cpp @@ -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); +} |