Skip to content

Commit ed1dba4

Browse files
H-G-Hristovaadeshps-mcw
authored andcommitted
[libc++][any] Applied [[nodiscard]] (llvm#168826)
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
1 parent 127f024 commit ed1dba4

File tree

7 files changed

+78
-33
lines changed

7 files changed

+78
-33
lines changed

libcxx/include/any

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ public:
262262
_LIBCPP_HIDE_FROM_ABI void swap(any& __rhs) _NOEXCEPT;
263263

264264
// 6.3.4 any observers
265-
_LIBCPP_HIDE_FROM_ABI bool has_value() const _NOEXCEPT { return __h_ != nullptr; }
265+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool has_value() const _NOEXCEPT { return __h_ != nullptr; }
266266

267267
# if _LIBCPP_HAS_RTTI
268-
_LIBCPP_HIDE_FROM_ABI const type_info& type() const _NOEXCEPT {
268+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI const type_info& type() const _NOEXCEPT {
269269
if (__h_) {
270270
return *static_cast<type_info const*>(this->__call(_Action::_TypeInfo));
271271
} else {
@@ -492,17 +492,17 @@ inline _LIBCPP_HIDE_FROM_ABI void any::swap(any& __rhs) _NOEXCEPT {
492492
inline _LIBCPP_HIDE_FROM_ABI void swap(any& __lhs, any& __rhs) _NOEXCEPT { __lhs.swap(__rhs); }
493493

494494
template <class _Tp, class... _Args>
495-
inline _LIBCPP_HIDE_FROM_ABI any make_any(_Args&&... __args) {
495+
[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI any make_any(_Args&&... __args) {
496496
return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
497497
}
498498

499499
template <class _Tp, class _Up, class... _Args>
500-
inline _LIBCPP_HIDE_FROM_ABI any make_any(initializer_list<_Up> __il, _Args&&... __args) {
500+
[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI any make_any(initializer_list<_Up> __il, _Args&&... __args) {
501501
return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
502502
}
503503

504504
template <class _ValueType>
505-
inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any const& __v) {
505+
[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any const& __v) {
506506
using _RawValueType = __remove_cvref_t<_ValueType>;
507507
static_assert(is_constructible<_ValueType, _RawValueType const&>::value,
508508
"ValueType is required to be a const lvalue reference "
@@ -514,7 +514,7 @@ inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any const& __v) {
514514
}
515515

516516
template <class _ValueType>
517-
inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any& __v) {
517+
[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any& __v) {
518518
using _RawValueType = __remove_cvref_t<_ValueType>;
519519
static_assert(is_constructible<_ValueType, _RawValueType&>::value,
520520
"ValueType is required to be an lvalue reference "
@@ -526,7 +526,7 @@ inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any& __v) {
526526
}
527527

528528
template <class _ValueType>
529-
inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any&& __v) {
529+
[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any&& __v) {
530530
using _RawValueType = __remove_cvref_t<_ValueType>;
531531
static_assert(is_constructible<_ValueType, _RawValueType>::value,
532532
"ValueType is required to be an rvalue reference "
@@ -538,7 +538,7 @@ inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any&& __v) {
538538
}
539539

540540
template <class _ValueType>
541-
inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) _NOEXCEPT {
541+
[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) _NOEXCEPT {
542542
static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
543543
static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
544544
return std::any_cast<_ValueType>(const_cast<any*>(__any));
@@ -555,7 +555,7 @@ inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void*, /*IsFunction
555555
}
556556

557557
template <class _ValueType>
558-
_LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) _NOEXCEPT {
558+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) _NOEXCEPT {
559559
using __any_imp::_Action;
560560
static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
561561
static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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++17
10+
11+
// Check that functions are marked [[nodiscard]]
12+
13+
#include <any>
14+
#include <utility>
15+
#include <vector>
16+
17+
#include "test_macros.h"
18+
19+
void test() {
20+
std::any a{94};
21+
22+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
23+
a.has_value();
24+
#if !defined(TEST_HAS_NO_RTTI)
25+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
26+
a.type();
27+
#endif
28+
29+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
std::make_any<int>(82);
31+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
32+
std::make_any<std::vector<int>>({94, 82, 50});
33+
34+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
35+
std::any_cast<const int&>(std::as_const(a));
36+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
37+
std::any_cast<int&>(a);
38+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
39+
std::any_cast<int&&>(std::move(a));
40+
41+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
42+
std::any_cast<int*>(&a);
43+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
44+
std::any_cast<const int*>(&std::as_const(a));
45+
}

libcxx/test/std/utilities/any/any.nonmembers/any.cast/any_cast_request_invalid_value_category.verify.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,31 @@ void test_const_lvalue_cast_request_non_const_lvalue() {
2323
const std::any a;
2424
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
2525
// expected-error@any:* {{drops 'const' qualifier}}
26-
std::any_cast<TestType&>(a); // expected-note {{requested here}}
26+
(void)std::any_cast<TestType&>(a); // expected-note {{requested here}}
2727

2828
const std::any a2(42);
2929
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
3030
// expected-error@any:* {{drops 'const' qualifier}}
31-
std::any_cast<int&>(a2); // expected-note {{requested here}}
31+
(void)std::any_cast<int&>(a2); // expected-note {{requested here}}
3232
}
3333

3434
void test_lvalue_any_cast_request_rvalue() {
3535
std::any a;
3636
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
37-
std::any_cast<TestType&&>(a); // expected-note {{requested here}}
37+
(void)std::any_cast<TestType&&>(a); // expected-note {{requested here}}
3838

3939
std::any a2(42);
4040
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
41-
std::any_cast<int&&>(a2); // expected-note {{requested here}}
41+
(void)std::any_cast<int&&>(a2); // expected-note {{requested here}}
4242
}
4343

4444
void test_rvalue_any_cast_request_lvalue() {
4545
std::any a;
4646
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
4747
// expected-error@any:* {{non-const lvalue reference to type 'TestType' cannot bind to a temporary}}
48-
std::any_cast<TestType&>(std::move(a)); // expected-note {{requested here}}
48+
(void)std::any_cast<TestType&>(std::move(a)); // expected-note {{requested here}}
4949

5050
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
5151
// expected-error@any:* {{non-const lvalue reference to type 'int' cannot bind to a temporary}}
52-
std::any_cast<int&>(42);
52+
(void)std::any_cast<int&>(42);
5353
}

libcxx/test/std/utilities/any/any.nonmembers/any.cast/const_correctness.verify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ void test() {
3131
std::any a;
3232

3333
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
34-
std::any_cast<TestType&>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
34+
(void)std::any_cast<TestType&>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
3535

3636
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
37-
std::any_cast<TestType&&>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
37+
(void)std::any_cast<TestType&&>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
3838

3939
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
40-
std::any_cast<TestType2&>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
40+
(void)std::any_cast<TestType2&>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
4141

4242
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
43-
std::any_cast<TestType2&&>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
43+
(void)std::any_cast<TestType2&&>(static_cast<std::any const&&>(a)); // expected-note {{requested here}}
4444
}

libcxx/test/std/utilities/any/any.nonmembers/any.cast/not_copy_constructible.verify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ struct no_move {
4545
void test() {
4646
std::any a;
4747
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an lvalue reference or a CopyConstructible type}}
48-
std::any_cast<no_copy>(static_cast<std::any&>(a)); // expected-note {{requested here}}
48+
(void)std::any_cast<no_copy>(static_cast<std::any&>(a)); // expected-note {{requested here}}
4949

5050
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be a const lvalue reference or a CopyConstructible type}}
51-
std::any_cast<no_copy>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
51+
(void)std::any_cast<no_copy>(static_cast<std::any const&>(a)); // expected-note {{requested here}}
5252

53-
std::any_cast<no_copy>(static_cast<std::any&&>(a)); // OK
53+
(void)std::any_cast<no_copy>(static_cast<std::any&&>(a)); // OK
5454

5555
// expected-error-re@any:* {{static assertion failed{{.*}}ValueType is required to be an rvalue reference or a CopyConstructible type}}
56-
std::any_cast<no_move>(static_cast<std::any&&>(a));
56+
(void)std::any_cast<no_move>(static_cast<std::any&&>(a));
5757
}

libcxx/test/std/utilities/any/any.nonmembers/any.cast/reference_types.verify.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@ void test() {
2222
std::any a = 1;
2323

2424
// expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
25-
std::any_cast<int&>(&a); // expected-note {{requested here}}
25+
(void)std::any_cast<int&>(&a); // expected-note {{requested here}}
2626

2727
// expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
28-
std::any_cast<int&&>(&a); // expected-note {{requested here}}
28+
(void)std::any_cast<int&&>(&a); // expected-note {{requested here}}
2929

3030
// expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
31-
std::any_cast<int const&>(&a); // expected-note {{requested here}}
31+
(void)std::any_cast<int const&>(&a); // expected-note {{requested here}}
3232

3333
// expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
34-
std::any_cast<int const&&>(&a); // expected-note {{requested here}}
34+
(void)std::any_cast<int const&&>(&a); // expected-note {{requested here}}
3535

3636
const std::any& a2 = a;
3737

3838
// expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
39-
std::any_cast<int&>(&a2); // expected-note {{requested here}}
39+
(void)std::any_cast<int&>(&a2); // expected-note {{requested here}}
4040

4141
// expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
42-
std::any_cast<int&&>(&a2); // expected-note {{requested here}}
42+
(void)std::any_cast<int&&>(&a2); // expected-note {{requested here}}
4343

4444
// expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
45-
std::any_cast<int const&>(&a2); // expected-note {{requested here}}
45+
(void)std::any_cast<int const&>(&a2); // expected-note {{requested here}}
4646

4747
// expected-error-re@any:* 1 {{static assertion failed{{.*}}_ValueType may not be a reference.}}
48-
std::any_cast<int const&&>(&a2); // expected-note {{requested here}}
48+
(void)std::any_cast<int const&&>(&a2); // expected-note {{requested here}}
4949
}

libcxx/test/std/utilities/any/any.nonmembers/any.cast/void.verify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ void test() {
2323
const std::any ca = 1;
2424

2525
// expected-error-re@any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
26-
std::any_cast<void>(&ca); // expected-note {{requested here}}
26+
(void)std::any_cast<void>(&ca); // expected-note {{requested here}}
2727
}
2828
{
2929
std::any a = 1;
3030

3131
// expected-error-re@any:* {{static assertion failed{{.*}}_ValueType may not be void.}}
32-
std::any_cast<void>(&a); // expected-note {{requested here}}
32+
(void)std::any_cast<void>(&a); // expected-note {{requested here}}
3333
}
3434
}

0 commit comments

Comments
 (0)