[clang] [Clang] Add -Wtrivial-auto-var-init warning for unreachable variables (PR #178318)

Justin Stitt via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 30 14:52:41 PST 2026


================
@@ -1955,10 +1956,29 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
   // If this local has an initializer, emit it now.
   const Expr *Init = D.getInit();
 
+  auto wouldAutoInitApply = [&]() {
+    if (D.isConstexpr() || D.getAttr<UninitializedAttr>())
+      return false;
+    if (type->getAsTagDecl() &&
+        type->getAsTagDecl()->hasAttr<NoTrivialAutoVarInitAttr>())
+      return false;
+    if (CurFuncDecl && CurFuncDecl->hasAttr<NoTrivialAutoVarInitAttr>())
+      return false;
+    return getContext().getLangOpts().getTrivialAutoVarInit() !=
+           LangOptions::TrivialAutoVarInitKind::Uninitialized;
+  };
+
   // If we are at an unreachable point, we don't need to emit the initializer
   // unless it contains a label.
   if (!HaveInsertPoint()) {
     if (!Init || !ContainsLabel(Init)) {
+      // Warn when -ftrivial-auto-var-init would initialize but can't.
+      if (D.getDeclName() && isTrivialInitializer(Init) &&
----------------
JustinStitt wrote:

Ok PTAL at [12fff88](https://github.com/llvm/llvm-project/pull/178318/commits/12fff88c959586d97f74a164c512053dfa4a775b). I added a method `HandleUnreachableBlock()` to the `UnreachableCodeHandler` class so I could re-use the cfg walk from `reachable_code::FindUnreachableCode`.

The diff looks larger because clang format forced me to de-indent everything in the anonymous namespace, I really just added `HandleUnreachableBlock()` and the flag `CheckTrivialAutoVarInit` throughout.

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


More information about the cfe-commits mailing list