[clang-tools-extra] [clang-tidy] Fix modernize-use-using wrongly modifies typedef of parenthesized functions (PR #176565)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 17 04:36:50 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: mitchell (zeyi2)
<details>
<summary>Changes</summary>
Closes [#<!-- -->176267](https://github.com/llvm/llvm-project/issues/176267)
---
Full diff: https://github.com/llvm/llvm-project/pull/176565.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp (+17-3)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp (+27-1)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index 085dbde60db61..d6f422b428c31 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -140,10 +140,24 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
// without the identifier.
if (TypeRange.fullyContains(MatchedDecl->getLocation())) {
FunctionPointerCase = true;
- const auto RangeLeftOfIdentifier = CharSourceRange::getCharRange(
- TypeRange.getBegin(), MatchedDecl->getLocation());
+ SourceLocation StartLoc = MatchedDecl->getLocation();
+ SourceLocation EndLoc = MatchedDecl->getLocation();
+
+ while (true) {
+ Token Prev = utils::lexer::getPreviousToken(StartLoc, SM, LO);
+ const std::optional<Token> Next =
+ utils::lexer::findNextTokenSkippingComments(EndLoc, SM, LO);
+ if (Prev.isNot(tok::l_paren) || !Next || Next->isNot(tok::r_paren))
+ break;
+
+ StartLoc = Prev.getLocation();
+ EndLoc = Next->getLocation();
+ }
+
+ const auto RangeLeftOfIdentifier =
+ CharSourceRange::getCharRange(TypeRange.getBegin(), StartLoc);
const auto RangeRightOfIdentifier = CharSourceRange::getCharRange(
- Lexer::getLocForEndOfToken(MatchedDecl->getLocation(), 0, SM, LO),
+ Lexer::getLocForEndOfToken(EndLoc, 0, SM, LO),
Lexer::getLocForEndOfToken(TypeRange.getEnd(), 0, SM, LO));
const std::string VerbatimType =
(Lexer::getSourceText(RangeLeftOfIdentifier, SM, LO) +
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 94a11b1acb73a..c3b255d461e01 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -109,6 +109,10 @@ Changes in existing checks
- Added support for analyzing function parameters with the `AnalyzeParameters`
option.
+- Improved :doc:`modernize-use-using
+ <clang-tidy/checks/modernize/use-using>` check by avoiding the generation
+ of invalid code for function types with redundant parentheses.
+
- Improved :doc:`performance-move-const-arg
<clang-tidy/checks/performance/move-const-arg>` check by avoiding false
positives on trivially copyable types with a non-public copy constructor.
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 a1f32b06df091..324616d274646 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
@@ -344,7 +344,7 @@ typedef int InExternCPP;
}
namespace ISSUE_72179
-{
+{
void foo()
{
typedef int a;
@@ -461,3 +461,29 @@ namespace GH173732 {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
// CHECK-MESSAGES: :[[@LINE-2]]:29: warning: use 'using' instead of 'typedef' [modernize-use-using]
}
+
+namespace GH176267 {
+ typedef int(f1)(double);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
+ // CHECK-FIXES: using f1 = int(double);
+
+ typedef int(f2)(double);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
+ // CHECK-FIXES: using f2 = int(double);
+
+ typedef int f3(double);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
+ // CHECK-FIXES: using f3 = int (double);
+
+ typedef int (*f4)(double);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
+ // CHECK-FIXES: using f4 = int (*)(double);
+
+ typedef int ((f5))(double);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
+ // CHECK-FIXES: using f5 = int (double);
+
+ typedef int ( /* comment */ f6 ) (double);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' [modernize-use-using]
+ // CHECK-FIXES: using f6 = int (double);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/176565
More information about the cfe-commits
mailing list