Skip to content

Commit 04ca1b6

Browse files
authored
[clang-tidy]fix misc-unused-using-decls false positive false for using 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
1 parent 2abf997 commit 04ca1b6

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "UnusedUsingDeclsCheck.h"
1010
#include "clang/AST/ASTContext.h"
11+
#include "clang/AST/Decl.h"
1112
#include "clang/ASTMatchers/ASTMatchFinder.h"
1213
#include "clang/Lex/Lexer.h"
1314

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

153+
if (const auto *T = Result.Nodes.getNodeAs<Type>("usedType")) {
154+
if (const auto *ND = T->getAsTagDecl())
155+
RemoveNamedDecl(ND);
156+
return;
157+
}
158+
148159
if (const auto *UsedShadow =
149160
Result.Nodes.getNodeAs<UsingShadowDecl>("usedShadow")) {
150161
removeFromFoundDecls(UsedShadow->getTargetDecl());

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ Changes in existing checks
291291
<clang-tidy/checks/misc/redundant-expression>` check to ignore
292292
false-positives in unevaluated context (e.g., ``decltype``).
293293

294+
- Improved :doc:`misc-unused-using-decls
295+
<clang-tidy/checks/misc/unused-using-decls>` check to avoid false positive when
296+
using in elaborated type.
297+
294298
- Improved :doc:`modernize-avoid-bind
295299
<clang-tidy/checks/modernize/avoid-bind>` check to
296300
not emit a ``return`` for fixes when the function returns ``void``.

clang-tools-extra/test/clang-tidy/checkers/misc/unused-using-decls.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,12 @@ template <typename T, template <typename> class U> class Bar {};
213213
// We used to report Q unsued, because we only checked the first template
214214
// argument.
215215
Bar<int, Q> *bar;
216+
217+
namespace gh69714 {
218+
struct StructGH69714_1 {};
219+
struct StructGH69714_2 {};
220+
} // namespace gh69714
221+
using gh69714::StructGH69714_1;
222+
using gh69714::StructGH69714_2;
223+
struct StructGH69714_1 a;
224+
struct StructGH69714_2 *b;

0 commit comments

Comments
 (0)