Skip to content

Commit f5d97e7

Browse files
committed
[libc++][variant] 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 - https://wg21.link/variant
1 parent 80ec43d commit f5d97e7

File tree

2 files changed

+93
-17
lines changed

2 files changed

+93
-17
lines changed

libcxx/include/variant

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
288288

289289
class _LIBCPP_EXPORTED_FROM_ABI bad_variant_access : public exception {
290290
public:
291-
const char* what() const _NOEXCEPT override;
291+
[[__nodiscard__]] const char* what() const _NOEXCEPT override;
292292
};
293293

294294
_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
@@ -1282,11 +1282,11 @@ public:
12821282
return __impl_.template __emplace<_Ip>(__il, std::forward<_Args>(__args)...);
12831283
}
12841284

1285-
_LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
1285+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool valueless_by_exception() const noexcept {
12861286
return __impl_.valueless_by_exception();
12871287
}
12881288

1289-
_LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
1289+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t index() const noexcept { return __impl_.index(); }
12901290

12911291
template < bool _Dummy = true,
12921292
enable_if_t< __all<(__dependent_type<is_move_constructible<_Types>, _Dummy>::value &&
@@ -1329,7 +1329,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr bool __holds_alternative(const variant<_Types...
13291329
}
13301330

13311331
template <class _Tp, class... _Types>
1332-
_LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
1332+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool holds_alternative(const variant<_Types...>& __v) noexcept {
13331333
return std::__holds_alternative<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13341334
}
13351335

@@ -1343,55 +1343,57 @@ _LIBCPP_HIDE_FROM_ABI constexpr auto&& __generic_get(_Vp&& __v) {
13431343
}
13441344

13451345
template <size_t _Ip, class... _Types>
1346-
_LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>& get(variant<_Types...>& __v) {
1346+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&
1347+
get(variant<_Types...>& __v) {
13471348
static_assert(_Ip < sizeof...(_Types));
13481349
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
13491350
return std::__generic_get<_Ip>(__v);
13501351
}
13511352

13521353
template <size_t _Ip, class... _Types>
1353-
_LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&& get(variant<_Types...>&& __v) {
1354+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
1355+
get(variant<_Types...>&& __v) {
13541356
static_assert(_Ip < sizeof...(_Types));
13551357
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
13561358
return std::__generic_get<_Ip>(std::move(__v));
13571359
}
13581360

13591361
template <size_t _Ip, class... _Types>
1360-
_LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
1362+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
13611363
get(const variant<_Types...>& __v) {
13621364
static_assert(_Ip < sizeof...(_Types));
13631365
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
13641366
return std::__generic_get<_Ip>(__v);
13651367
}
13661368

13671369
template <size_t _Ip, class... _Types>
1368-
_LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
1370+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
13691371
get(const variant<_Types...>&& __v) {
13701372
static_assert(_Ip < sizeof...(_Types));
13711373
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
13721374
return std::__generic_get<_Ip>(std::move(__v));
13731375
}
13741376

13751377
template <class _Tp, class... _Types>
1376-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp& get(variant<_Types...>& __v) {
1378+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& get(variant<_Types...>& __v) {
13771379
static_assert(!is_void_v<_Tp>);
13781380
return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13791381
}
13801382

13811383
template <class _Tp, class... _Types>
1382-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& get(variant<_Types...>&& __v) {
1384+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& get(variant<_Types...>&& __v) {
13831385
static_assert(!is_void_v<_Tp>);
13841386
return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
13851387
}
13861388

13871389
template <class _Tp, class... _Types>
1388-
_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& get(const variant<_Types...>& __v) {
1390+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& get(const variant<_Types...>& __v) {
13891391
static_assert(!is_void_v<_Tp>);
13901392
return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
13911393
}
13921394

13931395
template <class _Tp, class... _Types>
1394-
_LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& get(const variant<_Types...>&& __v) {
1396+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& get(const variant<_Types...>&& __v) {
13951397
static_assert(!is_void_v<_Tp>);
13961398
return std::get<__find_exactly_one_t<_Tp, _Types...>::value>(std::move(__v));
13971399
}
@@ -1403,29 +1405,29 @@ _LIBCPP_HIDE_FROM_ABI constexpr auto* __generic_get_if(_Vp* __v) noexcept {
14031405
}
14041406

14051407
template <size_t _Ip, class... _Types>
1406-
_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
1408+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<variant_alternative_t<_Ip, variant<_Types...>>>
14071409
get_if(variant<_Types...>* __v) noexcept {
14081410
static_assert(_Ip < sizeof...(_Types));
14091411
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
14101412
return std::__generic_get_if<_Ip>(__v);
14111413
}
14121414

14131415
template <size_t _Ip, class... _Types>
1414-
_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
1416+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const variant_alternative_t<_Ip, variant<_Types...>>>
14151417
get_if(const variant<_Types...>* __v) noexcept {
14161418
static_assert(_Ip < sizeof...(_Types));
14171419
static_assert(!is_void_v<variant_alternative_t<_Ip, variant<_Types...>>>);
14181420
return std::__generic_get_if<_Ip>(__v);
14191421
}
14201422

14211423
template <class _Tp, class... _Types>
1422-
_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
1424+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> get_if(variant<_Types...>* __v) noexcept {
14231425
static_assert(!is_void_v<_Tp>);
14241426
return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
14251427
}
14261428

14271429
template <class _Tp, class... _Types>
1428-
_LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
1430+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<const _Tp> get_if(const variant<_Types...>* __v) noexcept {
14291431
static_assert(!is_void_v<_Tp>);
14301432
return std::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
14311433
}
@@ -1606,7 +1608,7 @@ struct hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>
16061608
using result_type _LIBCPP_DEPRECATED_IN_CXX17 = size_t;
16071609
# endif
16081610

1609-
_LIBCPP_HIDE_FROM_ABI size_t operator()(const variant<_Types...>& __v) const {
1611+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const variant<_Types...>& __v) const {
16101612
using __variant_detail::__visitation::__variant;
16111613
size_t __res =
16121614
__v.valueless_by_exception()
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 <variant>
14+
15+
#include <test_macros.h>
16+
17+
void test() {
18+
{
19+
std::bad_variant_access ex;
20+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
21+
22+
ex.what();
23+
}
24+
25+
{
26+
struct First {};
27+
struct Second {};
28+
29+
std::variant<First, Second> v;
30+
const std::variant<First, Second> cv;
31+
32+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
33+
cv.valueless_by_exception();
34+
35+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
36+
cv.index();
37+
38+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
39+
std::holds_alternative<First>(v);
40+
41+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
42+
std::get<0>(v);
43+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
44+
std::get<0>(cv);
45+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
46+
std::get<0>(std::move(v));
47+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
48+
std::get<0>(std::move(cv));
49+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
50+
std::get<First>(v);
51+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
52+
std::get<First>(cv);
53+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
54+
std::get<First>(std::move(v));
55+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
56+
std::get<First>(std::move(cv));
57+
58+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
59+
std::get_if<0>(&v);
60+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
61+
std::get_if<0>(&cv);
62+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
63+
std::get_if<First>(&v);
64+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
65+
std::get_if<First>(&cv);
66+
}
67+
68+
{
69+
std::hash< std::variant<int, float>> hash;
70+
71+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
72+
hash(std::variant<int, float>{});
73+
}
74+
}

0 commit comments

Comments
 (0)