[clang-tools-extra] [clang-tidy] Correct fix-it range for function pointer-like typedef in `modernize-use-using` (PR #173751)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 27 18:22:33 PST 2025
https://github.com/flovent created https://github.com/llvm/llvm-project/pull/173751
Extends the function case to all function pointer-like cases, because their source range contains name.
see more in https://github.com/llvm/llvm-project/pull/65558
Closes [#173732](https://github.com/llvm/llvm-project/issues/173732)
>From 99f046d4bca5ac39a33245514f90f52fb0837e04 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sun, 28 Dec 2025 10:17:19 +0800
Subject: [PATCH] [clang-tidy] Correct fix-it range for function pointer-like
typedef in `modernize-use-using`
---
.../clang-tidy/modernize/UseUsingCheck.cpp | 8 +++++---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
.../checkers/modernize/use-using.cpp | 19 +++++++++++++++++++
3 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index 4831963326c8d..085dbde60db61 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -130,13 +130,16 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
const TypeLoc TL = MatchedDecl->getTypeSourceInfo()->getTypeLoc();
- auto [Type, QualifierStr] = [MatchedDecl, this, &TL, &SM,
+ bool FunctionPointerCase = false;
+ auto [Type, QualifierStr] = [MatchedDecl, this, &TL, &FunctionPointerCase,
+ &SM,
&LO]() -> std::pair<std::string, std::string> {
SourceRange TypeRange = TL.getSourceRange();
// Function pointer case, get the left and right side of the identifier
// without the identifier.
if (TypeRange.fullyContains(MatchedDecl->getLocation())) {
+ FunctionPointerCase = true;
const auto RangeLeftOfIdentifier = CharSourceRange::getCharRange(
TypeRange.getBegin(), MatchedDecl->getLocation());
const auto RangeRightOfIdentifier = CharSourceRange::getCharRange(
@@ -205,8 +208,7 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
}
if (!ReplaceRange.getEnd().isMacroID()) {
- const SourceLocation::IntTy Offset =
- MatchedDecl->getFunctionType() ? 0 : Name.size();
+ const SourceLocation::IntTy Offset = FunctionPointerCase ? 0 : Name.size();
LastReplacementEnd = ReplaceRange.getEnd().getLocWithOffset(Offset);
}
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 86bfd1d489898..ce16f94816aa3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -583,6 +583,10 @@ Changes in existing checks
constructor call, and fixed a crash when handling format strings
containing non-ASCII characters.
+- Improved :doc:`modernize-use-using
+ <clang-tidy/checks/modernize/use-using>` check to correctly provide fix-its
+ for typedefs of pointers or references to array types.
+
- Improved :doc:`performance-unnecessary-copy-initialization
<clang-tidy/checks/performance/unnecessary-copy-initialization>` by printing
the type of the diagnosed variable.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 5b8eca2825645..4365c8d05d710 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -437,3 +437,22 @@ namespace GH97009 {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
// CHECK-FIXES: using Function = bool (*)(PointType, PointType);
}
+
+namespace GH173732 {
+ // reference to array
+ typedef char (&refarray)[2];
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
+ // CHECK-FIXES: using refarray = char (&)[2];
+ typedef char &ref;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
+ // CHECK-FIXES: using ref = char &;
+
+
+ // pointer to array
+ typedef char (*ptrarray)[2];
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use
+ // CHECK-FIXES: using ptrarray = char (*)[2];
+ typedef char *ptr;
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use
+ // CHECK-FIXES: using ptr = char *;
+}
More information about the cfe-commits
mailing list