Skip to content

Commit

Permalink
[clang-tidy]fix misc-unused-using-decls false positive false for usin…
Browse files Browse the repository at this point in the history
…g in elaborated type (#70230)

`ElaboratedType` including tag keywords and any nested-name-specifiers.
We should ignore nested-name-specifiers case but consider tag keywords
case for `misc-unused-using-decls` check
Fixes: #69714
  • Loading branch information
HerrCai0907 committed Oct 26, 2023
1 parent 2abf997 commit 04ca1b6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "UnusedUsingDeclsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"

Expand Down Expand Up @@ -71,6 +72,10 @@ void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
templateArgument().bind("used")))),
this);
Finder->addMatcher(userDefinedLiteral().bind("used"), this);
Finder->addMatcher(
loc(elaboratedType(unless(hasQualifier(nestedNameSpecifier())),
hasUnqualifiedDesugaredType(type().bind("usedType")))),
this);
// Cases where we can identify the UsingShadowDecl directly, rather than
// just its target.
// FIXME: cover more cases in this way, as the AST supports it.
Expand Down Expand Up @@ -145,6 +150,12 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
return;
}

if (const auto *T = Result.Nodes.getNodeAs<Type>("usedType")) {
if (const auto *ND = T->getAsTagDecl())
RemoveNamedDecl(ND);
return;
}

if (const auto *UsedShadow =
Result.Nodes.getNodeAs<UsingShadowDecl>("usedShadow")) {
removeFromFoundDecls(UsedShadow->getTargetDecl());
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ Changes in existing checks
<clang-tidy/checks/misc/redundant-expression>` check to ignore
false-positives in unevaluated context (e.g., ``decltype``).

- Improved :doc:`misc-unused-using-decls
<clang-tidy/checks/misc/unused-using-decls>` check to avoid false positive when
using in elaborated type.

- Improved :doc:`modernize-avoid-bind
<clang-tidy/checks/modernize/avoid-bind>` check to
not emit a ``return`` for fixes when the function returns ``void``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,12 @@ template <typename T, template <typename> class U> class Bar {};
// We used to report Q unsued, because we only checked the first template
// argument.
Bar<int, Q> *bar;

namespace gh69714 {
struct StructGH69714_1 {};
struct StructGH69714_2 {};
} // namespace gh69714
using gh69714::StructGH69714_1;
using gh69714::StructGH69714_2;
struct StructGH69714_1 a;
struct StructGH69714_2 *b;

0 comments on commit 04ca1b6

Please sign in to comment.