[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 16:16:50 PDT 2026
https://github.com/ZenginU created https://github.com/llvm/llvm-project/pull/226739
First PR to LLVM, so will be tagging the reviewers.
@zeyi2 @vbvictor @localspook
Fixes the pointer derenferencing with the help of `fixit::formatDereference`. Most of the Diff is caused by `RangeArg`refactoring.
Did not use StringRef as `formatDereference` returns a `std::string`.
Assisted by Codex for codebase exploration and utility usage.
Fixes:
#188257
Godbolt link from original issuer:
https://godbolt.org/z/7af31ox3j
>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] 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 e66e1e541e461..6529c23ee5cb6 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 1724b9311e57e..e98234191c15e 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);
More information about the cfe-commits
mailing list