[libcxx-commits] [libcxx] [libc++[[ranges] Applied `[[nodiscard]]` to `as_rvalue_view` (PR #173145)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Dec 20 01:50:45 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Hristo Hristov (H-G-Hristov)
<details>
<summary>Changes</summary>
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue.
- https://libcxx.llvm.org/CodingGuidelines.html
- https://wg21.link/ranges
- https://wg21.link/range.as.rvalue
Towards #<!-- -->172124
---
Full diff: https://github.com/llvm/llvm-project/pull/173145.diff
2 Files Affected:
- (modified) libcxx/include/__ranges/as_rvalue_view.h (+8-8)
- (added) libcxx/test/libcxx/ranges/range.adaptors/range.as_rvalue_view/nodiscard.verify.cpp (+59)
``````````diff
diff --git a/libcxx/include/__ranges/as_rvalue_view.h b/libcxx/include/__ranges/as_rvalue_view.h
index 08acf3d7736c6..a553f39998e0e 100644
--- a/libcxx/include/__ranges/as_rvalue_view.h
+++ b/libcxx/include/__ranges/as_rvalue_view.h
@@ -48,27 +48,27 @@ class as_rvalue_view : public view_interface<as_rvalue_view<_View>> {
_LIBCPP_HIDE_FROM_ABI constexpr explicit as_rvalue_view(_View __base) : __base_(std::move(__base)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
requires copy_constructible<_View>
{
return __base_;
}
- _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
- _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
requires(!__simple_view<_View>)
{
return move_iterator(ranges::begin(__base_));
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
requires range<const _View>
{
return move_iterator(ranges::begin(__base_));
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end()
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end()
requires(!__simple_view<_View>)
{
if constexpr (common_range<_View>) {
@@ -78,7 +78,7 @@ class as_rvalue_view : public view_interface<as_rvalue_view<_View>> {
}
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
requires range<const _View>
{
if constexpr (common_range<const _View>) {
@@ -88,13 +88,13 @@ class as_rvalue_view : public view_interface<as_rvalue_view<_View>> {
}
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size()
requires sized_range<_View>
{
return ranges::size(__base_);
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
requires sized_range<const _View>
{
return ranges::size(__base_);
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.as_rvalue_view/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.as_rvalue_view/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..d3b711dae631f
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.as_rvalue_view/nodiscard.verify.cpp
@@ -0,0 +1,59 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "test_range.h"
+
+struct NonSimpleView : std::ranges::view_base {
+ int* begin() const;
+ int* end() const;
+
+ const int* begin();
+ const int* end();
+
+ constexpr std::size_t size() { return 0; };
+};
+static_assert(!simple_view<NonSimpleView>);
+
+void test() {
+ NonSimpleView range;
+
+ auto v = std::views::as_rvalue(range);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.base();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::move(v).base();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.begin();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(v).begin();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.end();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(v).end();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.size();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(v).size();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::as_rvalue(range);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::views::as_rvalue(v);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/173145
More information about the libcxx-commits
mailing list