Skip to content

Conversation

@a-tarasyuk
Copy link
Member

Fixes #163731


This PR addresses false-positive shadow diagnostics for lambdas inside explicit object member functions

struct S {
  int x;
  void m(this S &self) {
    auto lambda = [](int x) { return x; }; // ok
  }
};

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 31, 2025
@a-tarasyuk a-tarasyuk changed the title [Clang] fixes false-positive lambda shadow diagnostics in explicit object member functions [Clang] fix false-positive lambda shadow diagnostics in explicit object member functions Oct 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2025

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #163731


This PR addresses false-positive shadow diagnostics for lambdas inside explicit object member functions

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 --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

@github-actions
Copy link

github-actions bot commented Nov 1, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@a-tarasyuk a-tarasyuk requested a review from zwuis November 1, 2025 15:48
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM aside from a nit

@a-tarasyuk a-tarasyuk merged commit 316236b into llvm:main Nov 6, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Clang] Lambda parameter shadowing a field while using explicit object parameter

5 participants