[clang] [Clang] fix false-positive lambda shadow diagnostics in explicit object member functions (PR #165919)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 31 13:58:00 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Oleksandr T. (a-tarasyuk)
<details>
<summary>Changes</summary>
Fixes #<!-- -->163731
---
This PR addresses false-positive shadow diagnostics for lambdas inside explicit object member functions
```cpp
struct S {
int x;
void m(this S &self) {
auto lambda = [](int x) { return x; }; // ok
}
};
```
---
Full diff: https://github.com/llvm/llvm-project/pull/165919.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/Sema/SemaDecl.cpp (+5)
- (modified) clang/test/SemaCXX/warn-shadow-in-lambdas.cpp (+28)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 92fc9381a5868..593e117777314 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -432,6 +432,7 @@ Bug Fixes in This Version
- Fixed a failed assertion with empty filename arguments in ``__has_embed``. (#GH159898)
- Fixed a failed assertion with empty filename in ``#embed`` directive. (#GH162951)
- Fixed a crash triggered by unterminated ``__has_embed``. (#GH162953)
+- Fixed false-positive shadow diagnostics for lambdas in explicit object member functions. (#GH163731)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index fc3aabf5741ca..96aea9877719e 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8640,6 +8640,11 @@ void Sema::DiagnoseShadowingLambdaDecls(const LambdaScopeInfo *LSI) {
<< Shadow.VD->getDeclName() << /*explicitly*/ 0;
Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
} else if (isa<FieldDecl>(ShadowedDecl)) {
+ if (CXXMethodDecl *MD =
+ dyn_cast<CXXMethodDecl>(getFunctionLevelDeclContext()))
+ if (MD->isExplicitObjectMemberFunction())
+ continue;
+
Diag(Shadow.VD->getLocation(),
LSI->isCXXThisCaptured() ? diag::warn_decl_shadow
: diag::warn_decl_shadow_uncaptured_local)
diff --git a/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp b/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
index 0042ef035c84c..47653a9c39d15 100644
--- a/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
+++ b/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
@@ -3,6 +3,7 @@
// RUN: %clang_cc1 -std=c++14 -verify=expected,cxx14 -fsyntax-only -Wshadow-all %s
// RUN: %clang_cc1 -std=c++17 -verify -fsyntax-only -Wshadow-all %s
// RUN: %clang_cc1 -std=c++20 -verify -fsyntax-only -Wshadow-all %s
+// RUN: %clang_cc1 -std=c++23 -verify -fsyntax-only -Wshadow-all %s
void foo(int param) { // expected-note 1+ {{previous declaration is here}}
int var = 0; // expected-note 1+ {{previous declaration is here}}
@@ -268,5 +269,32 @@ int foo() {
}();
#endif
}
+}
+
+#if __cplusplus >= 202302L
+namespace GH163731 {
+struct S1 {
+ int a;
+ void m(this S1 &self) {
+ auto lambda = [](int a) { return a; };
+ }
+};
+
+struct S2 {
+ int a;
+ void m(this S2 &self) {
+ int a = 1; // expected-note {{previous declaration is here}}
+ auto lambda = [](int a) { // expected-warning {{declaration shadows a local variable}}
+ return a;
+ };
+ }
+};
+struct S3 {
+ int a;
+ void m(this S3 &self) {
+ auto lambda = [self](int a) { return a + self.a; };
+ }
+};
}
+#endif
``````````
</details>
https://github.com/llvm/llvm-project/pull/165919
More information about the cfe-commits
mailing list