[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
This commit is contained in:
Haojian Wu 2017-06-23 09:36:49 +00:00
parent b794c0a5ca
commit 25e54c93bb
2 changed files with 21 additions and 5 deletions

View File

@ -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) {

View File

@ -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)));
}