[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:18:05 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:
> 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:
https://github.com/llvm/llvm-project/blob/5911754a3085e1c95cec12971f075ab3f024d71d/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-typename.cpp#L167-L170
Without the `unless`, it fails; 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:
```cpp
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?
https://github.com/llvm/llvm-project/pull/170540
More information about the cfe-commits
mailing list