[libcxx-commits] [libcxx] [libc++][ranges] Applied [[nodiscard]] to `transform_view` (PR #203791)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jun 15 14:52:15 PDT 2026
https://github.com/lknknm updated https://github.com/llvm/llvm-project/pull/203791
>From 87ef47a2ba60da1854021c47c9aa27da5130b039 Mon Sep 17 00:00:00 2001
From: lknknm <sxswt at protonmail.com>
Date: Sun, 14 Jun 2026 22:38:25 +0200
Subject: [PATCH 1/5] add: nodiscard to transform_view<_View, _Fn>::__sentinel
---
libcxx/include/__ranges/transform_view.h | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/libcxx/include/__ranges/transform_view.h b/libcxx/include/__ranges/transform_view.h
index 98b38dd9b2aa1..01a5d18f7857d 100644
--- a/libcxx/include/__ranges/transform_view.h
+++ b/libcxx/include/__ranges/transform_view.h
@@ -213,7 +213,7 @@ class transform_view<_View, _Fn>::__iterator
_LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() && { return std::move(__current_); }
- _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
noexcept(noexcept(std::invoke(*__parent_->__func_, *__current_))) {
return std::invoke(*__parent_->__func_, *__current_);
}
@@ -361,26 +361,26 @@ class transform_view<_View, _Fn>::__sentinel {
requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
: __end_(std::move(__i.__end_)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
template <bool _OtherConst>
requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
- _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
return __x.__current_ == __y.__end_;
}
template <bool _OtherConst>
requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
- _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
return __x.__current_ - __y.__end_;
}
template <bool _OtherConst>
requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
- _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
- return __x.__end_ - __y.__current_;
+ return __x.__end_ - __y.__current_;
}
};
>From d08e3bd32ff8ea407e449a8c2fafd00b2174b0c0 Mon Sep 17 00:00:00 2001
From: lknknm <sxswt at protonmail.com>
Date: Sun, 14 Jun 2026 23:23:46 +0200
Subject: [PATCH 2/5] fix: clang-format
---
libcxx/include/__ranges/transform_view.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__ranges/transform_view.h b/libcxx/include/__ranges/transform_view.h
index 01a5d18f7857d..9f904dcd79342 100644
--- a/libcxx/include/__ranges/transform_view.h
+++ b/libcxx/include/__ranges/transform_view.h
@@ -365,7 +365,8 @@ class transform_view<_View, _Fn>::__sentinel {
template <bool _OtherConst>
requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr bool
+ operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
return __x.__current_ == __y.__end_;
}
@@ -380,7 +381,7 @@ class transform_view<_View, _Fn>::__sentinel {
requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
- return __x.__end_ - __y.__current_;
+ return __x.__end_ - __y.__current_;
}
};
>From 22992bd85cb737237b48a7270fb3aeed3007f51e Mon Sep 17 00:00:00 2001
From: lknknm <sxswt at protonmail.com>
Date: Sun, 14 Jun 2026 23:58:32 +0200
Subject: [PATCH 3/5] complete: add no-discard to base() iterators
---
libcxx/include/__ranges/transform_view.h | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/libcxx/include/__ranges/transform_view.h b/libcxx/include/__ranges/transform_view.h
index 9f904dcd79342..8dcc4438daea5 100644
--- a/libcxx/include/__ranges/transform_view.h
+++ b/libcxx/include/__ranges/transform_view.h
@@ -88,12 +88,12 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS transform_view : public view_interfac
_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 transform_view(_View __base, _Fn __func)
: __func_(std::in_place, std::move(__func)), __base_(std::move(__base)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
requires copy_constructible<_View>
{
return __base_;
}
- _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
_LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> begin() { return __iterator<false>{*this, ranges::begin(__base_)}; }
_LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> begin() const
@@ -209,9 +209,9 @@ class transform_view<_View, _Fn>::__iterator
requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
: __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __current_; }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __current_; }
- _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() && { return std::move(__current_); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() && { return std::move(__current_); }
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
noexcept(noexcept(std::invoke(*__parent_->__func_, *__current_))) {
>From 91c1d417f7e6bf4c8d910567eb74777203c3e3fa Mon Sep 17 00:00:00 2001
From: lknknm <sxswt at protonmail.com>
Date: Mon, 15 Jun 2026 23:39:01 +0200
Subject: [PATCH 4/5] add: nodiscard to size()
---
libcxx/include/__ranges/transform_view.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__ranges/transform_view.h b/libcxx/include/__ranges/transform_view.h
index 8dcc4438daea5..44b4dad2ca456 100644
--- a/libcxx/include/__ranges/transform_view.h
+++ b/libcxx/include/__ranges/transform_view.h
@@ -119,12 +119,12 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS transform_view : public view_interfac
return __iterator<true>(*this, ranges::end(__base_));
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size()
requires sized_range<_View>
{
return ranges::size(__base_);
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
requires sized_range<const _View>
{
return ranges::size(__base_);
>From 9ff52e13d0ebbf32f52357e1550c6d4bf2724598 Mon Sep 17 00:00:00 2001
From: lknknm <sxswt at protonmail.com>
Date: Mon, 15 Jun 2026 23:51:58 +0200
Subject: [PATCH 5/5] add: nodiscard to transform_view begin() and end()
iterators
---
libcxx/include/__ranges/transform_view.h | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/libcxx/include/__ranges/transform_view.h b/libcxx/include/__ranges/transform_view.h
index 44b4dad2ca456..414bff7084495 100644
--- a/libcxx/include/__ranges/transform_view.h
+++ b/libcxx/include/__ranges/transform_view.h
@@ -93,27 +93,36 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS transform_view : public view_interfac
{
return __base_;
}
+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
- _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> begin() { return __iterator<false>{*this, ranges::begin(__base_)}; }
- _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> begin() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> begin() {
+ return __iterator<false>{*this, ranges::begin(__base_)};
+ }
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> begin() const
requires range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
{
return __iterator<true>(*this, ranges::begin(__base_));
}
- _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<false> end() { return __sentinel<false>(ranges::end(__base_)); }
- _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> end()
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<false> end() {
+ return __sentinel<false>(ranges::end(__base_));
+ }
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> end()
requires common_range<_View>
{
return __iterator<false>(*this, ranges::end(__base_));
}
- _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<true> end() const
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __sentinel<true> end() const
requires range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
{
return __sentinel<true>(ranges::end(__base_));
}
- _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> end() const
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __iterator<true> end() const
requires common_range<const _View> && __regular_invocable_with_range_ref<const _Fn&, const _View>
{
return __iterator<true>(*this, ranges::end(__base_));
More information about the libcxx-commits
mailing list