[clang] [Sema] Diagnose use of if/else-if condition variable inside else-if/else branch(s) (PR #156436)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 2 07:12:50 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp -- clang/test/Sema/warn-conditional-scope.cpp clang/lib/Sema/SemaStmt.cpp
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index f0f5c6238..a6dc71973 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -21,11 +21,11 @@
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/ExprObjC.h"
 #include "clang/AST/IgnoreExpr.h"
+#include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/AST/StmtCXX.h"
 #include "clang/AST/StmtObjC.h"
 #include "clang/AST/TypeLoc.h"
 #include "clang/AST/TypeOrdering.h"
-#include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
@@ -978,26 +978,28 @@ StmtResult Sema::ActOnIfStmt(SourceLocation IfLoc,
 
     if (auto *CondVar = Cond.get().first) {
       ConditionVar = dyn_cast<VarDecl>(CondVar);
-    }    
+    }
 
     if (ConditionVar) {
       struct ElseVariableUsageChecker
-        : public RecursiveASTVisitor<ElseVariableUsageChecker> {
+          : public RecursiveASTVisitor<ElseVariableUsageChecker> {
         VarDecl *TargetVar;
         Sema &SemaRef;
-        ElseVariableUsageChecker(VarDecl *Var, Sema &S) : TargetVar(Var), SemaRef(S) {}
+        ElseVariableUsageChecker(VarDecl *Var, Sema &S)
+            : TargetVar(Var), SemaRef(S) {}
 
         bool VisitDeclRefExpr(DeclRefExpr *DRE) {
           if (DRE->getDecl() == TargetVar) {
-            SemaRef.Diag(DRE->getBeginLoc(), diag::warn_out_of_scope_var_usage) << TargetVar->getName();
+            SemaRef.Diag(DRE->getBeginLoc(), diag::warn_out_of_scope_var_usage)
+                << TargetVar->getName();
           }
           return true;
         }
-      };   
+      };
 
       ElseVariableUsageChecker Checker(ConditionVar, *this);
       Checker.TraverseStmt(elseStmt);
-    }    
+    }
   }
 
   if (ConstevalOrNegatedConsteval ||

``````````

</details>


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


More information about the cfe-commits mailing list