[clang-tools-extra] [clang-tidy] Fix redundant-branch-condition false positive in loops (PR #225827)

Mamadou Wane via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 24 06:46:54 PDT 2026


================
@@ -40,6 +42,29 @@ static bool isChangedBefore(const Stmt *S, const Stmt *NextS, const Stmt *PrevS,
          SM.isBeforeInTranslationUnit(MutS->getEndLoc(), NextS->getBeginLoc());
 }
 
+/// Returns the outermost loop that encloses `S` and is itself enclosed by
+/// `Outer`, or null if there is no such loop. The walk passes through
+/// declarations, such as a variable initialized by a lambda, but stops at the
+/// enclosing function.
+static const Stmt *getOutermostLoopBetween(const Stmt *S, const Stmt *Outer,
+                                           ASTContext *Context) {
+  const Stmt *Loop = nullptr;
+  DynTypedNodeList Parents = Context->getParents(*S);
+  while (!Parents.empty()) {
+    const DynTypedNode Parent = Parents[0];
+    if (Parent.get<FunctionDecl>())
+      break;
+    if (const auto *ParentStmt = Parent.get<Stmt>()) {
+      if (ParentStmt == Outer)
+        break;
+      if (isa<ForStmt, WhileStmt, DoStmt, CXXForRangeStmt>(ParentStmt))
+        Loop = ParentStmt;
+    }
+    Parents = Context->getParents(Parent);
----------------
mamadou-wane wrote:

Added the comment in c19d0254d.

https://github.com/llvm/llvm-project/pull/225827


More information about the cfe-commits mailing list