[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 09:05:39 PDT 2025


================
@@ -8508,10 +8525,17 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
           return;
         }
       }
-      if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl);
-          VD && VD->hasLocalStorage()) {
-        // A variable can't shadow a local variable in an enclosing scope, if
-        // they are separated by a non-capturing declaration context.
+      // Apply scoping logic to both VarDecl and BindingDecl
+      bool shouldApplyScopingLogic = false;
+      if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl)) {
+        shouldApplyScopingLogic = VD->hasLocalStorage();
----------------
zwuis wrote:

> Done at 27fc9b7095e6ab993782e121e1ff1a3776b2ab0a

Oh the changes are bigger than what I think:

```diff
-} else if (isa<BindingDecl>(ShadowedDecl)) {
-  shouldApplyScopingLogic = true;
+} else if (const auto *BD = dyn_cast<BindingDecl>(ShadowedDecl)) {
+  HasLocalStorage = cast<VarDecl>(BD->getDecomposedDecl())->hasLocalStorage();
```

WDYT?

---

By the way, variable names should start with an upper-case letter. See <https://llvm.org/docs/CodingStandards.html#name-types-functions-variables-and-enumerators-properly>.

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


More information about the cfe-commits mailing list