[clang-tidy] Fix null pointer dereference in readability-identifier-naming

Summary:
readability-identifier-naming causes a null pointer dereference when checking an identifier introduced by a structured binding whose right hand side is an undeclared identifier.

Running the check on a file that is just the following results in a crash:
```
auto [left] = right;
```

Patch by Mark Stegeman!

Reviewers: alexfh, hokein, aaron.ballman, JonasToth

Reviewed By: hokein, aaron.ballman

Subscribers: madsravn, xazax.hun, cfe-commits

Tags: #clang-tools-extra, #clang

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

llvm-svn: 361809
This commit is contained in:
Haojian Wu 2019-05-28 11:54:01 +00:00
parent 53f2f32865
commit 2255b31cec
2 changed files with 10 additions and 4 deletions

View File

@ -800,10 +800,11 @@ void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) {
// Fix type aliases in value declarations
if (const auto *Value = Result.Nodes.getNodeAs<ValueDecl>("decl")) {
if (const auto *Typedef =
Value->getType().getTypePtr()->getAs<TypedefType>()) {
addUsage(NamingCheckFailures, Typedef->getDecl(),
Value->getSourceRange());
if (const auto *TypePtr = Value->getType().getTypePtrOrNull()) {
if (const auto *Typedef = TypePtr->getAs<TypedefType>()) {
addUsage(NamingCheckFailures, Typedef->getDecl(),
Value->getSourceRange());
}
}
}

View File

@ -0,0 +1,5 @@
// RUN: %check_clang_tidy -expect-clang-tidy-error %s readability-identifier-naming %t
// This used to cause a null pointer dereference.
auto [left] = right;
// CHECK-MESSAGES: :[[@LINE-1]]:15: error: use of undeclared identifier 'right'