-
Notifications
You must be signed in to change notification settings - Fork 15k
[clang][Sema] Fix false positive -Wshadow with structured binding captures #157667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ivanmurashko
merged 9 commits into
llvm:main
from
ivanmurashko:fix-structured-binding-lambda-shadow-68605
Sep 15, 2025
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6aa3e64
[clang][Sema] Fix false positive -Wshadow with structured binding cap…
ivanmurashko c0723fe
[Sema] Fix inconsistent shadow warnings for lambda capture of structu…
ivanmurashko 74a2ff7
Fix warn-shadow-in-lambdas test for structured bindings
ivanmurashko 27fc9b7
[Sema] Consolidate VarDecl and BindingDecl shadow detection using Val…
ivanmurashko 578e61b
[Clang] Simplify BindingDecl scoping logic using getDecomposedDecl()-…
ivanmurashko 13a6db5
[Clang] Add release note for consistent structured binding shadow war…
ivanmurashko e1c4dc2
Apply suggestions from code review
ivanmurashko ef25bd2
[Clang] Add missing newline at end of PR68605.cpp test file
ivanmurashko a4a34de
Merge branch 'main' into fix-structured-binding-lambda-shadow-68605
ivanmurashko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // RUN: %clang_cc1 -verify -fsyntax-only -std=c++20 -Wshadow %s | ||
| // RUN: %clang_cc1 -verify=all -fsyntax-only -std=c++20 -Wshadow-all %s | ||
|
|
||
| // Test for issue #68605: Inconsistent shadow warnings for lambda capture of structured bindings. | ||
| // | ||
| // The issue was that structured binding lambda captures were incorrectly classified | ||
| // as regular shadow warnings (shown with -Wshadow) while regular parameter captures | ||
| // were classified as uncaptured-local warnings (shown only with -Wshadow-all). | ||
| // | ||
| // This test validates that both VarDecl and BindingDecl lambda captures now | ||
| // behave consistently: no warnings with -Wshadow, but uncaptured-local warnings | ||
| // with -Wshadow-all. | ||
|
|
||
| namespace std { | ||
| template<typename T> T&& move(T&& t) { return static_cast<T&&>(t); } | ||
| } | ||
|
|
||
| namespace issue_68605 { | ||
|
|
||
| // Simple pair-like struct for testing | ||
| struct Pair { | ||
| int first; | ||
| int second; | ||
| Pair(int f, int s) : first(f), second(s) {} | ||
| }; | ||
|
|
||
| // Test case 1: Regular parameter - consistent behavior | ||
| void foo1(Pair val) { // all-note {{previous declaration is here}} | ||
| [val = std::move(val)](){}(); // all-warning {{declaration shadows a local variable}} | ||
| } | ||
|
|
||
| // Test case 2: Structured binding - now consistent with regular parameter | ||
| void foo2(Pair val) { | ||
| auto [a,b] = val; // all-note {{previous declaration is here}} | ||
| [a = std::move(a)](){}(); // all-warning {{declaration shadows a structured binding}} | ||
| } | ||
|
|
||
| // Test case 3: Multiple captures showing consistent behavior | ||
| void foo3() { | ||
| Pair data{42, 100}; | ||
| auto [id, value] = data; // all-note 2{{previous declaration is here}} | ||
|
|
||
| // Both show consistent uncaptured-local warnings with -Wshadow-all | ||
| auto lambda1 = [id = id](){ return id; }; // all-warning {{declaration shadows a structured binding}} | ||
| auto lambda2 = [value = value](){ return value; }; // all-warning {{declaration shadows a structured binding}} | ||
| } | ||
|
|
||
| // Test case 4: Mixed scenario showing consistent behavior | ||
| void foo4() { | ||
| int regular_var = 10; // all-note {{previous declaration is here}} | ||
| Pair pair_data{1, 2}; | ||
| auto [x, y] = pair_data; // all-note 2{{previous declaration is here}} | ||
|
|
||
| // All captures now show consistent uncaptured-local warnings with -Wshadow-all | ||
| auto lambda1 = [regular_var = regular_var](){}; // all-warning {{declaration shadows a local variable}} | ||
| auto lambda2 = [x = x](){}; // all-warning {{declaration shadows a structured binding}} | ||
| auto lambda3 = [y = y](){}; // all-warning {{declaration shadows a structured binding}} | ||
| } | ||
|
|
||
| // Test case 5: Ensure we don't break existing shadow detection for actual shadowing | ||
| void foo5() { | ||
| int outer = 5; // expected-note {{previous declaration is here}} all-note {{previous declaration is here}} | ||
| auto [a, b] = Pair{1, 2}; // expected-note {{previous declaration is here}} all-note {{previous declaration is here}} | ||
|
|
||
| // This SHOULD still warn - it's actual shadowing within the lambda body | ||
| auto lambda = [outer, a](){ // expected-note {{variable 'outer' is explicitly captured here}} all-note {{variable 'outer' is explicitly captured here}} expected-note {{variable 'a' is explicitly captured here}} all-note {{variable 'a' is explicitly captured here}} | ||
| int outer = 10; // expected-warning {{declaration shadows a local variable}} all-warning {{declaration shadows a local variable}} | ||
| int a = 20; // expected-warning {{declaration shadows a structured binding}} all-warning {{declaration shadows a structured binding}} | ||
| }; | ||
| } | ||
|
|
||
| } // namespace issue_68605 | ||
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.