Skip to content

Commit 789845e

Browse files
[libc++] Mini-cleanup for [[nodiscard]] (#172275)
1. Remove incorrect `[[nodiscard]]` from compound assignment operators in `<__filesystem/copy_options.h>`. Also add regression tests. 2. Add missing `[[nodiscard]]` mark for `mdspan::size` in `<__mdspan/mdspan.h>` and test it. 3. Enable verifying `[[nodiscard]]` in C++03 for various components. These components are either present in C++03 or backported by libc++ from C++11/17.
1 parent 14a23d8 commit 789845e

23 files changed

+111
-66
lines changed

libcxx/include/__filesystem/copy_options.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ enum class copy_options : unsigned short {
5050
return static_cast<copy_options>(~static_cast<unsigned short>(__lhs));
5151
}
5252

53-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline copy_options& operator&=(copy_options& __lhs, copy_options __rhs) {
53+
_LIBCPP_HIDE_FROM_ABI inline copy_options& operator&=(copy_options& __lhs, copy_options __rhs) {
5454
return __lhs = __lhs & __rhs;
5555
}
5656

57-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline copy_options& operator|=(copy_options& __lhs, copy_options __rhs) {
57+
_LIBCPP_HIDE_FROM_ABI inline copy_options& operator|=(copy_options& __lhs, copy_options __rhs) {
5858
return __lhs = __lhs | __rhs;
5959
}
6060

61-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline copy_options& operator^=(copy_options& __lhs, copy_options __rhs) {
61+
_LIBCPP_HIDE_FROM_ABI inline copy_options& operator^=(copy_options& __lhs, copy_options __rhs) {
6262
return __lhs = __lhs ^ __rhs;
6363
}
6464

libcxx/include/__mdspan/mdspan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class mdspan {
214214
}(make_index_sequence<rank()>()));
215215
}
216216

217-
_LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept {
217+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_type size() const noexcept {
218218
// Could leave this as only checked in debug mode: semantically size() is never
219219
// guaranteed to be related to any accessible range
220220
_LIBCPP_ASSERT_UNCATEGORIZED(

libcxx/test/libcxx/containers/views/mdspan/mdspan/assert.size.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main(int, char**) {
4343
assert(map.required_span_size() == static_cast<signed char>(12));
4444
// 100 x 3 exceeds 256
4545
{
46-
TEST_LIBCPP_ASSERT_FAILURE(([=] { mds.size(); }()), "mdspan: size() is not representable as size_type");
46+
TEST_LIBCPP_ASSERT_FAILURE(([=] { (void)mds.size(); }()), "mdspan: size() is not representable as size_type");
4747
}
4848
}
4949
return 0;

libcxx/test/libcxx/containers/views/mdspan/nodiscard.verify.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ void test() {
3333
mdsp.static_extent(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
3434
mdsp.extent(0); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
3535

36+
mdsp.size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
37+
3638
mdsp.extents(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
3739
mdsp.data_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
3840
mdsp.mapping(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}

libcxx/test/libcxx/diagnostics/algorithm.nodiscard.verify.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03
10-
119
// check that <algorithm> functions are marked [[nodiscard]]
1210

1311
// clang-format off
@@ -188,11 +186,13 @@ void test() {
188186
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
189187
std::max(1, 2, std::greater<int>());
190188

189+
#if TEST_STD_VER >= 11
191190
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
192191
std::max({1, 2, 3});
193192

194193
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
195194
std::max({1, 2, 3}, std::greater<int>());
195+
#endif
196196

197197
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
198198
std::min_element(std::begin(arr), std::end(arr));
@@ -206,11 +206,13 @@ void test() {
206206
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
207207
std::min(1, 2, std::greater<int>());
208208

209+
#if TEST_STD_VER >= 11
209210
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
210211
std::min({1, 2, 3});
211212

212213
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
213214
std::min({1, 2, 3}, std::greater<int>());
215+
#endif
214216

215217
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
216218
std::minmax_element(std::begin(arr), std::end(arr));
@@ -224,11 +226,13 @@ void test() {
224226
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
225227
std::minmax(1, 2, std::greater<int>());
226228

229+
#if TEST_STD_VER >= 11
227230
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
228231
std::minmax({1, 2, 3});
229232

230233
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
231234
std::minmax({1, 2, 3}, std::greater<int>());
235+
#endif
232236

233237
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
234238
std::mismatch(std::begin(arr), std::end(arr), std::begin(arr));

libcxx/test/libcxx/diagnostics/array.nodiscard.verify.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03
10-
119
// check that <array> functions are marked [[nodiscard]]
1210

1311
#include <array>
@@ -18,7 +16,7 @@
1816
template <std::size_t N>
1917
void test_members() {
2018
std::array<int, N> a;
21-
const std::array<int, N> ca{};
19+
const std::array<int, N> ca = {};
2220

2321
a.begin(); // expected-warning 2 {{ignoring return value of function declared with 'nodiscard' attribute}}
2422
ca.begin(); // expected-warning 2 {{ignoring return value of function declared with 'nodiscard' attribute}}
@@ -57,7 +55,7 @@ void test_members() {
5755

5856
template <typename ArrT>
5957
void test_get() {
60-
std::array<int, 94> a{};
58+
std::array<int, 94> a = {};
6159

6260
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
6361
std::get<0>(a);

libcxx/test/libcxx/diagnostics/cstdlib.nodiscard.verify.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03
10-
119
// We don't control the implementation of the stdlib.h functions on windows
1210
// UNSUPPORTED: windows
1311

libcxx/test/libcxx/diagnostics/deque.nodiscard.verify.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03
10-
119
// check that <deque> functions are marked [[nodiscard]]
1210

1311
#include <deque>

libcxx/test/libcxx/diagnostics/forward_list.nodiscard.verify.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03
10-
119
// check that <forward_list> functions are marked [[nodiscard]]
1210

1311
#include <forward_list>

libcxx/test/libcxx/diagnostics/functional.nodiscard.verify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03
10-
119
// check that <functional> functions are marked [[nodiscard]]
1210

1311
#include <cstddef>
@@ -20,7 +18,7 @@ void test() {
2018

2119
// Function wrappers
2220

23-
#if !defined(TEST_HAS_NO_RTTI)
21+
#if TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_RTTI)
2422
std::function<void(int)> f;
2523
const std::function<void(int)> cf;
2624

@@ -48,14 +46,16 @@ void test() {
4846
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
4947
std::bind_front([](int a) { return a; }, 94);
5048
#endif
49+
#if TEST_STD_VER >= 11
5150
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
5251
std::bind([](int a) { return a; }, 94);
5352
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
5453
std::bind<float>([](int a) { return a; }, 94);
54+
#endif
5555

5656
// Reference wrappers
5757

58-
std::reference_wrapper<int> rw{i};
58+
std::reference_wrapper<int> rw = i;
5959
rw.get(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
6060

6161
std::ref(i); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}

0 commit comments

Comments
 (0)