[libcxx-commits] [libcxx] WIP [libc++][numeric] Applied `[[nodiscard]]` to `<numeric>` (PR #202770)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jun 9 15:46:43 PDT 2026
https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/202770
>From 57c2d70ab534e603199acfbbcf408f6535ab4809 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 9 Jun 2026 23:42:23 +0300
Subject: [PATCH] [libc++][numeric] Applied `[[nodiscard]]` to `<numeric>`
https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
Applied `[[nodiscard]]` to the remaining functions.
Towards: https://github.com/llvm/llvm-project/issues/172124
Reference:
- https://eel.is/c++draft/numeric.ops.overview
- https://eel.is/c++draft/numeric.ops
---
libcxx/include/__numeric/accumulate.h | 4 +-
libcxx/include/__numeric/gcd_lcm.h | 4 +-
libcxx/include/__numeric/inner_product.h | 4 +-
libcxx/include/__numeric/pstl.h | 12 ++---
libcxx/include/__numeric/reduce.h | 8 +--
libcxx/include/__numeric/transform_reduce.h | 8 +--
.../test/libcxx/numerics/nodiscard.verify.cpp | 48 -----------------
.../numerics/numeric.ops/nodiscard.verify.cpp | 54 +++++++++++++++++++
.../libcxx/utilities/bit/nodiscard.verify.cpp | 4 ++
9 files changed, 78 insertions(+), 68 deletions(-)
delete mode 100644 libcxx/test/libcxx/numerics/nodiscard.verify.cpp
create mode 100644 libcxx/test/libcxx/numerics/numeric.ops/nodiscard.verify.cpp
diff --git a/libcxx/include/__numeric/accumulate.h b/libcxx/include/__numeric/accumulate.h
index fedc1c46dfd02..1e6a5f378a886 100644
--- a/libcxx/include/__numeric/accumulate.h
+++ b/libcxx/include/__numeric/accumulate.h
@@ -23,7 +23,7 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIterator, class _Tp>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) {
for (; __first != __last; ++__first)
#if _LIBCPP_STD_VER >= 20
@@ -35,7 +35,7 @@ accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) {
}
template <class _InputIterator, class _Tp, class _BinaryOperation>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op) {
for (; __first != __last; ++__first)
#if _LIBCPP_STD_VER >= 20
diff --git a/libcxx/include/__numeric/gcd_lcm.h b/libcxx/include/__numeric/gcd_lcm.h
index 5ab870fa7349c..753b42621fcc1 100644
--- a/libcxx/include/__numeric/gcd_lcm.h
+++ b/libcxx/include/__numeric/gcd_lcm.h
@@ -47,7 +47,7 @@ constexpr _LIBCPP_HIDE_FROM_ABI _Result __abs_in_type(_Source __t) noexcept {
}
template <class _Tp, class _Up>
-constexpr _LIBCPP_HIDE_FROM_ABI common_type_t<_Tp, _Up> gcd(_Tp __m, _Up __n) {
+[[nodiscard]] constexpr _LIBCPP_HIDE_FROM_ABI common_type_t<_Tp, _Up> gcd(_Tp __m, _Up __n) {
static_assert(is_integral<_Tp>::value && is_integral<_Up>::value, "Arguments to gcd must be integer types");
static_assert(!is_same<__remove_cv_t<_Tp>, bool>::value, "First argument to gcd cannot be bool");
static_assert(!is_same<__remove_cv_t<_Up>, bool>::value, "Second argument to gcd cannot be bool");
@@ -95,7 +95,7 @@ constexpr _LIBCPP_HIDE_FROM_ABI common_type_t<_Tp, _Up> gcd(_Tp __m, _Up __n) {
}
template <class _Tp, class _Up>
-constexpr _LIBCPP_HIDE_FROM_ABI common_type_t<_Tp, _Up> lcm(_Tp __m, _Up __n) {
+[[nodiscard]] constexpr _LIBCPP_HIDE_FROM_ABI common_type_t<_Tp, _Up> lcm(_Tp __m, _Up __n) {
static_assert(is_integral<_Tp>::value && is_integral<_Up>::value, "Arguments to lcm must be integer types");
static_assert(!is_same<__remove_cv_t<_Tp>, bool>::value, "First argument to lcm cannot be bool");
static_assert(!is_same<__remove_cv_t<_Up>, bool>::value, "Second argument to lcm cannot be bool");
diff --git a/libcxx/include/__numeric/inner_product.h b/libcxx/include/__numeric/inner_product.h
index 0deab3d421b77..734a51b69098d 100644
--- a/libcxx/include/__numeric/inner_product.h
+++ b/libcxx/include/__numeric/inner_product.h
@@ -23,7 +23,7 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIterator1, class _InputIterator2, class _Tp>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) {
for (; __first1 != __last1; ++__first1, (void)++__first2)
#if _LIBCPP_STD_VER >= 20
@@ -35,7 +35,7 @@ inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2
}
template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp inner_product(
+[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp inner_product(
_InputIterator1 __first1,
_InputIterator1 __last1,
_InputIterator2 __first2,
diff --git a/libcxx/include/__numeric/pstl.h b/libcxx/include/__numeric/pstl.h
index fe7b2cc7a82cc..ecbc9f5348333 100644
--- a/libcxx/include/__numeric/pstl.h
+++ b/libcxx/include/__numeric/pstl.h
@@ -41,7 +41,7 @@ template <class _ExecutionPolicy,
class _BinaryOperation,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
-_LIBCPP_HIDE_FROM_ABI _Tp reduce(
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _Tp reduce(
_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Tp __init, _BinaryOperation __op) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "reduce requires ForwardIterators");
using _Implementation = __pstl::__dispatch<__pstl::__reduce, __pstl::__current_configuration, _RawPolicy>;
@@ -58,7 +58,7 @@ template <class _ExecutionPolicy,
class _Tp,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
-_LIBCPP_HIDE_FROM_ABI _Tp
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _Tp
reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Tp __init) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "reduce requires ForwardIterators");
using _Implementation = __pstl::__dispatch<__pstl::__reduce, __pstl::__current_configuration, _RawPolicy>;
@@ -70,7 +70,7 @@ template <class _ExecutionPolicy,
class _ForwardIterator,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
-_LIBCPP_HIDE_FROM_ABI __iterator_value_type<_ForwardIterator>
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI __iterator_value_type<_ForwardIterator>
reduce(_ExecutionPolicy&& __policy, _ForwardIterator __first, _ForwardIterator __last) {
_LIBCPP_REQUIRE_CPP17_FORWARD_ITERATOR(_ForwardIterator, "reduce requires ForwardIterators");
using _Implementation = __pstl::__dispatch<__pstl::__reduce, __pstl::__current_configuration, _RawPolicy>;
@@ -90,7 +90,7 @@ template <class _ExecutionPolicy,
class _BinaryOperation2,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
-_LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
_ExecutionPolicy&& __policy,
_ForwardIterator1 __first1,
_ForwardIterator1 __last1,
@@ -120,7 +120,7 @@ template <class _ExecutionPolicy,
class _Tp,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
-_LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
_ExecutionPolicy&& __policy,
_ForwardIterator1 __first1,
_ForwardIterator1 __last1,
@@ -147,7 +147,7 @@ template <class _ExecutionPolicy,
class _UnaryOperation,
class _RawPolicy = __remove_cvref_t<_ExecutionPolicy>,
enable_if_t<is_execution_policy_v<_RawPolicy>, int> = 0>
-_LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _Tp transform_reduce(
_ExecutionPolicy&& __policy,
_ForwardIterator __first,
_ForwardIterator __last,
diff --git a/libcxx/include/__numeric/reduce.h b/libcxx/include/__numeric/reduce.h
index 6c205bf581fb9..a3bfab1fc0948 100644
--- a/libcxx/include/__numeric/reduce.h
+++ b/libcxx/include/__numeric/reduce.h
@@ -26,21 +26,21 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
template <class _InputIterator, class _Tp, class _BinaryOp>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
-reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b) {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI
+_LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b) {
for (; __first != __last; ++__first)
__init = __b(std::move(__init), *__first);
return __init;
}
template <class _InputIterator, class _Tp>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
reduce(_InputIterator __first, _InputIterator __last, _Tp __init) {
return std::reduce(__first, __last, __init, std::plus<>());
}
template <class _InputIterator>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename iterator_traits<_InputIterator>::value_type
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename iterator_traits<_InputIterator>::value_type
reduce(_InputIterator __first, _InputIterator __last) {
return std::reduce(__first, __last, typename iterator_traits<_InputIterator>::value_type{});
}
diff --git a/libcxx/include/__numeric/transform_reduce.h b/libcxx/include/__numeric/transform_reduce.h
index f1150510f0c36..382c314ca4982 100644
--- a/libcxx/include/__numeric/transform_reduce.h
+++ b/libcxx/include/__numeric/transform_reduce.h
@@ -25,15 +25,15 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER >= 17
template <class _InputIterator, class _Tp, class _BinaryOp, class _UnaryOp>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
-transform_reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b, _UnaryOp __u) {
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+_Tp transform_reduce(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOp __b, _UnaryOp __u) {
for (; __first != __last; ++__first)
__init = __b(std::move(__init), __u(*__first));
return __init;
}
template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOp1, class _BinaryOp2>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp transform_reduce(
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp transform_reduce(
_InputIterator1 __first1,
_InputIterator1 __last1,
_InputIterator2 __first2,
@@ -46,7 +46,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp transform_reduce(
}
template <class _InputIterator1, class _InputIterator2, class _Tp>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp
transform_reduce(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init) {
return std::transform_reduce(__first1, __last1, __first2, std::move(__init), std::plus<>(), std::multiplies<>());
}
diff --git a/libcxx/test/libcxx/numerics/nodiscard.verify.cpp b/libcxx/test/libcxx/numerics/nodiscard.verify.cpp
deleted file mode 100644
index 089ebb73b1502..0000000000000
--- a/libcxx/test/libcxx/numerics/nodiscard.verify.cpp
+++ /dev/null
@@ -1,48 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// REQUIRES: std-at-least-c++20
-
-// <numeric>
-
-// Check that functions are marked [[nodiscard]]
-
-#include <bit>
-#include <numeric>
-
-#include "test_macros.h"
-
-void test() {
- // [bit.rotate]
- std::rotl(0u, 0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::rotr(0u, 0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-
- // clang-format off
-#if TEST_STD_VER >= 26
- // [numeric.sat]
- std::saturating_add(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::saturating_sub(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::saturating_mul(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::saturating_div(94, 82); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::saturating_cast<signed int>(49); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
-#endif // TEST_STD_VER >= 26
- // clang-format on
-
-#if TEST_STD_VER >= 20
- {
- int arr[]{94, 82, 49};
-
- // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::midpoint(94, 82);
- // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::midpoint(arr, arr + 2);
- // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::midpoint(94.0, 82.0);
- }
-#endif
-}
diff --git a/libcxx/test/libcxx/numerics/numeric.ops/nodiscard.verify.cpp b/libcxx/test/libcxx/numerics/numeric.ops/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..668a0d08583d5
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numeric.ops/nodiscard.verify.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <numeric>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <numeric>
+
+#include "test_macros.h"
+
+void test() {
+ {
+ std::initializer_list<int> il{94, 82};
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ accumulate(il.begin(), il.end(), 49);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ accumulate(il.begin(), il.end(), 49, std::multiplies<>());
+
+ }
+
+#if TEST_STD_VER >= 26
+ // [numeric.sat]
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::saturating_add(94, 82);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::saturating_sub(94, 82);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::saturating_mul(94, 82);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::saturating_div(94, 82);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::saturating_cast<signed int>(49);
+#endif // TEST_STD_VER >= 26
+
+#if TEST_STD_VER >= 20
+ {
+ int arr[]{94, 82, 49};
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::midpoint(94, 82);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::midpoint(arr, arr + 2);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::midpoint(94.0, 82.0);
+ }
+#endif
+}
diff --git a/libcxx/test/libcxx/utilities/bit/nodiscard.verify.cpp b/libcxx/test/libcxx/utilities/bit/nodiscard.verify.cpp
index b172d515cf2fe..4d3c9e41b783e 100644
--- a/libcxx/test/libcxx/utilities/bit/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/utilities/bit/nodiscard.verify.cpp
@@ -30,4 +30,8 @@ void func() {
std::countr_one(0u); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
std::has_single_bit(0u); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
std::popcount(0u); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+ // [bit.rotate]
+ std::rotl(0u, 0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::rotr(0u, 0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}
More information about the libcxx-commits
mailing list