Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ using namespace clang::ast_matchers;
namespace clang::tidy::readability {

void RedundantTypenameCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(typeLoc(unless(hasAncestor(decl(isInstantiated()))))
.bind("nonDependentTypeLoc"),
this);
Finder->addMatcher(
typeLoc(loc(TypeMatcher(anyOf(typedefType(), tagType(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove TypeMatcher?

-typeLoc(loc(TypeMatcher(anyOf(...))))
+typeLoc(loc(anyOf(...)))

deducedTemplateSpecializationType(),
templateSpecializationType()))),
unless(hasAncestor(decl(isInstantiated()))))
.bind("nonDependentTypeLoc"),
this);
Comment on lines +21 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we skip the hasAncestor part (I'd keep the anyOf you added anyways since we are producing less matches that way, which also improves perf)

typeLoc(loc(qualType(unless(dependentNameType())))

?

Then we are not checking a type that is dependent, which eliminates the need for the hasAncestor and also the

      if (NonDependentTypeLoc->getType()->isDependentType())
        return SourceLocation();

later

Copy link
Contributor

@zwuis zwuis Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we can remove unless(...). The traverse mode of this checker is TK_IgnoreUnlessSpelledInSource, so decl(isInstantiated()) doesn't matches anything IIUC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are great suggestions. Thanks 😃

I’ll try to clean those up and see how our test case works after.

Copy link
Contributor

@localspook localspook Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we can remove unless(...). The traverse mode of this checker is TK_IgnoreUnlessSpelledInSource, so decl(isInstantiated()) doesn't matches anything IIUC.

I originally added the unless to make this test case pass:

template <typename T>
void n(typename T::R *) {}
template void n<NotDependent>(NotDependent::R *);

Without the unless, we get a false positive; the traversal seems to descend into the instantiation despite the traversal mode being TK_IgnoreUnlessSpelledInSource. Experimenting just now, I tried setting the traversal mode on the matcher explicitly:

  Finder->addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, typeLoc()
                         .bind("nonDependentTypeLoc")),
                     this);

And now it does work! Is there maybe an issue causing the traversal mode to not get propagated?

Copy link
Contributor

@localspook localspook Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, bingo. There are a bunch of addMatcher overloads. The ones for DeclarationMatcher and StatementMatcher respect getCheckTraversalKind:

void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,
MatchCallback *Action) {
std::optional<TraversalKind> TK;
if (Action)
TK = Action->getCheckTraversalKind();
if (TK)
Matchers.DeclOrStmt.emplace_back(traverse(*TK, NodeMatch), Action);
else
Matchers.DeclOrStmt.emplace_back(NodeMatch, Action);
Matchers.AllCallbacks.insert(Action);
}

And all the rest (including the one we're calling) just... don't?!
void MatchFinder::addMatcher(const TypeLocMatcher &NodeMatch,
MatchCallback *Action) {
Matchers.TypeLoc.emplace_back(NodeMatch, Action);
Matchers.AllCallbacks.insert(Action);
}


if (!getLangOpts().CPlusPlus20)
return;
Expand Down