[clang-tools-extra] 4dd92d6 - [clang-tidy] Fix bugprone-terminating-continue when continue appears inside a switch

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 20 03:59:49 PDT 2021


Author: Nathan James
Date: 2021-03-20T10:59:37Z
New Revision: 4dd92d61dbc4b3c51a98e1d0bfccabed24759ba9

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

LOG: [clang-tidy] Fix bugprone-terminating-continue when continue appears inside a switch

Don't emit a warning if the `continue` appears in a switch context as changing it to `break` will break out of the switch rather than a do loop containing the switch.
Fixes https://llvm.org/PR49492.

Reviewed By: aaron.ballman

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/bugprone/TerminatingContinueCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/bugprone-terminating-continue.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/TerminatingContinueCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TerminatingContinueCheck.cpp
index 43402a221218..65da4c325de4 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TerminatingContinueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TerminatingContinueCheck.cpp
@@ -26,10 +26,11 @@ void TerminatingContinueCheck::registerMatchers(MatchFinder *Finder) {
              equalsBoundNode("closestLoop"));
 
   Finder->addMatcher(
-      continueStmt(hasAncestor(stmt(anyOf(forStmt(), whileStmt(),
-                                          cxxForRangeStmt(), doStmt()))
-                                   .bind("closestLoop")),
-                   hasAncestor(DoWithFalse))
+      continueStmt(
+          hasAncestor(stmt(anyOf(forStmt(), whileStmt(), cxxForRangeStmt(),
+                                 doStmt(), switchStmt()))
+                          .bind("closestLoop")),
+          hasAncestor(DoWithFalse))
           .bind("continue"),
       this);
 }

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-terminating-continue.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-terminating-continue.cpp
index 4bdcbc42fc47..04fc4a80ea7d 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-terminating-continue.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-terminating-continue.cpp
@@ -32,6 +32,15 @@ void f() {
     // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: 'continue' in loop with false condition is equivalent to 'break' [bugprone-terminating-continue]
     // CHECK-FIXES: if (x > 0) break;
   } while (false);
+
+  switch (2) {
+  case 2:
+    do {
+      continue; // LoopInSwitch
+      // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'continue' in loop with false condition is equivalent to 'break' [bugprone-terminating-continue]
+      // CHECK-FIXES: break; // LoopInSwitch
+    } while (0);
+  }
 }
 
 void g() {
@@ -62,4 +71,12 @@ void g() {
       if (n>2) continue;
     }
   } while (false);
+
+  do {
+    switch (2) {
+    case 1:
+    case 2:
+      continue;
+    }
+  } while (false);
 }


        


More information about the cfe-commits mailing list