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

Hristo Hristov via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 8 20:59:08 PST 2025


================
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 the libc++ extension that std::ranges::chunk_view::iterator<Const>::operator* is marked as [[nodiscard]].
+
+#include <ranges>
+#include <utility>
+
+void test() {
+  char range[6] = {'x', 'x', 'y', 'y', 'z', 'z'};
+  auto view     = range | std::views::chunk(2);
+
+  // clang-format off
+  *view.begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  *std::as_const(view).begin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  // clang-format on
+}
----------------
H-G-Hristov wrote:

In case you missed my previous comment: https://github.com/llvm/llvm-project/pull/171234#discussion_r2600958296
Unlike the general rule to test each function in a separate file, we'd put all `[[nodiscard]]` tests in a single file `nodiscard.verify.cpp`.
Currently: `libcxx/test/libcxx/diagnostics/ranges.nodiscard.verify.cpp` exists but I'd split it per range because it will become unwieldy.

`// clang-format off` and `on` is OK but I'd prefer the following to avoid having to format manually:

```suggestion
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.begin(); 
   // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
  *std::as_const(view).begin();
}
```

There are also other options provided by lit but I don't use them. You can use whatever you prefer. No chances requested.


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


More information about the llvm-commits mailing list