Skip to content

Commit d1cbed0

Browse files
committed
[clang-tidy] Improved modernize-use-using by fixing a false-negative
The check needs a parent decl to match but if the typedef is in a function, the parent is a declStmt which is not a decl by itself. Improved the matcher to match on either a decl or a declstmt and extract the decl from the stmt in the latter case. fixes #72179
1 parent c67a4ae commit d1cbed0

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static constexpr llvm::StringLiteral ExternCDeclName = "extern-c-decl";
2424
static constexpr llvm::StringLiteral ParentDeclName = "parent-decl";
2525
static constexpr llvm::StringLiteral TagDeclName = "tag-decl";
2626
static constexpr llvm::StringLiteral TypedefName = "typedef";
27+
static constexpr llvm::StringLiteral DeclStmtName = "decl-stmt";
2728

2829
UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
2930
: ClangTidyCheck(Name, Context),
@@ -41,7 +42,8 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
4142
unless(isInstantiated()),
4243
optionally(hasAncestor(
4344
linkageSpecDecl(isExternCLinkage()).bind(ExternCDeclName))),
44-
hasParent(decl().bind(ParentDeclName)))
45+
anyOf(hasParent(decl().bind(ParentDeclName)),
46+
hasParent(declStmt().bind(DeclStmtName))))
4547
.bind(TypedefName),
4648
this);
4749

@@ -51,17 +53,25 @@ void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
5153
tagDecl(
5254
anyOf(allOf(unless(anyOf(isImplicit(),
5355
classTemplateSpecializationDecl())),
54-
hasParent(decl().bind(ParentDeclName))),
56+
anyOf(hasParent(decl().bind(ParentDeclName)),
57+
hasParent(declStmt().bind(DeclStmtName)))),
5558
// We want the parent of the ClassTemplateDecl, not the parent
5659
// of the specialization.
5760
classTemplateSpecializationDecl(hasAncestor(classTemplateDecl(
58-
hasParent(decl().bind(ParentDeclName)))))))
61+
anyOf(hasParent(decl().bind(ParentDeclName)),
62+
hasParent(declStmt().bind(DeclStmtName))))))))
5963
.bind(TagDeclName),
6064
this);
6165
}
6266

6367
void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
6468
const auto *ParentDecl = Result.Nodes.getNodeAs<Decl>(ParentDeclName);
69+
70+
if (!ParentDecl) {
71+
const auto *ParentDeclStmt = Result.Nodes.getNodeAs<DeclStmt>(DeclStmtName);
72+
ParentDecl = ParentDeclStmt->getSingleDecl();
73+
}
74+
6575
if (!ParentDecl)
6676
return;
6777

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ Changes in existing checks
183183
<clang-tidy/checks/modernize/use-override>` check to also remove any trailing
184184
whitespace when deleting the ``virtual`` keyword.
185185

186+
- Improved :doc:`modernize-use-using <clang-tidy/checks/modernize/use-using>`
187+
check by fixing false-negative in functions.
188+
186189
- Improved :doc:`readability-implicit-bool-conversion
187190
<clang-tidy/checks/readability/implicit-bool-conversion>` check to provide
188191
valid fix suggestions for ``static_cast`` without a preceding space and

clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,21 @@ typedef int InExternCPP;
342342
// CHECK-FIXES: using InExternCPP = int;
343343

344344
}
345+
346+
namespace ISSUE_72179
347+
{
348+
void foo()
349+
{
350+
typedef int a;
351+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 'typedef' [modernize-use-using]
352+
// CHECK-FIXES: using a = int;
353+
354+
}
355+
356+
void foo2()
357+
{
358+
typedef struct { int a; union { int b; }; } c;
359+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use 'using' instead of 'typedef' [modernize-use-using]
360+
// CHECK-FIXES: using c = struct { int a; union { int b; }; };
361+
}
362+
}

0 commit comments

Comments
 (0)