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

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jun 19 11:41:42 PDT 2026


================
@@ -0,0 +1,242 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// Test the libc++ extension that std::views::transform is marked as [[nodiscard]].
+
+#include <ranges>
+#include <functional>
+
+struct TestView : std::ranges::view_interface<TestView> {
+  int* begin();
+  char* begin() const;
+  const int* end();
+  const char* end() const;
+};
+
+void test() {
+  int range[] = {1, 2, 3};
+  auto f      = [](int i) { return i; };
+
+  auto identity_view     = TestView{} | std::views::transform(std::identity{});
+  auto transformed_range = range | std::views::transform(f);
+
+  const auto const_identity_view     = TestView{} | std::views::transform(std::identity{});
+  const auto const_transformed_range = range | std::views::transform(f);
+
+  {
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    identity_view.begin();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    const_identity_view.begin();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    transformed_range.begin();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    const_transformed_range.begin();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    identity_view.end();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    const_identity_view.end();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    transformed_range.end();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    const_transformed_range.end();
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::views::transform(f);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::views::transform(range, f);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    range | std::views::transform(f);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::views::all | std::views::transform(f);
+  }
+
+  //===---------------------------------------------------------------------------------------===//
+  //=== ADL-based begin() / end() ===//
+
+  using std::begin, std::end, std::size;
+
+  {
+    // ===== Non-const views =====
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    begin(identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    begin(transformed_range);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    end(identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    end(transformed_range);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    size(identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    size(transformed_range);
+  }
+
+  {
+    // ===== Const views =====
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    begin(const_identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    begin(const_transformed_range);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    end(const_identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    end(const_transformed_range);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    size(const_identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    size(const_transformed_range);
+  }
+
+  //===---------------------------------------------------------------------------------------===//
+  //=== std::ranges CPO begin() / end() ===//
+
+  {
+    // ===== Non-const views =====
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::begin(identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::begin(transformed_range);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::end(identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::end(transformed_range);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::size(identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::size(transformed_range);
+  }
+
+  {
+    // ===== Const views =====
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::begin(const_identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::begin(const_transformed_range);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::end(const_identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::end(const_transformed_range);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::size(const_identity_view);
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::ranges::size(const_transformed_range);
+  }
----------------
lknknm wrote:

You mean the ADL and CPOs?

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


More information about the libcxx-commits mailing list