[libcxx-commits] [libcxx] [libc++] Add [[nodiscard]] to std::prev and std::next (PR #109550)
Marc Auberer via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Sep 25 14:25:23 PDT 2024
https://github.com/marcauberer updated https://github.com/llvm/llvm-project/pull/109550
>From ea7e0a70b997fb09ec1798941e4310f7a30a3ff5 Mon Sep 17 00:00:00 2001
From: Marc Auberer <marc.auberer at chillibits.com>
Date: Sat, 21 Sep 2024 22:47:15 +0200
Subject: [PATCH 1/3] [libc++] Add [[nodiscard]] to std::prev and std::next
---
libcxx/include/__iterator/next.h | 8 ++++----
libcxx/include/__iterator/prev.h | 6 +++---
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h
index fb6c8ea6d75508..4064bcec4e183b 100644
--- a/libcxx/include/__iterator/next.h
+++ b/libcxx/include/__iterator/next.h
@@ -43,25 +43,25 @@ next(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n =
namespace ranges {
struct __next {
template <input_or_output_iterator _Ip>
- _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x) const {
++__x;
return __x;
}
template <input_or_output_iterator _Ip>
- _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
ranges::advance(__x, __n);
return __x;
}
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
- _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp __bound_sentinel) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp __bound_sentinel) const {
ranges::advance(__x, __bound_sentinel);
return __x;
}
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
- _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp __bound_sentinel) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp __bound_sentinel) const {
ranges::advance(__x, __n, __bound_sentinel);
return __x;
}
diff --git a/libcxx/include/__iterator/prev.h b/libcxx/include/__iterator/prev.h
index e950d8dc414717..0b88d33d924cfe 100644
--- a/libcxx/include/__iterator/prev.h
+++ b/libcxx/include/__iterator/prev.h
@@ -42,19 +42,19 @@ prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n =
namespace ranges {
struct __prev {
template <bidirectional_iterator _Ip>
- _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x) const {
--__x;
return __x;
}
template <bidirectional_iterator _Ip>
- _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n) const {
ranges::advance(__x, -__n);
return __x;
}
template <bidirectional_iterator _Ip>
- _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound_iter) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound_iter) const {
ranges::advance(__x, -__n, __bound_iter);
return __x;
}
>From e988c95b6a9e57138cd9d4efb485eff9108baac3 Mon Sep 17 00:00:00 2001
From: Marc Auberer <marc.auberer at chillibits.com>
Date: Sat, 21 Sep 2024 22:52:50 +0200
Subject: [PATCH 2/3] Format
---
libcxx/include/__iterator/next.h | 3 ++-
libcxx/include/__iterator/prev.h | 3 ++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h
index 4064bcec4e183b..27b21e6f52c272 100644
--- a/libcxx/include/__iterator/next.h
+++ b/libcxx/include/__iterator/next.h
@@ -61,7 +61,8 @@ struct __next {
}
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp __bound_sentinel) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip
+ operator()(_Ip __x, iter_difference_t<_Ip> __n, _Sp __bound_sentinel) const {
ranges::advance(__x, __n, __bound_sentinel);
return __x;
}
diff --git a/libcxx/include/__iterator/prev.h b/libcxx/include/__iterator/prev.h
index 0b88d33d924cfe..6ee9f08308660e 100644
--- a/libcxx/include/__iterator/prev.h
+++ b/libcxx/include/__iterator/prev.h
@@ -54,7 +54,8 @@ struct __prev {
}
template <bidirectional_iterator _Ip>
- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound_iter) const {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip
+ operator()(_Ip __x, iter_difference_t<_Ip> __n, _Ip __bound_iter) const {
ranges::advance(__x, -__n, __bound_iter);
return __x;
}
>From 9f8f94e983e22d3d97dec285453af9d8e4894aa2 Mon Sep 17 00:00:00 2001
From: Marc Auberer <marc.auberer at chillibits.com>
Date: Wed, 25 Sep 2024 23:25:13 +0200
Subject: [PATCH 3/3] Add tests
---
libcxx/include/__iterator/next.h | 2 +-
libcxx/include/__iterator/prev.h | 2 +-
.../diagnostics/iterator.nodiscard.verify.cpp | 14 +++++++++++---
3 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h
index 27b21e6f52c272..1f68a5bec8f39c 100644
--- a/libcxx/include/__iterator/next.h
+++ b/libcxx/include/__iterator/next.h
@@ -25,7 +25,7 @@
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter
next(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {
// Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation.
// Note that this check duplicates the similar check in `std::advance`.
diff --git a/libcxx/include/__iterator/prev.h b/libcxx/include/__iterator/prev.h
index 6ee9f08308660e..7e97203836eb98 100644
--- a/libcxx/include/__iterator/prev.h
+++ b/libcxx/include/__iterator/prev.h
@@ -25,7 +25,7 @@
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _InputIter, __enable_if_t<__has_input_iterator_category<_InputIter>::value, int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _InputIter
prev(_InputIter __x, typename iterator_traits<_InputIter>::difference_type __n = 1) {
// Calling `advance` with a negative value on a non-bidirectional iterator is a no-op in the current implementation.
// Note that this check duplicates the similar check in `std::advance`.
diff --git a/libcxx/test/libcxx/diagnostics/iterator.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/iterator.nodiscard.verify.cpp
index 8f9bc3e411f907..b84a35baca85f0 100644
--- a/libcxx/test/libcxx/diagnostics/iterator.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/iterator.nodiscard.verify.cpp
@@ -20,7 +20,15 @@ void test() {
int c_array[] = {1, 2, 3};
std::initializer_list<int> initializer_list;
- std::empty(container); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::empty(c_array); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
- std::empty(initializer_list); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::empty(container); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::empty(c_array); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::empty(initializer_list); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::prev(c_array); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::next(c_array); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::prev(c_array); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ auto pv = std::ranges::prev(container.end(), 2); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::next(pv, 2, container.begin()); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::next(c_array); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ auto nv = std::ranges::next(container.begin(), 2); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::ranges::next(pv, 1, container.end()); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}
More information about the libcxx-commits
mailing list