[clang-tools-extra] [clang-tidy] Fix when std::ranges::fix is offered on a pointer type in `modernize-use-ranges` (PR #226739)
Umut Zengin via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 26 20:15:40 PDT 2026
https://github.com/ZenginU updated https://github.com/llvm/llvm-project/pull/226739
>From 585393c47572efe2e5ebf047a4e7fd54ec8b76df Mon Sep 17 00:00:00 2001
From: ZenginU <umutzengin00 at gmail.com>
Date: Sat, 26 Sep 2026 18:33:36 -0400
Subject: [PATCH 1/3] Fix: Fix when std::ranges::fix is offered on a pointer
type.
---
.../clang-tidy/utils/UseRangesCheck.cpp | 15 +++++++++++----
.../clang-tidy/checkers/modernize/use-ranges.cpp | 15 ++++++++++++++-
2 files changed, 25 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
index e66e1e541e461e..6529c23ee5cb6a 100644
--- a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "UseRangesCheck.h"
+#include "FixItHintUtils.h"
#include "Matchers.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
@@ -268,13 +269,19 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) {
for (const auto &[First, Second, Replace] : Sig) {
auto ArgNode = ArgName + std::to_string(First);
if (const auto *ArgExpr = Result.Nodes.getNodeAs<Expr>(ArgNode)) {
- Diag << FixItHint::CreateReplacement(
- Call->getArg(Replace == Indexes::Second ? Second : First)
- ->getSourceRange(),
+ const Expr *RangeArg =
+ Call->getArg(Replace == Indexes::Second ? Second : First);
+ std::string ReplaceText =
Lexer::getSourceText(
CharSourceRange::getTokenRange(ArgExpr->getSourceRange()),
Result.Context->getSourceManager(),
- Result.Context->getLangOpts()));
+ Result.Context->getLangOpts())
+ .str();
+ if (ArgExpr->getType()->isPointerType() &&
+ isa<CXXMemberCallExpr>(RangeArg->IgnoreParenImpCasts()))
+ ReplaceText = fixit::formatDereference(*ArgExpr, *Result.Context);
+ Diag << FixItHint::CreateReplacement(RangeArg->getSourceRange(),
+ ReplaceText);
} else {
assert(ReverseDescriptor && "Couldn't find forward argument");
ArgNode.push_back('R');
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
index 1724b9311e57ea..e98234191c15e7 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-ranges.cpp
@@ -11,7 +11,8 @@
void Positives() {
std::vector<int> I, J;
std::vector<std::unique_ptr<int>> K;
-
+ std::vector<int> PointerContainers[2];
+ std::vector<int> *P = PointerContainers;
// Expect to have no check messages
std::find(K.begin(), K.end(), nullptr);
@@ -27,6 +28,18 @@ void Positives() {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
// CHECK-FIXES: std::ranges::find(I, 1);
+ std::find(P->cbegin(), P->cend(), 2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+ // CHECK-FIXES: std::ranges::find(*P, 2);
+
+ std::find(P->begin(), P->end(), 3);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+ // CHECK-FIXES: std::ranges::find(*P, 3);
+
+ std::find((P + 1)->begin(), (P + 1)->end(), 4);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+ // CHECK-FIXES: std::ranges::find(*(P + 1), 4);
+
std::find(std::begin(I), std::end(I), 2);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
// CHECK-FIXES: std::ranges::find(I, 2);
>From 3eb10646f05e06a639b73d41fc92865a004f0bd3 Mon Sep 17 00:00:00 2001
From: ZenginU <umutzengin00 at gmail.com>
Date: Sat, 26 Sep 2026 23:01:15 -0400
Subject: [PATCH 2/3] Add the fix to the ReleaseNotes.md
---
clang-tools-extra/docs/ReleaseNotes.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 833638a47abc63..52b201dc11d4fc 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -240,6 +240,10 @@ infrastructure are described first, followed by tool-specific sections.
<clang-tidy/checks/modernize/use-noexcept>` when analyzing malformed template
code with an unparsed exception specification.
+- Improved {doc}`modernize-use-ranges
+ <clang-tidy/checks/modernize/use-ranges>` check by correctly dereferencing
+ raw pointers to containers in fix-its.
+
- Extend {doc}`modernize-use-nullptr
<clang-tidy/checks/modernize/use-nullptr>` to turn `decltype(nullptr)` into
`std::nullptr_t` from `<cstdef>`.
>From 402a1245559e873af7b24579cadf118a98f1c06a Mon Sep 17 00:00:00 2001
From: ZenginU <umutzengin00 at gmail.com>
Date: Sat, 26 Sep 2026 23:14:17 -0400
Subject: [PATCH 3/3] Fix ordering.
---
clang-tools-extra/docs/ReleaseNotes.md | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 52b201dc11d4fc..ac6f6b8fe6865e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -240,14 +240,14 @@ infrastructure are described first, followed by tool-specific sections.
<clang-tidy/checks/modernize/use-noexcept>` when analyzing malformed template
code with an unparsed exception specification.
-- Improved {doc}`modernize-use-ranges
- <clang-tidy/checks/modernize/use-ranges>` check by correctly dereferencing
- raw pointers to containers in fix-its.
-
- Extend {doc}`modernize-use-nullptr
<clang-tidy/checks/modernize/use-nullptr>` to turn `decltype(nullptr)` into
`std::nullptr_t` from `<cstdef>`.
+- Improved {doc}`modernize-use-ranges
+ <clang-tidy/checks/modernize/use-ranges>` check by correctly dereferencing
+ raw pointers to containers in fix-its.
+
- Improved {doc}`performance-inefficient-algorithm
<clang-tidy/checks/performance/inefficient-algorithm>` check to no longer
produce a fix with the container or the searched-for value missing, such as
More information about the cfe-commits
mailing list