[clang] [clang][Sema] Fix false positive -Wshadow with structured binding captures (PR #157667)

Yanzuo Liu via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 10 05:56:13 PDT 2025


================
@@ -8496,6 +8498,21 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
                 ->ShadowingDecls.push_back({D, VD});
             return;
           }
+        } else if (isa<BindingDecl>(ShadowedDecl)) {
+          // Apply lambda capture logic only when D is actually a lambda capture
+          if (isa<VarDecl>(D) && cast<VarDecl>(D)->isInitCapture()) {
+            if (RD->getLambdaCaptureDefault() == LCD_None) {
+              // BindingDecls cannot be explicitly captured, so always treat as
+              // uncaptured
+              WarningDiag = diag::warn_decl_shadow_uncaptured_local;
+            } else {
+              // Same deferred handling as VarDecl
+              cast<LambdaScopeInfo>(getCurFunction())
+                  ->ShadowingDecls.push_back({D, ShadowedDecl});
+              return;
+            }
+          }
+          // For non-init-capture cases, fall through to regular shadow logic
----------------
zwuis wrote:

Structured bindings should be handled same as variables. We can consolidate these two parts. (update `getCaptureLocation()` to take `const ValueDecl *` first)

```cpp
if (isa<VarDecl, BindingDecl>(ShadowedDecl)) {
  const auto *VD = cast<ValueDecl>(ShadowedDecl);
  // ...
}
```

Ditto below in `Sema::DiagnoseShadowingLambdaDecls()`.

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


More information about the cfe-commits mailing list