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

Oliver Hunt via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 29 15:59:09 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) &&
----------------
ojhunt wrote:

In the short term that _might_ be reasonable, but having made the mistake of having diagnostics in codegen previously it's really not a good plan in the long term. Another thing I've found is having logic in codegen that _could_ be in sema often results in duplicating work (at runtime I mean, not duplicating development work).


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


More information about the cfe-commits mailing list