[clang-tidy] Fix `readability-redundant-declaration` false positive for template...
authorFabian Wolff <fabian.wolff@alumni.ethz.ch>
Mon, 17 Jan 2022 19:50:32 +0000 (20:50 +0100)
committerFabian Wolff <fabian.wolff@alumni.ethz.ch>
Mon, 17 Jan 2022 19:50:32 +0000 (20:50 +0100)
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

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

index dfe3fbc..78ba969 100644 (file)
@@ -37,7 +37,7 @@ void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) {
                       functionDecl(unless(anyOf(
                           isDefinition(), isDefaulted(),
                           doesDeclarationForceExternallyVisibleDefinition(),
-                          hasParent(friendDecl()))))))
+                          hasAncestor(friendDecl()))))))
           .bind("Decl"),
       this);
 }
index 90fb98a..85ee9a5 100644 (file)
@@ -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