summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2017-06-23 09:36:49 +0000
committerHaojian Wu <hokein@google.com>2017-06-23 09:36:49 +0000
commit25e54c93bba13c08a26e073cdbc9dc3af58c093d (patch)
tree3508592e170e3310f7a9162b11cb683d951bf114
parentb794c0a5ca23d5e225143e0ba9f8a3e6ca61d46e (diff)
downloadbcm5719-llvm-25e54c93bba13c08a26e073cdbc9dc3af58c093d.tar.gz
bcm5719-llvm-25e54c93bba13c08a26e073cdbc9dc3af58c093d.zip
[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 llvm-svn: 306091
-rw-r--r--clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp12
-rw-r--r--clang-tools-extra/test/clang-tidy/modernize-use-nullptr.cpp14
2 files changed, 21 insertions, 5 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
index 198ccb0a816..9de4e622585 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp
@@ -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) {
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 7e1eb1459c0..45d44602dd2 100644
--- a/clang-tools-extra/test/clang-tidy/modernize-use-nullptr.cpp
+++ b/clang-tools-extra/test/clang-tidy/modernize-use-nullptr.cpp
@@ -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)));
+}
OpenPOWER on IntegriCloud