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

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jan 4 22:37:59 PST 2026


================
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++20
+
+// Check that functions are marked [[nodiscard]]
+
+#include <cstddef>
+#include <ranges>
+#include <utility>
+
+struct View : std::ranges::view_interface<View> {
+  int* begin() noexcept;
+  int* end() noexcept;
+  const int* begin() const noexcept;
+  const int* end() const noexcept;
+};
+
+template <>
+inline constexpr bool std::ranges::enable_view<View> = true;
+
+static_assert(std::ranges::sized_range<View>);
+static_assert(std::ranges::sized_range<const View>);
+static_assert(std::contiguous_iterator<std::ranges::iterator_t<View>>);
+static_assert(std::contiguous_iterator<std::ranges::iterator_t<const View>>);
+static_assert(std::ranges::forward_range<View>);
+static_assert(std::ranges::forward_range<const View>);
+static_assert(std::sized_sentinel_for<std::ranges::sentinel_t<View>, std::ranges::iterator_t<View>>);
+static_assert(std::sized_sentinel_for<std::ranges::sentinel_t<const View>, std::ranges::iterator_t<const View>>);
+static_assert(std::ranges::bidirectional_range<View>);
+static_assert(std::ranges::bidirectional_range<const View>);
+static_assert(std::ranges::common_range<View>);
+static_assert(std::ranges::common_range<const View>);
+
+void test() {
+  View v;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  v.empty();
----------------
H-G-Hristov wrote:

I don't think that's worth it for the `[[nodiscard]]` case.

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


More information about the libcxx-commits mailing list