Skip to content

Commit d591bdc

Browse files
committed
[clang-tidy] Fixed crash 44745 in readability-else-after-return
Summary: Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=44745 | readability-else-after-return crashes ]] Reviewers: aaron.ballman, alexfh, hokein, JonasToth, gribozavr2 Reviewed By: alexfh Subscribers: merge_guards_bot, xazax.hun, cfe-commits Tags: #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D73841
1 parent d431c5d commit d591bdc

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ static const char WarningMessage[] = "do not use 'else' after '%0'";
2727
static const char WarnOnUnfixableStr[] = "WarnOnUnfixable";
2828

2929
const DeclRefExpr *findUsage(const Stmt *Node, int64_t DeclIdentifier) {
30+
if (!Node)
31+
return nullptr;
3032
if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Node)) {
3133
if (DeclRef->getDecl()->getID() == DeclIdentifier) {
3234
return DeclRef;
@@ -44,6 +46,8 @@ const DeclRefExpr *findUsage(const Stmt *Node, int64_t DeclIdentifier) {
4446
const DeclRefExpr *
4547
findUsageRange(const Stmt *Node,
4648
const llvm::iterator_range<int64_t *> &DeclIdentifiers) {
49+
if (!Node)
50+
return nullptr;
4751
if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Node)) {
4852
if (llvm::is_contained(DeclIdentifiers, DeclRef->getDecl()->getID())) {
4953
return DeclRef;

clang-tools-extra/test/clang-tidy/checkers/readability-else-after-return.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,16 @@ int lifeTimeExtensionTests(int a) {
213213
return b;
214214
}
215215
}
216+
217+
void test_B44745() {
218+
// This is the actual minimum test case for the crash in bug 44745. We aren't
219+
// too worried about the warning or fix here, more we don't want a crash.
220+
// CHECK-MESSAGES: :[[@LINE+3]]:5: warning: do not use 'else' after 'return' [readability-else-after-return]
221+
if (auto X = false) {
222+
return;
223+
} else {
224+
for (;;) {
225+
}
226+
}
227+
return;
228+
}

0 commit comments

Comments
 (0)