[libcxx-commits] [libcxx] [libc++][ranges] Applied [[nodiscard]] to `adjacent_transform_view` (PR #205900)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 2 15:47:14 PDT 2026


================
@@ -0,0 +1,94 @@
+
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// Test the libc++ extension that std::ranges::adjacent_transform_view and std::views::adjacent_transform are marked as [[nodiscard]].
+
+#include <functional>
+#include <ranges>
+#include <utility>
+
+struct View : std::ranges::view_interface<View> {
+  int* begin();
+  const int* begin() const;
+  volatile int* end();
+  const volatile int* end() 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>>);
+
+void test() {
+  auto v = View{} | std::views::adjacent_transform<2>(std::multiplies());
+
+  // [range.adjacent.transform.view]
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(v).base();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::move(v).base();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  v.begin();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(v).begin();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  v.end();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(v).end();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  v.size();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(v).size();
+
+  // [range.adjacent.transform.iterator]
+
+  auto it   = v.begin();
+  auto c_it = std::as_const(v).begin();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  *c_it;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  c_it[0];
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it + 0;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  0 + it;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it - 0;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  it - it;
+
+  // [range.adjacent.tranform.sentinel]
+
+  auto st = v.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}}
+  st - c_it;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  c_it - st;
+
+  // [range.adjacent.overview]
+
+  int range[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::views::adjacent_transform<0>(range);
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::views::adjacent_transform<2>(range, std::multiplies());
----------------
Zingam wrote:

```suggestion
  int range[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  static_assert(std::ranges::forward_range<decltype(range)>);

  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
  std::views::adjacent_transform<0>(range, Fn{});

  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
  std::views::adjacent_transform<2>(range, std::multiplies());

  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
  std::views::adjacent_transform<2>(std::multiplies());
```

There are three operators. Take `Fn` from `adaptor.pass.cpp`:
```c++
  struct Fn {
    int operator()(auto...) const;
  };
```

https://github.com/llvm/llvm-project/pull/205900


More information about the libcxx-commits mailing list