[clang] [Clang] eliminate shadowing warnings for parameters using deducing this (PR #114813)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 4 07:41:21 PST 2024


https://github.com/a-tarasyuk created https://github.com/llvm/llvm-project/pull/114813

Fixes #95707

>From 91ff2b4226110ea35c78f0f1b6ff89b4bec2c788 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Mon, 4 Nov 2024 17:38:46 +0200
Subject: [PATCH] [Clang] eliminate shadowing warnings for parameters using
 deducing this

---
 clang/docs/ReleaseNotes.rst              |  2 ++
 clang/lib/Sema/SemaDecl.cpp              |  7 +++++--
 clang/test/SemaCXX/cxx2b-warn-shadow.cpp | 10 ++++++++++
 3 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/SemaCXX/cxx2b-warn-shadow.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1372e49dfac03c..af8288ee8a8296 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -464,6 +464,8 @@ Improvements to Clang's diagnostics
 
 - Clang now diagnoses ``[[deprecated]]`` attribute usage on local variables (#GH90073).
 
+- Clang now omits shadowing warnings for parameter names using deducing this (#GH95707).
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 1aa3e8edfe1b13..38b07b15eb3eb2 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8236,11 +8236,14 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
   DeclContext *NewDC = D->getDeclContext();
 
   if (FieldDecl *FD = dyn_cast<FieldDecl>(ShadowedDecl)) {
-    // Fields are not shadowed by variables in C++ static methods.
-    if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC))
+    if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) {
+      // Fields are not shadowed by variables in C++ static methods.
       if (MD->isStatic())
         return;
 
+      if (!MD->getParent()->isLambda() && MD->isExplicitObjectMemberFunction())
+        return;
+    }
     // Fields shadowed by constructor parameters are a special case. Usually
     // the constructor initializes the field with the parameter.
     if (isa<CXXConstructorDecl>(NewDC))
diff --git a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
new file mode 100644
index 00000000000000..0cf47f3848979c
--- /dev/null
+++ b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -std=c++2b -Wshadow-all %s
+
+namespace GH95707 {
+struct Foo {
+  int a; // expected-note {{previous declaration is here}}
+
+  void f1(this auto &self, int a) { self.a = a; }
+  void f2(int a) { } // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
+};
+} // namespace GH95707



More information about the cfe-commits mailing list