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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

<details>
<summary>Changes</summary>

Fixes #<!-- -->95707

---
Full diff: https://github.com/llvm/llvm-project/pull/114813.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+2) 
- (modified) clang/lib/Sema/SemaDecl.cpp (+5-2) 
- (added) clang/test/SemaCXX/cxx2b-warn-shadow.cpp (+10) 


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

``````````

</details>


https://github.com/llvm/llvm-project/pull/114813


More information about the cfe-commits mailing list