[clang-tools-extra] [clang-tidy] Add llvm::accumulate to llvm-use-ranges (PR #177655)
Jakub Kuderski via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 24 03:08:52 PST 2026
https://github.com/kuhar updated https://github.com/llvm/llvm-project/pull/177655
>From a59ecdad923abee59bab0c869d0da7ee00e9491d Mon Sep 17 00:00:00 2001
From: Jakub Kuderski <jakub at nod-labs.com>
Date: Fri, 23 Jan 2026 14:04:06 -0500
Subject: [PATCH 1/2] [clang-tidy] Add llvm::accumulate to llvm-use-ranges
---
.../clang-tidy/llvm/UseRangesCheck.cpp | 40 +++++--------------
clang-tools-extra/docs/ReleaseNotes.rst | 3 +-
.../clang-tidy/checks/llvm/use-ranges.rst | 1 +
.../clang-tidy/checkers/llvm/use-ranges.cpp | 15 +++++++
4 files changed, 28 insertions(+), 31 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
index 70d8e75405e91..bd82915dc232d 100644
--- a/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
@@ -55,36 +55,16 @@ utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
};
// Single range algorithms.
- AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig),
- {"adjacent_find",
- "all_of",
- "any_of",
- "binary_search",
- "copy",
- "copy_if",
- "count",
- "count_if",
- "fill",
- "find",
- "find_if",
- "find_if_not",
- "for_each",
- "is_sorted",
- "lower_bound",
- "max_element",
- "min_element",
- "none_of",
- "partition",
- "partition_point",
- "remove_if",
- "replace",
- "replace_copy",
- "replace_copy_if",
- "stable_sort",
- "transform",
- "uninitialized_copy",
- "unique",
- "upper_bound"});
+ AddStdToLLVM(
+ llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig),
+ {"accumulate", "adjacent_find", "all_of", "any_of",
+ "binary_search", "copy", "copy_if", "count",
+ "count_if", "fill", "find", "find_if",
+ "find_if_not", "for_each", "is_sorted", "lower_bound",
+ "max_element", "min_element", "none_of", "partition",
+ "partition_point", "remove_if", "replace", "replace_copy",
+ "replace_copy_if", "stable_sort", "transform", "uninitialized_copy",
+ "unique", "upper_bound"});
// Two range algorithms.
AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(TwoSig),
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 1e5d4ef5dbb3b..5cfb6476dcc1f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -122,7 +122,8 @@ Changes in existing checks
- Improved :doc:`llvm-use-ranges
<clang-tidy/checks/llvm/use-ranges>` check by adding support for the following
- algorithms: ``std::replace_copy`` and ``std::replace_copy_if``.
+ algorithms: ``std::accumulate``, ``std::replace_copy``, and
+ ``std::replace_copy_if``.
- Improved :doc:`misc-const-correctness
<clang-tidy/checks/misc/const-correctness>` check:
diff --git a/clang-tools-extra/docs/clang-tidy/checks/llvm/use-ranges.rst b/clang-tools-extra/docs/clang-tidy/checks/llvm/use-ranges.rst
index 6ef7f476ba2ff..6f9efa590de53 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/llvm/use-ranges.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/llvm/use-ranges.rst
@@ -27,6 +27,7 @@ Supported algorithms
Calls to the following STL algorithms are checked:
+``std::accumulate``,
``std::adjacent_find``,
``std::all_of``,
``std::any_of``,
diff --git a/clang-tools-extra/test/clang-tidy/checkers/llvm/use-ranges.cpp b/clang-tools-extra/test/clang-tidy/checkers/llvm/use-ranges.cpp
index 58cdeb440eaa3..415593ae16a53 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/llvm/use-ranges.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/llvm/use-ranges.cpp
@@ -21,6 +21,12 @@ template <typename T> class vector {
template <typename T> T* begin(T (&arr)[5]);
template <typename T> T* end(T (&arr)[5]);
+template <class InputIt, class T>
+T accumulate(InputIt first, InputIt last, T init);
+
+template <class InputIt, class T, class BinaryOp>
+T accumulate(InputIt first, InputIt last, T init, BinaryOp op);
+
template <class InputIt, class T>
InputIt find(InputIt first, InputIt last, const T &value);
@@ -78,11 +84,20 @@ OutputIt replace_copy_if(InputIt first, InputIt last, OutputIt d_first,
bool is_even(int x);
void double_ref(int& x);
+int multiply(int a, int b);
void test_positive() {
std::vector<int> vec;
int arr[5] = {1, 2, 3, 4, 5};
+ int sum = std::accumulate(vec.begin(), vec.end(), 0);
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use an LLVM range-based algorithm
+ // CHECK-FIXES: int sum = llvm::accumulate(vec, 0);
+
+ int product = std::accumulate(vec.begin(), vec.end(), 1, multiply);
+ // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use an LLVM range-based algorithm
+ // CHECK-FIXES: int product = llvm::accumulate(vec, 1, multiply);
+
auto it1 = std::find(vec.begin(), vec.end(), 3);
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use an LLVM range-based algorithm
// CHECK-FIXES: auto it1 = llvm::find(vec, 3);
>From a3ee6e44111e8bd18b39f411960dd745139107dd Mon Sep 17 00:00:00 2001
From: Jakub Kuderski <jakub at nod-labs.com>
Date: Sat, 24 Jan 2026 06:08:37 -0500
Subject: [PATCH 2/2] Format (one funciton name per line)
---
.../clang-tidy/llvm/UseRangesCheck.cpp | 45 ++++++++++++++-----
1 file changed, 34 insertions(+), 11 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
index bd82915dc232d..8e769d542bc32 100644
--- a/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
@@ -54,17 +54,40 @@ utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
Results.try_emplace(("::std::" + Name).str(), Replacer);
};
- // Single range algorithms.
- AddStdToLLVM(
- llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig),
- {"accumulate", "adjacent_find", "all_of", "any_of",
- "binary_search", "copy", "copy_if", "count",
- "count_if", "fill", "find", "find_if",
- "find_if_not", "for_each", "is_sorted", "lower_bound",
- "max_element", "min_element", "none_of", "partition",
- "partition_point", "remove_if", "replace", "replace_copy",
- "replace_copy_if", "stable_sort", "transform", "uninitialized_copy",
- "unique", "upper_bound"});
+ // Single range algorithms. One per line, keep sorted.
+ // clang-format off
+ AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig),
+ {"accumulate",
+ "adjacent_find",
+ "all_of",
+ "any_of",
+ "binary_search",
+ "copy",
+ "copy_if",
+ "count",
+ "count_if",
+ "fill",
+ "find",
+ "find_if",
+ "find_if_not",
+ "for_each",
+ "is_sorted",
+ "lower_bound",
+ "max_element",
+ "min_element",
+ "none_of",
+ "partition",
+ "partition_point",
+ "remove_if",
+ "replace",
+ "replace_copy",
+ "replace_copy_if",
+ "stable_sort",
+ "transform",
+ "uninitialized_copy",
+ "unique",
+ "upper_bound"});
+ // clang-format on
// Two range algorithms.
AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(TwoSig),
More information about the cfe-commits
mailing list