[libcxx-commits] [libcxx] [libc++] Implement P2442R1 `std::views::chunk` (PR #171234)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jun 25 23:04:37 PDT 2026


================
@@ -0,0 +1,73 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <ranges>
+
+// Test that functions are marked [[nodiscard]].
+
+#include <ranges>
+#include <utility>
+
+void test() {
+  char range[6] = {'x', 'x', 'y', 'y', 'z', 'z'};
+  auto view     = range | std::views::chunk(2);
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  view.base();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(view).base();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::move(std::as_const(view)).base();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::move(view).base();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  view.begin();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::as_const(view).begin();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::iter_move(view.begin());
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::iter_move(std::as_const(view).begin());
----------------
Zingam wrote:



This tests `std::ranges::iter_move` and not the member `iter_move`. 
Maybe not te best example but you want something like that:
https://github.com/llvm/llvm-project/blob/8d60471f89fc7b57c08d64d7524dfbff48078567/libcxx/test/libcxx/ranges/range.adaptors/range.join.with/nodiscard.verify.cpp#L44
Also there is no need to write to test cases if one is enough.

e.g. 

```suggestion

  auto cIt = std::as_const(view).begin();

....

  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
  iter_move(cIt);
```

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


More information about the libcxx-commits mailing list