[clang-tools-extra] 69059c4 - [clang-tidy] Add llvm::accumulate to llvm-use-ranges (#177655)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 24 03:24:20 PST 2026
Author: Jakub Kuderski
Date: 2026-01-24T11:24:17Z
New Revision: 69059c42f7539fd6c41b0a152862ae8741bf8016
URL: https://github.com/llvm/llvm-project/commit/69059c42f7539fd6c41b0a152862ae8741bf8016
DIFF: https://github.com/llvm/llvm-project/commit/69059c42f7539fd6c41b0a152862ae8741bf8016.diff
LOG: [clang-tidy] Add llvm::accumulate to llvm-use-ranges (#177655)
I missed this in https://github.com/llvm/llvm-project/pull/177457.
All range wrappers from STLExtras should be covered by llvm-use-ranges
now.
Added:
Modified:
clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/llvm/use-ranges.rst
clang-tools-extra/test/clang-tidy/checkers/llvm/use-ranges.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp b/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
index 70d8e75405e91..8e769d542bc32 100644
--- a/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp
@@ -54,9 +54,11 @@ utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
Results.try_emplace(("::std::" + Name).str(), Replacer);
};
- // Single range algorithms.
+ // Single range algorithms. One per line, keep sorted.
+ // clang-format off
AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig),
- {"adjacent_find",
+ {"accumulate",
+ "adjacent_find",
"all_of",
"any_of",
"binary_search",
@@ -85,6 +87,7 @@ utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
"uninitialized_copy",
"unique",
"upper_bound"});
+ // clang-format on
// 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);
More information about the cfe-commits
mailing list