[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `views::zip_transform` (PR #207120)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jul 1 22:16:51 PDT 2026


https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/207120

`[[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

>From b000ef2650ce3656f562bf8663fd9deef15a3201 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Thu, 2 Jul 2026 08:16:28 +0300
Subject: [PATCH] [libc++][ranges] Applied `[[nodiscard]]` to
 `views::zip_transform`

`[[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
---
 libcxx/include/__ranges/zip_transform_view.h  | 32 +++----
 .../range.zip.transform/nodiscard.verify.cpp  | 88 +++++++++++++++++++
 2 files changed, 104 insertions(+), 16 deletions(-)
 create mode 100644 libcxx/test/libcxx/ranges/range.adaptors/range.zip.transform/nodiscard.verify.cpp

diff --git a/libcxx/include/__ranges/zip_transform_view.h b/libcxx/include/__ranges/zip_transform_view.h
index c56632a2608a1..71849105d40f6 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,25 @@ 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 +307,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 +327,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..9c8673c60b1dd
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.zip.transform/nodiscard.verify.cpp
@@ -0,0 +1,88 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+struct View : std::ranges::view_interface<View> {
+  int* begin();
+  const int* begin() const;
+  volatile int* end();
+  const volatile int* end() const;
+
+  int size() const;
+};
+static_assert(!std::ranges::common_range<View>);
+static_assert(!std::same_as<std::ranges::iterator_t<View>, std::ranges::iterator_t<const View>>);
+static_assert(!std::same_as<std::ranges::sentinel_t<View>, std::ranges::sentinel_t<const View>>);
+
+template <class... Args>
+struct Invocable {
+  int operator()(Args...) const { return 5; }
+};
+
+void test() {
+  View range;
+  std::ranges::zip_transform_view ztv{[](int x) { return x; }, range};
+
+  // [range.zip_transform.view]
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  ztv.begin();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(ztv).begin();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  ztv.end();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(ztv).end();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  ztv.size();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(ztv).size();
+
+  // [range.zip_transform.iterator]
+
+  auto it = ztv.begin();
+  auto cIt = std::as_const(ztv).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 st = ztv.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;
+
+  // [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; }, range);
+}
\ No newline at end of file



More information about the libcxx-commits mailing list