This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by fwolff on Nov 19 2021, 4:32 PM.

Details

Summary

Fixes 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().

Diff Detail

Event Timeline

fwolff created this revision.Nov 19 2021, 4:32 PM
fwolff requested review of this revision.Nov 19 2021, 4:32 PM

Hmm, the test case you added passes without the patch applied: https://godbolt.org/z/T9TerMYGz

I think the issue is that the class needs to be an instantiated template type: https://godbolt.org/z/bhznPdsvf, and a second (interesting but contrived) test case would be:

template <typename>
struct Friendly {
  template <typename T>
  friend void generic_friend() {}
};

template <typename T>
void generic_friend();

int main() {
  Friendly<int> f;
  generic_friend<int>();
}

(We do NOT want to diagnose the second declaration of generic_friend as being redundant -- it is necessary to make the definition visible.)

fwolff updated this revision to Diff 391163.Dec 1 2021, 4:29 PM

Hmm, the test case you added passes without the patch applied: https://godbolt.org/z/T9TerMYGz

You are right; I have fixed the test now, and I've also added your other example as a second test.

aaron.ballman accepted this revision.Dec 8 2021, 6:44 AM

I'm a bit worried about using hasAncestor() (that has a tendency to match in surprising nested locations), but I can't come up with a test case that would be an issue, so this LGTM.

This revision is now accepted and ready to land.Dec 8 2021, 6:44 AM