[clang-tools-extra] d591bdc - [clang-tidy] Fixed crash 44745 in readability-else-after-return

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 2 06:04:53 PST 2020


Author: Nathan James
Date: 2020-02-02T14:04:46Z
New Revision: d591bdce6d623208d4aeb335a762d839f0f8f0f7

URL: https://github.com/llvm/llvm-project/commit/d591bdce6d623208d4aeb335a762d839f0f8f0f7
DIFF: https://github.com/llvm/llvm-project/commit/d591bdce6d623208d4aeb335a762d839f0f8f0f7.diff

LOG: [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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
index 93b197a52a52..b3b4e0de2bf0 100644
--- a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp
@@ -27,6 +27,8 @@ static const char WarningMessage[] = "do not use 'else' after '%0'";
 static const char WarnOnUnfixableStr[] = "WarnOnUnfixable";
 
 const DeclRefExpr *findUsage(const Stmt *Node, int64_t DeclIdentifier) {
+  if (!Node)
+    return nullptr;
   if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Node)) {
     if (DeclRef->getDecl()->getID() == DeclIdentifier) {
       return DeclRef;
@@ -44,6 +46,8 @@ const DeclRefExpr *findUsage(const Stmt *Node, int64_t DeclIdentifier) {
 const DeclRefExpr *
 findUsageRange(const Stmt *Node,
                const llvm::iterator_range<int64_t *> &DeclIdentifiers) {
+  if (!Node)
+    return nullptr;
   if (const auto *DeclRef = dyn_cast<DeclRefExpr>(Node)) {
     if (llvm::is_contained(DeclIdentifiers, DeclRef->getDecl()->getID())) {
       return DeclRef;

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability-else-after-return.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-else-after-return.cpp
index 69ac9eb29571..1e3b4cf5755a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-else-after-return.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-else-after-return.cpp
@@ -213,3 +213,16 @@ int lifeTimeExtensionTests(int a) {
     return b;
   }
 }
+
+void test_B44745() {
+  // This is the actual minimum test case for the crash in bug 44745. We aren't
+  // too worried about the warning or fix here, more we don't want a crash.
+  // CHECK-MESSAGES: :[[@LINE+3]]:5: warning: do not use 'else' after 'return' [readability-else-after-return]
+  if (auto X = false) {
+    return;
+  } else {
+    for (;;) {
+    }
+  }
+  return;
+}


        


More information about the cfe-commits mailing list