Skip to content

Commit 46fb406

Browse files
committed
[libc++[[ranges] Applied [[nodiscard]] to as_rvalue_view
`[[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
1 parent 99553bc commit 46fb406

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

libcxx/include/__ranges/as_rvalue_view.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,27 @@ class as_rvalue_view : public view_interface<as_rvalue_view<_View>> {
4848

4949
_LIBCPP_HIDE_FROM_ABI constexpr explicit as_rvalue_view(_View __base) : __base_(std::move(__base)) {}
5050

51-
_LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
51+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
5252
requires copy_constructible<_View>
5353
{
5454
return __base_;
5555
}
5656

57-
_LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
57+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
5858

59-
_LIBCPP_HIDE_FROM_ABI constexpr auto begin()
59+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
6060
requires(!__simple_view<_View>)
6161
{
6262
return move_iterator(ranges::begin(__base_));
6363
}
6464

65-
_LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
65+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
6666
requires range<const _View>
6767
{
6868
return move_iterator(ranges::begin(__base_));
6969
}
7070

71-
_LIBCPP_HIDE_FROM_ABI constexpr auto end()
71+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end()
7272
requires(!__simple_view<_View>)
7373
{
7474
if constexpr (common_range<_View>) {
@@ -78,7 +78,7 @@ class as_rvalue_view : public view_interface<as_rvalue_view<_View>> {
7878
}
7979
}
8080

81-
_LIBCPP_HIDE_FROM_ABI constexpr auto end() const
81+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
8282
requires range<const _View>
8383
{
8484
if constexpr (common_range<const _View>) {
@@ -88,13 +88,13 @@ class as_rvalue_view : public view_interface<as_rvalue_view<_View>> {
8888
}
8989
}
9090

91-
_LIBCPP_HIDE_FROM_ABI constexpr auto size()
91+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size()
9292
requires sized_range<_View>
9393
{
9494
return ranges::size(__base_);
9595
}
9696

97-
_LIBCPP_HIDE_FROM_ABI constexpr auto size() const
97+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
9898
requires sized_range<const _View>
9999
{
100100
return ranges::size(__base_);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++23
10+
11+
// Check that functions are marked [[nodiscard]]
12+
13+
#include <ranges>
14+
#include <utility>
15+
16+
#include "test_range.h"
17+
18+
struct NonSimpleView : std::ranges::view_base {
19+
int* begin() const;
20+
int* end() const;
21+
22+
const int* begin();
23+
const int* end();
24+
25+
constexpr std::size_t size() { return 0; };
26+
};
27+
static_assert(!simple_view<NonSimpleView>);
28+
29+
void test() {
30+
NonSimpleView range;
31+
32+
auto v = std::views::as_rvalue(range);
33+
34+
// [range.as.rvalue.view]
35+
36+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
37+
v.base();
38+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
39+
std::move(v).base();
40+
41+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
42+
v.begin();
43+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
44+
std::as_const(v).begin();
45+
46+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
47+
v.end();
48+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
49+
std::as_const(v).end();
50+
51+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
52+
v.size();
53+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
54+
std::as_const(v).size();
55+
56+
// [range.as.rvalue.overview]
57+
58+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
59+
std::views::as_rvalue(range);
60+
61+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
62+
std::views::as_rvalue(v);
63+
}

0 commit comments

Comments
 (0)