[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `views::zip_transform` (PR #207120)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jul 6 00:00:09 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Hristo Hristov (H-G-Hristov)
<details>
<summary>Changes</summary>
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue.
- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/range.zip.transform
Towards #<!-- -->172124
---
Full diff: https://github.com/llvm/llvm-project/pull/207120.diff
4 Files Affected:
- (modified) libcxx/include/__ranges/zip_transform_view.h (+17-16)
- (added) libcxx/test/libcxx/ranges/range.adaptors/range.zip.transform/nodiscard.verify.cpp (+129)
- (modified) libcxx/test/std/ranges/range.adaptors/range_adaptor_types.h (-30)
- (modified) libcxx/test/support/test_iterators.h (+30-1)
``````````diff
diff --git a/libcxx/include/__ranges/zip_transform_view.h b/libcxx/include/__ranges/zip_transform_view.h
index c56632a2608a1..ffa3e9f9c6dd2 100644
--- a/libcxx/include/__ranges/zip_transform_view.h
+++ b/libcxx/include/__ranges/zip_transform_view.h
@@ -79,15 +79,15 @@ class zip_transform_view : public view_interface<zip_transform_view<_Fn, _Views.
_LIBCPP_HIDE_FROM_ABI constexpr explicit zip_transform_view(_Fn __fun, _Views... __views)
: __zip_(std::move(__views)...), __fun_(in_place, std::move(__fun)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { return __iterator<false>(*this, __zip_.begin()); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { return __iterator<false>(*this, __zip_.begin()); }
- _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
requires range<const _InnerView> && regular_invocable<const _Fn&, range_reference_t<const _Views>...>
{
return __iterator<true>(*this, __zip_.begin());
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
if constexpr (common_range<_InnerView>) {
return __iterator<false>(*this, __zip_.end());
} else {
@@ -95,7 +95,7 @@ class zip_transform_view : public view_interface<zip_transform_view<_Fn, _Views.
}
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
requires range<const _InnerView> && regular_invocable<const _Fn&, range_reference_t<const _Views>...>
{
if constexpr (common_range<const _InnerView>) {
@@ -105,13 +105,13 @@ class zip_transform_view : public view_interface<zip_transform_view<_Fn, _Views.
}
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size()
requires sized_range<_InnerView>
{
return __zip_.size();
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
requires sized_range<const _InnerView>
{
return __zip_.size();
@@ -184,7 +184,7 @@ class zip_transform_view<_Fn, _Views...>::__iterator
requires _Const && convertible_to<__ziperator<false>, __ziperator<_Const>>
: __parent_(__i.__parent_), __inner_(std::move(__i.__inner_)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
noexcept(noexcept(std::apply(__get_deref_and_invoke(), __zip_view_iterator_access::__get_underlying(__inner_)))) {
return std::apply(__get_deref_and_invoke(), __zip_view_iterator_access::__get_underlying(__inner_));
}
@@ -233,7 +233,7 @@ class zip_transform_view<_Fn, _Views...>::__iterator
return *this;
}
- _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const
requires random_access_range<_Base>
{
return std::apply(
@@ -255,25 +255,26 @@ class zip_transform_view<_Fn, _Views...>::__iterator
return __x.__inner_ <=> __y.__inner_;
}
- _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(const __iterator& __i, difference_type __n)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(const __iterator& __i, difference_type __n)
requires random_access_range<_Base>
{
return __iterator(*__i.__parent_, __i.__inner_ + __n);
}
- _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, const __iterator& __i)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, const __iterator& __i)
requires random_access_range<_Base>
{
return __iterator(*__i.__parent_, __i.__inner_ + __n);
}
- _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(const __iterator& __i, difference_type __n)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(const __iterator& __i, difference_type __n)
requires random_access_range<_Base>
{
return __iterator(*__i.__parent_, __i.__inner_ - __n);
}
- _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type
+ operator-(const __iterator& __x, const __iterator& __y)
requires sized_sentinel_for<__ziperator<_Const>, __ziperator<_Const>>
{
return __x.__inner_ - __y.__inner_;
@@ -307,14 +308,14 @@ class zip_transform_view<_Fn, _Views...>::__sentinel {
template <bool _OtherConst>
requires sized_sentinel_for<__zentinel<_Const>, __ziperator<_OtherConst>>
- _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
return __x.__inner_ - __y.__inner_;
}
template <bool _OtherConst>
requires sized_sentinel_for<__zentinel<_Const>, __ziperator<_OtherConst>>
- _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
return __x.__inner_ - __y.__inner_;
}
@@ -327,14 +328,14 @@ struct __fn {
template <class _Fn>
requires(move_constructible<decay_t<_Fn>> && regular_invocable<decay_t<_Fn>&> &&
is_object_v<invoke_result_t<decay_t<_Fn>&>>)
- _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&&) const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&&) const
noexcept(noexcept(auto(views::empty<decay_t<invoke_result_t<decay_t<_Fn>&>>>))) {
return views::empty<decay_t<invoke_result_t<decay_t<_Fn>&>>>;
}
template <class _Fn, class... _Ranges>
requires(sizeof...(_Ranges) > 0)
- _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&& __fun, _Ranges&&... __rs) const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Fn&& __fun, _Ranges&&... __rs) const
noexcept(noexcept(zip_transform_view(std::forward<_Fn>(__fun), std::forward<_Ranges>(__rs)...)))
-> decltype(zip_transform_view(std::forward<_Fn>(__fun), std::forward<_Ranges>(__rs)...)) {
return zip_transform_view(std::forward<_Fn>(__fun), std::forward<_Ranges>(__rs)...);
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.zip.transform/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.zip.transform/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..e681d338dc786
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.zip.transform/nodiscard.verify.cpp
@@ -0,0 +1,129 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++23
+
+// Check that functions are marked [[nodiscard]]
+
+#include <ranges>
+#include <utility>
+#include <vector>
+
+#include "test_iterators.h"
+
+struct CommonView : std::ranges::view_interface<CommonView> {
+ int* begin();
+ int* begin() const;
+ int* end();
+ int* end() const;
+
+ int size() const;
+};
+static_assert(std::ranges::common_range<CommonView>);
+static_assert(std::same_as<std::ranges::iterator_t<CommonView>, std::ranges::iterator_t<const CommonView>>);
+static_assert(std::same_as<std::ranges::sentinel_t<CommonView>, std::ranges::sentinel_t<const CommonView>>);
+
+struct ForwardSizedNonCommon {
+ int* buffer_ = nullptr;
+ std::size_t size_ = 0;
+
+ template <std::size_t N>
+ constexpr ForwardSizedNonCommon(int (&b)[N]) : buffer_(b), size_(N) {}
+
+ constexpr ForwardSizedNonCommon(int* b, std::size_t s) : buffer_(b), size_(s) {}
+
+ using iterator = forward_sized_iterator<int*>;
+ using sentinel = sized_sentinel<iterator>;
+
+ constexpr iterator begin() const { return iterator(buffer_); }
+ constexpr sentinel end() const { return sentinel(iterator(buffer_ + size_)); }
+};
+static_assert(std::ranges::forward_range<ForwardSizedNonCommon>);
+static_assert(std::ranges::sized_range<ForwardSizedNonCommon>);
+static_assert(!std::ranges::common_range<ForwardSizedNonCommon>);
+static_assert(!std::ranges::random_access_range<ForwardSizedNonCommon>);
+
+template <class... Args>
+struct Invocable {
+ int operator()(Args...) const { return 5; }
+};
+
+void test() {
+ CommonView commonRange;
+ std::ranges::zip_transform_view commonZtv{[](int x) { return x; }, commonRange};
+ static_assert(std::same_as<decltype(commonZtv.end()), decltype(commonZtv.begin())>);
+
+ // [range.zip_transform.view]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ commonZtv.begin();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(commonZtv).begin();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ commonZtv.end();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(commonZtv).end();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ commonZtv.size();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(commonZtv).size();
+
+ // [range.zip_transform.iterator]
+ {
+ auto it = commonZtv.begin();
+ auto cIt = std::as_const(commonZtv).begin();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ *cIt;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cIt[0];
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ it + 1;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ 1 + it;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ it - 1;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ it - it;
+ }
+
+ // [range.zip_transform.sentinel]
+ {
+ auto MakeTuple = [](auto&&... args) { return std::tuple(std::forward<decltype(args)>(args)...); };
+ int arr[] = {1, 2, 3};
+ std::ranges::zip_transform_view nonCommonZtv{MakeTuple, std::views::iota(0, 3), ForwardSizedNonCommon(arr)};
+ static_assert(!std::ranges::common_range<decltype(nonCommonZtv)>);
+ static_assert(!std::same_as<decltype(nonCommonZtv.end()), decltype(nonCommonZtv.begin())>);
+ auto it = nonCommonZtv.begin();
+ auto cIt = std::as_const(nonCommonZtv).begin();
+ auto st = nonCommonZtv.end();
+ auto cSt = std::as_const(nonCommonZtv).end();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ it - st;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ st - it;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ it - cSt;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cSt - it;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ cIt - st;
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ st - cIt;
+ }
+
+ // [range.zip_transform.overview]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::zip_transform(Invocable<>{});
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::zip_transform([](int x) { return x; }, commonRange);
+}
diff --git a/libcxx/test/std/ranges/range.adaptors/range_adaptor_types.h b/libcxx/test/std/ranges/range.adaptors/range_adaptor_types.h
index 0b654566e35b8..41ca28fa905e4 100644
--- a/libcxx/test/std/ranges/range.adaptors/range_adaptor_types.h
+++ b/libcxx/test/std/ranges/range.adaptors/range_adaptor_types.h
@@ -208,36 +208,6 @@ struct BasicView : IntBufferView {
}
};
-template <class Base = int*>
-struct forward_sized_iterator {
- Base it_ = nullptr;
-
- using iterator_category = std::forward_iterator_tag;
- using value_type = int;
- using difference_type = std::intptr_t;
- using pointer = Base;
- using reference = decltype(*Base{});
-
- forward_sized_iterator() = default;
- constexpr forward_sized_iterator(Base it) : it_(it) {}
-
- constexpr reference operator*() const { return *it_; }
-
- constexpr forward_sized_iterator& operator++() {
- ++it_;
- return *this;
- }
- constexpr forward_sized_iterator operator++(int) { return forward_sized_iterator(it_++); }
-
- friend constexpr bool operator==(const forward_sized_iterator&, const forward_sized_iterator&) = default;
-
- friend constexpr difference_type operator-(const forward_sized_iterator& x, const forward_sized_iterator& y) {
- return x.it_ - y.it_;
- }
-};
-static_assert(std::forward_iterator<forward_sized_iterator<>>);
-static_assert(std::sized_sentinel_for<forward_sized_iterator<>, forward_sized_iterator<>>);
-
using ForwardSizedView = BasicView<forward_sized_iterator<>>;
static_assert(std::ranges::forward_range<ForwardSizedView>);
static_assert(std::ranges::sized_range<ForwardSizedView>);
diff --git a/libcxx/test/support/test_iterators.h b/libcxx/test/support/test_iterators.h
index 4fc8345c2dcef..f443abcddb3bb 100644
--- a/libcxx/test/support/test_iterators.h
+++ b/libcxx/test/support/test_iterators.h
@@ -1175,6 +1175,36 @@ class sized_sentinel {
template <class It>
sized_sentinel(It) -> sized_sentinel<It>;
+template <class Base = int*>
+struct forward_sized_iterator {
+ Base it_ = nullptr;
+
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = int;
+ using difference_type = std::intptr_t;
+ using pointer = Base;
+ using reference = decltype(*Base{});
+
+ forward_sized_iterator() = default;
+ constexpr forward_sized_iterator(Base it) : it_(it) {}
+
+ constexpr reference operator*() const { return *it_; }
+
+ constexpr forward_sized_iterator& operator++() {
+ ++it_;
+ return *this;
+ }
+ constexpr forward_sized_iterator operator++(int) { return forward_sized_iterator(it_++); }
+
+ friend constexpr bool operator==(const forward_sized_iterator&, const forward_sized_iterator&) = default;
+
+ friend constexpr difference_type operator-(const forward_sized_iterator& x, const forward_sized_iterator& y) {
+ return x.it_ - y.it_;
+ }
+};
+static_assert(std::forward_iterator<forward_sized_iterator<>>);
+static_assert(std::sized_sentinel_for<forward_sized_iterator<>, forward_sized_iterator<>>);
+
namespace adl {
class Iterator {
@@ -1880,5 +1910,4 @@ using cpp20_input_iterator_list =
#endif
} // namespace types
-
#endif // SUPPORT_TEST_ITERATORS_H
``````````
</details>
https://github.com/llvm/llvm-project/pull/207120
More information about the libcxx-commits
mailing list