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

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 18 15:51:28 PDT 2021


njames93 updated this revision to Diff 331713.
njames93 marked an inline comment as done.
njames93 added a comment.

Add test case for do inside switch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98338/new/

https://reviews.llvm.org/D98338

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


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-terminating-continue.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-terminating-continue.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-terminating-continue.cpp
@@ -32,6 +32,15 @@
     // 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 @@
       if (n>2) continue;
     }
   } while (false);
+
+  do {
+    switch (2) {
+    case 1:
+    case 2:
+      continue;
+    }
+  } while (false);
 }
Index: clang-tools-extra/clang-tidy/bugprone/TerminatingContinueCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/TerminatingContinueCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/TerminatingContinueCheck.cpp
@@ -26,10 +26,11 @@
              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);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98338.331713.patch
Type: text/x-patch
Size: 1930 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210318/7b9bcf44/attachment.bin>


More information about the cfe-commits mailing list