[clang-tools-extra] 42bc327 - [clang-tidy] Fix `readability-redundant-declaration` false positive for template friend declaration

Fabian Wolff via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 17 11:57:14 PST 2022


Author: Fabian Wolff
Date: 2022-01-17T20:50:32+01:00
New Revision: 42bc3275d3687524ddc0d20c72722b9324f87be4

URL: https://github.com/llvm/llvm-project/commit/42bc3275d3687524ddc0d20c72722b9324f87be4
DIFF: https://github.com/llvm/llvm-project/commit/42bc3275d3687524ddc0d20c72722b9324f87be4.diff

LOG: [clang-tidy] Fix `readability-redundant-declaration` false positive for template friend declaration

Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=48086 | PR#48086 ]]. The problem is that the current matcher uses `hasParent()` to detect friend declarations, but for a template friend declaration, the immediate parent of the `FunctionDecl` is a `FunctionTemplateDecl`, not the `FriendDecl`. Therefore, I have replaced the matcher with `hasAncestor()`.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D114299

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/readability-redundant-declaration.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
index dfe3fbc96bf1b..78ba9691c2557 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
@@ -37,7 +37,7 @@ void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) {
                       functionDecl(unless(anyOf(
                           isDefinition(), isDefaulted(),
                           doesDeclarationForceExternallyVisibleDefinition(),
-                          hasParent(friendDecl()))))))
+                          hasAncestor(friendDecl()))))))
           .bind("Decl"),
       this);
 }

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-declaration.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-declaration.cpp
index 90fb98a882983..85ee9a52dfb7e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-declaration.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-declaration.cpp
@@ -70,6 +70,32 @@ struct Friendly {
 
 void enemy();
 
+template <typename>
+struct TemplateFriendly {
+  template <typename T>
+  friend void generic_friend();
+};
+
+template <typename T>
+void generic_friend() {}
+
+TemplateFriendly<int> template_friendly;
+
+template <typename>
+struct TemplateFriendly2 {
+  template <typename T>
+  friend void generic_friend2() {}
+};
+
+template <typename T>
+void generic_friend2();
+
+void generic_friend_caller() {
+  TemplateFriendly2<int> f;
+  generic_friend2<int>();
+}
+
+
 namespace macros {
 #define DECLARE(x) extern int x
 #define DEFINE(x) extern int x; int x = 42


        


More information about the cfe-commits mailing list