[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 Nov 28 10:20:48 PST 2026
https://github.com/ZenginU updated https://github.com/llvm/llvm-project/pull/226739
>From 9e3b2d83e1f87706e0e6c3ad51248b3dd0cc53d6 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/7] 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 67db20e15934f0..3b847e81d98bfa 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"
@@ -303,13 +304,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 958b47c1c1ee12..c0e0a01fe8e52c 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 5e7bad70f40266503d25bcb85f6485c0f8ca015b 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/7] 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 7f3eab9f64e3e1..4080a44ea75466 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -362,6 +362,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 0b979a94805d34916b1c694d4b0a4bf9830e32bb 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/7] 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 4080a44ea75466..fc4447463f88d2 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -362,10 +362,6 @@ 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>`.
@@ -386,6 +382,10 @@ infrastructure are described first, followed by tool-specific sections.
- Kept diagnostics but suppressed unsafe fix-its when no safe
result-preserving rewrite is available.
+- Fixed a bug in {doc}`modernize-use-ranges
+ <clang-tidy/checks/modernize/use-ranges>` that produced incorrect fix-its
+ for raw pointers to containers.
+
- 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
>From 3ab923773307641b77b5171283e4f158a9bab9e2 Mon Sep 17 00:00:00 2001
From: ZenginU <umutzengin00 at gmail.com>
Date: Sun, 27 Sep 2026 11:33:04 -0400
Subject: [PATCH 4/7] Use tooling::fixit::getText
---
clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
index 3b847e81d98bfa..c8b16674d743ce 100644
--- a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
@@ -20,6 +20,7 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
+#include "clang/Tooling/FixIt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
@@ -307,11 +308,7 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) {
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())
- .str();
+ tooling::fixit::getText(*ArgExpr, *Result.Context).str();
if (ArgExpr->getType()->isPointerType() &&
isa<CXXMemberCallExpr>(RangeArg->IgnoreParenImpCasts()))
ReplaceText = fixit::formatDereference(*ArgExpr, *Result.Context);
>From c2596be03eb781d7c942544646368d3ef43786c2 Mon Sep 17 00:00:00 2001
From: ZenginU <umutzengin00 at gmail.com>
Date: Sun, 27 Sep 2026 14:08:15 -0400
Subject: [PATCH 5/7] Add tests and use ternary operator for dereferencing.
---
clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp | 10 ++++++----
.../test/clang-tidy/checkers/modernize/use-ranges.cpp | 5 +++++
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
index c8b16674d743ce..c181975d3e6e6e 100644
--- a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
@@ -307,11 +307,13 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *ArgExpr = Result.Nodes.getNodeAs<Expr>(ArgNode)) {
const Expr *RangeArg =
Call->getArg(Replace == Indexes::Second ? Second : First);
+ const bool NeedsDereference =
+ ArgExpr->getType()->isPointerType() &&
+ isa<CXXMemberCallExpr>(RangeArg->IgnoreParenImpCasts());
std::string ReplaceText =
- tooling::fixit::getText(*ArgExpr, *Result.Context).str();
- if (ArgExpr->getType()->isPointerType() &&
- isa<CXXMemberCallExpr>(RangeArg->IgnoreParenImpCasts()))
- ReplaceText = fixit::formatDereference(*ArgExpr, *Result.Context);
+ NeedsDereference
+ ? fixit::formatDereference(*ArgExpr, *Result.Context)
+ : tooling::fixit::getText(*ArgExpr, *Result.Context).str();
Diag << FixItHint::CreateReplacement(RangeArg->getSourceRange(),
ReplaceText);
} else {
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 c0e0a01fe8e52c..b48a6c9254a15d 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
@@ -13,6 +13,7 @@ void Positives() {
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);
@@ -36,6 +37,10 @@ void Positives() {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
// CHECK-FIXES: std::ranges::find(*P, 3);
+ std::find((P)->begin(), (P)->end(), 4);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+ // CHECK-FIXES: std::ranges::find(*P, 4);
+
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);
>From 8ddc5c65af3824d6a700b7d1c95783f4611e038f Mon Sep 17 00:00:00 2001
From: ZenginU <umutzengin00 at gmail.com>
Date: Sun, 27 Sep 2026 17:14:14 -0400
Subject: [PATCH 6/7] Fix clang-tidy
---
clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
index c181975d3e6e6e..3c6b022d80aed9 100644
--- a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
@@ -310,7 +310,7 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) {
const bool NeedsDereference =
ArgExpr->getType()->isPointerType() &&
isa<CXXMemberCallExpr>(RangeArg->IgnoreParenImpCasts());
- std::string ReplaceText =
+ const std::string ReplaceText =
NeedsDereference
? fixit::formatDereference(*ArgExpr, *Result.Context)
: tooling::fixit::getText(*ArgExpr, *Result.Context).str();
>From 2210e4f97f3a4ac2c7816c54f96bccc572b5c138 Mon Sep 17 00:00:00 2001
From: ZenginU <umutzengin00 at gmail.com>
Date: Sat, 28 Nov 2026 13:02:58 -0500
Subject: [PATCH 7/7] Handle array decay in use-ranges fix-its
---
clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp | 3 ++-
.../test/clang-tidy/checkers/modernize/use-ranges.cpp | 8 ++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
index 3c6b022d80aed9..696fa8aec84b22 100644
--- a/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
@@ -308,7 +308,8 @@ void UseRangesCheck::check(const MatchFinder::MatchResult &Result) {
const Expr *RangeArg =
Call->getArg(Replace == Indexes::Second ? Second : First);
const bool NeedsDereference =
- ArgExpr->getType()->isPointerType() &&
+ (ArgExpr->getType()->isPointerType() ||
+ ArgExpr->getType()->isArrayType()) &&
isa<CXXMemberCallExpr>(RangeArg->IgnoreParenImpCasts());
const std::string ReplaceText =
NeedsDereference
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 b48a6c9254a15d..c2adc52aebf114 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
@@ -45,10 +45,18 @@ void Positives() {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
// CHECK-FIXES: std::ranges::find(*(P + 1), 4);
+ std::find(PointerContainers->begin(), PointerContainers->end(), 5);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+ // CHECK-FIXES: std::ranges::find(*PointerContainers, 5);
+
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);
+ std::find(std::begin(*P), std::end(*P), 2);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
+ // CHECK-FIXES: std::ranges::find(*P, 2);
+
std::find(std::cbegin(I), std::cend(I), 3);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
// CHECK-FIXES: std::ranges::find(I, 3);
More information about the cfe-commits
mailing list