21 Finder->addMatcher(typeLoc(unless(hasAncestor(decl(isInstantiated()))))
22 .bind(
"nonDependentTypeLoc"),
25 if (!getLangOpts().CPlusPlus20)
28 const auto InImplicitTypenameContext = anyOf(
30 typedefNameDecl(), templateTypeParmDecl(), nonTypeTemplateParmDecl(),
31 friendDecl(), fieldDecl(),
32 varDecl(hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())),
33 unless(parmVarDecl())),
34 parmVarDecl(hasParent(expr(requiresExpr()))),
35 parmVarDecl(hasParent(typeLoc(hasParent(decl(
36 anyOf(cxxMethodDecl(), hasParent(friendDecl()),
37 functionDecl(has(nestedNameSpecifier())),
38 cxxDeductionGuideDecl(hasDeclContext(recordDecl())))))))),
40 functionDecl(unless(cxxConversionDecl()))))),
41 hasParent(expr(anyOf(cxxNamedCastExpr(), cxxNewExpr()))));
43 typeLoc(InImplicitTypenameContext).bind(
"dependentTypeLoc"),
this);
47 const SourceLocation ElaboratedKeywordLoc = [&] {
48 if (
const auto *NonDependentTypeLoc =
49 Result.Nodes.getNodeAs<TypeLoc>(
"nonDependentTypeLoc")) {
50 if (
const auto TL = NonDependentTypeLoc->getAs<TypedefTypeLoc>())
51 return TL.getElaboratedKeywordLoc();
53 if (
const auto TL = NonDependentTypeLoc->getAs<TagTypeLoc>())
54 return TL.getElaboratedKeywordLoc();
56 if (
const auto TL = NonDependentTypeLoc
57 ->getAs<DeducedTemplateSpecializationTypeLoc>())
58 return TL.getElaboratedKeywordLoc();
61 NonDependentTypeLoc->getAs<TemplateSpecializationTypeLoc>())
62 if (!TL.getType()->isDependentType())
63 return TL.getElaboratedKeywordLoc();
65 TypeLoc InnermostTypeLoc =
66 *Result.Nodes.getNodeAs<TypeLoc>(
"dependentTypeLoc");
67 while (
const TypeLoc Next = InnermostTypeLoc.getNextTypeLoc())
68 InnermostTypeLoc = Next;
70 if (
const auto TL = InnermostTypeLoc.getAs<DependentNameTypeLoc>())
71 return TL.getElaboratedKeywordLoc();
74 InnermostTypeLoc.getAs<TemplateSpecializationTypeLoc>())
75 return TL.getElaboratedKeywordLoc();
78 return SourceLocation();
81 if (ElaboratedKeywordLoc.isInvalid())
84 if (Token ElaboratedKeyword;
85 Lexer::getRawToken(ElaboratedKeywordLoc, ElaboratedKeyword,
86 *Result.SourceManager, getLangOpts()) ||
87 ElaboratedKeyword.getRawIdentifier() !=
"typename")
90 diag(ElaboratedKeywordLoc,
"redundant 'typename'")
91 << FixItHint::CreateRemoval(ElaboratedKeywordLoc);