[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 05:29:07 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);
+ }
----------------
Sirraide wrote:
```suggestion
VarDecl *ConditionVar = dyn_cast_if_present<VarDecl>(Cond.get().first);
```
https://github.com/llvm/llvm-project/pull/156436
More information about the cfe-commits
mailing list