Skip to content

Commit d23449d

Browse files
authored
[Clang] Eliminate shadowing warnings for parameters of explicit object member functions (#114813)
Fixes #95707.
1 parent 9cada10 commit d23449d

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ Improvements to Clang's diagnostics
565565
- Clang now correctly recognises code after a call to a ``[[noreturn]]`` constructor
566566
as unreachable (#GH63009).
567567

568+
- Clang now omits shadowing warnings for parameter names in explicit object member functions (#GH95707).
569+
568570
Improvements to Clang's time-trace
569571
----------------------------------
570572

clang/lib/Sema/SemaDecl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8264,11 +8264,14 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
82648264
DeclContext *NewDC = D->getDeclContext();
82658265

82668266
if (FieldDecl *FD = dyn_cast<FieldDecl>(ShadowedDecl)) {
8267-
// Fields are not shadowed by variables in C++ static methods.
8268-
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC))
8267+
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) {
8268+
// Fields are not shadowed by variables in C++ static methods.
82698269
if (MD->isStatic())
82708270
return;
82718271

8272+
if (!MD->getParent()->isLambda() && MD->isExplicitObjectMemberFunction())
8273+
return;
8274+
}
82728275
// Fields shadowed by constructor parameters are a special case. Usually
82738276
// the constructor initializes the field with the parameter.
82748277
if (isa<CXXConstructorDecl>(NewDC))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -verify -fsyntax-only -std=c++2b -Wshadow-all %s
2+
3+
namespace GH95707 {
4+
struct Foo {
5+
int a; // expected-note 2 {{previous declaration is here}}
6+
7+
void f1(this auto &self, int a) { self.a = a; }
8+
void f2(int a) { } // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
9+
void f3() {
10+
(void)[&](this auto &self, int a) { }; // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
11+
}
12+
};
13+
} // namespace GH95707

0 commit comments

Comments
 (0)