[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `views::zip_transform` (PR #207120)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Jul 4 22:49:28 PDT 2026
================
@@ -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];
----------------
Zingam wrote:
The test suit is already inconsistent on snake_case vs CameCase, or whatEverCase including `[[nodiscard]]` tests.
So I am not nitpicking on that.
https://github.com/llvm/llvm-project/pull/207120
More information about the libcxx-commits
mailing list