clang-tidy: [misc-unused-using-decls] Support template types.

This fixes llvm.org/PR27429.

llvm-svn: 266866
This commit is contained in:
Daniel Jasper 2016-04-20 09:48:56 +00:00
parent fb5c307ccd
commit 25e17df663
2 changed files with 14 additions and 3 deletions

View File

@ -20,8 +20,9 @@ namespace misc {
void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
Finder->addMatcher(recordType(hasDeclaration(namedDecl().bind("used"))),
this);
auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
Finder->addMatcher(loc(recordType(DeclMatcher)), this);
Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this);
}
void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
@ -34,7 +35,7 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
Using->shadow_begin()->getTargetDecl()->getCanonicalDecl();
// FIXME: Handle other target types.
if (!isa<RecordDecl>(TargetDecl))
if (!isa<RecordDecl>(TargetDecl) && !isa<ClassTemplateDecl>(TargetDecl))
return;
FoundDecls[TargetDecl] = Using;
@ -53,6 +54,9 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
// 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<NamedDecl>("used")) {
if (const auto *Specialization =
dyn_cast<ClassTemplateSpecializationDecl>(Used))
Used = Specialization->getSpecializedTemplate();
auto I = FoundDecls.find(Used->getCanonicalDecl());
if (I != FoundDecls.end())
I->second = nullptr;

View File

@ -8,6 +8,8 @@ class B;
class C;
class D;
class D { public: static int i; };
template <typename T> class E {};
template <typename T> class F {};
}
// ----- Using declarations -----
@ -18,11 +20,16 @@ using n::A; // A
using n::B;
using n::C;
using n::D;
using n::E; // E
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: using decl 'E' is unused
// CHECK-FIXES: {{^}}// E
using n::F;
// ----- Usages -----
void f(B b);
void g() {
vector<C> data;
D::i = 1;
F<int> f;
}