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

Fabian Wolff via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 19 16:32:19 PST 2021


fwolff created this revision.
fwolff added reviewers: Eugene.Zelenko, alexfh, Szelethus.
fwolff added a project: clang-tools-extra.
Herald added subscribers: carlosgalvezp, xazax.hun.
fwolff requested review of this revision.
Herald added a subscriber: cfe-commits.

Fixes PR#48086 <https://bugs.llvm.org/show_bug.cgi?id=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()`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114299

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


Index: clang-tools-extra/test/clang-tidy/checkers/readability-redundant-declaration.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-redundant-declaration.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-redundant-declaration.cpp
@@ -66,8 +66,14 @@
 struct Friendly {
   friend void best_friend();
   friend void enemy();
+
+  template <typename T>
+  friend void generic_friend();
 };
 
+template <typename T>
+void generic_friend() {}
+
 void enemy();
 
 namespace macros {
Index: clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
@@ -37,7 +37,7 @@
                       functionDecl(unless(anyOf(
                           isDefinition(), isDefaulted(),
                           doesDeclarationForceExternallyVisibleDefinition(),
-                          hasParent(friendDecl()))))))
+                          hasAncestor(friendDecl()))))))
           .bind("Decl"),
       this);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114299.388663.patch
Type: text/x-patch
Size: 1229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211120/7e045f7b/attachment.bin>


More information about the cfe-commits mailing list