Skip to content

Commit 1f4da8f

Browse files
committed
[libc++][any] Applied [[nodiscard]]
`[[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 13ed14f commit 1f4da8f

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
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+
}

0 commit comments

Comments
 (0)