Index: clang-tidy/misc/UnusedUsingDeclsCheck.cpp =================================================================== --- clang-tidy/misc/UnusedUsingDeclsCheck.cpp +++ clang-tidy/misc/UnusedUsingDeclsCheck.cpp @@ -27,7 +27,6 @@ isa(TargetDecl) || isa(TargetDecl) || isa(TargetDecl); } - void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this); auto DeclMatcher = hasDeclaration(namedDecl().bind("used")); @@ -37,6 +36,11 @@ Finder->addMatcher(declRefExpr().bind("used"), this); Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind("used"))), this); + Finder->addMatcher( + callExpr(hasDeclaration(functionDecl(hasAnyTemplateArgument( + anyOf(refersToTemplate(templateName().bind("used")), + refersToDeclaration(functionDecl().bind("used"))))))), + this); } void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) { @@ -71,20 +75,27 @@ Contexts.push_back(Context); return; } - // Mark using declarations as used by setting FoundDecls' value to zero. As // the AST is walked in order, usages are only marked after a the // corresponding using declaration has been found. // FIXME: This currently doesn't look at whether the type reference is // actually found with the help of the using declaration. if (const auto *Used = Result.Nodes.getNodeAs("used")) { - if (const auto *Specialization = - dyn_cast(Used)) + if (const auto *FD = dyn_cast(Used)) { + removeFromFoundDecls(FD->getPrimaryTemplate()); + } else if (const auto *Specialization = + dyn_cast(Used)) { Used = Specialization->getSpecializedTemplate(); + } removeFromFoundDecls(Used); return; } + if (const auto *Used = Result.Nodes.getNodeAs("used")) { + removeFromFoundDecls(Used->getAsTemplateDecl()); + return; + } + if (const auto *DRE = Result.Nodes.getNodeAs("used")) { if (const auto *FD = dyn_cast(DRE->getDecl())) { if (const auto *FDT = FD->getPrimaryTemplate()) @@ -109,6 +120,8 @@ } void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) { + if (!D) + return; // FIXME: Currently, we don't handle the using-decls being used in different // scopes (such as different namespaces, different functions). Instead of // giving an incorrect message, we mark all of them as used. Index: test/clang-tidy/Inputs/unused-using-decls.h =================================================================== --- /dev/null +++ test/clang-tidy/Inputs/unused-using-decls.h @@ -0,0 +1,11 @@ +class MyClass { +public: + template