[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
This commit is contained in:
Fabian Wolff 2022-01-17 20:50:32 +01:00
parent 523573e90d
commit 42bc3275d3
2 changed files with 27 additions and 1 deletions

View File

@ -37,7 +37,7 @@ void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) {
functionDecl(unless(anyOf(
isDefinition(), isDefaulted(),
doesDeclarationForceExternallyVisibleDefinition(),
hasParent(friendDecl()))))))
hasAncestor(friendDecl()))))))
.bind("Decl"),
this);
}

View 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