[clang-tools-extra] [clang-tidy] Avoid expensive AST traversal in RedundantTypenameCheck (PR #170540)

Victor Chernyakin via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 4 23:41:29 PST 2025


================
@@ -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(),
+                                    deducedTemplateSpecializationType(),
+                                    templateSpecializationType()))),
+              unless(hasAncestor(decl(isInstantiated()))))
+          .bind("nonDependentTypeLoc"),
+      this);
----------------
localspook wrote:

Ah, bingo. There are a bunch of `addMatcher` overloads. The ones for `DeclarationMatcher` and `StatementMatcher` respect `getCheckTraversalKind`:
https://github.com/llvm/llvm-project/blob/722026886fb65f0d61e0384886ffc30e83623edf/clang/lib/ASTMatchers/ASTMatchFinder.cpp#L1670-L1680
And the rest (including the one we're calling) just... don't?!
https://github.com/llvm/llvm-project/blob/722026886fb65f0d61e0384886ffc30e83623edf/clang/lib/ASTMatchers/ASTMatchFinder.cpp#L1712-L1716

https://github.com/llvm/llvm-project/pull/170540


More information about the cfe-commits mailing list