diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index cd272396252d0..04daaab13dcfb 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -440,6 +440,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..2e9a6215a8834 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -8492,12 +8492,11 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl, DeclContext *NewDC = D->getDeclContext(); if (FieldDecl *FD = dyn_cast(ShadowedDecl)) { - if (CXXMethodDecl *MD = dyn_cast(NewDC)) { - // Fields are not shadowed by variables in C++ static methods. - if (MD->isStatic()) - return; - - if (!MD->getParent()->isLambda() && MD->isExplicitObjectMemberFunction()) + if (CXXMethodDecl *MD = + dyn_cast(getFunctionLevelDeclContext())) { + // Fields aren't shadowed in C++ static members or in member functions + // with an explicit object parameter. + if (MD->isStatic() || MD->isExplicitObjectMemberFunction()) return; } // Fields shadowed by constructor parameters are a special case. Usually diff --git a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp index 76866c4269474..9ce0c5a7434f5 100644 --- a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp +++ b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp @@ -11,3 +11,29 @@ struct Foo { } }; } // namespace GH95707 + +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; }; + } +}; +}