diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp index 219cc1f73ea..8a70e165c1a 100644 --- a/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp @@ -23,6 +23,7 @@ void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) { auto DeclMatcher = hasDeclaration(namedDecl().bind("used")); Finder->addMatcher(loc(recordType(DeclMatcher)), this); Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this); + Finder->addMatcher(declRefExpr().bind("used"), this); } void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) { @@ -34,8 +35,13 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) { const auto *TargetDecl = Using->shadow_begin()->getTargetDecl()->getCanonicalDecl(); - // FIXME: Handle other target types. - if (!isa<RecordDecl>(TargetDecl) && !isa<ClassTemplateDecl>(TargetDecl)) + // Ignores using-declarations defined in class definition. + if (isa<CXXRecordDecl>(TargetDecl->getDeclContext())) + return; + + if (!isa<RecordDecl>(TargetDecl) && !isa<ClassTemplateDecl>(TargetDecl) && + !isa<FunctionDecl>(TargetDecl) && !isa<VarDecl>(TargetDecl) && + !isa<FunctionTemplateDecl>(TargetDecl)) return; FoundDecls[TargetDecl] = Using; @@ -57,10 +63,26 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) { if (const auto *Specialization = dyn_cast<ClassTemplateSpecializationDecl>(Used)) Used = Specialization->getSpecializedTemplate(); - auto I = FoundDecls.find(Used->getCanonicalDecl()); - if (I != FoundDecls.end()) - I->second = nullptr; + removeFromFoundDecls(Used); + return; } + + if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>("used")) { + if (const auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) { + if (const auto *FDT = FD->getPrimaryTemplate()) + removeFromFoundDecls(FDT); + else + removeFromFoundDecls(FD); + } else if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) { + removeFromFoundDecls(VD); + } + } +} + +void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) { + auto I = FoundDecls.find(D->getCanonicalDecl()); + if (I != FoundDecls.end()) + I->second = nullptr; } void UnusedUsingDeclsCheck::onEndOfTranslationUnit() { |