[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 09:03:16 PDT 2025


================
@@ -971,6 +972,34 @@ StmtResult Sema::ActOnIfStmt(SourceLocation IfLoc,
   if (!ConstevalOrNegatedConsteval && !elseStmt)
     DiagnoseEmptyStmtBody(RParenLoc, thenStmt, diag::warn_empty_if_body);
 
+  // Checks for if/else-if condition variable usage in else-if/else scope
+  if (elseStmt) {
+    VarDecl *ConditionVar = nullptr;
+
+    if (auto *CondVar = Cond.get().first) {
+      ConditionVar = dyn_cast<VarDecl>(CondVar);
+    }    
+
+    if (ConditionVar) {
+      struct ElseVariableUsageChecker
+        : public RecursiveASTVisitor<ElseVariableUsageChecker> {
----------------
arrowten wrote:

@Sirraide RecursiveASTVisitor uses CRTP method which is already optimized if I am not wrong. So, don't you think that DynamicRecursiveASTVisitor will slow down the compilation process?

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


More information about the cfe-commits mailing list