Skip to content

Commit 416db44

Browse files
committed
[libc++][optional] 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/optional
1 parent 38678a9 commit 416db44

File tree

5 files changed

+212
-56
lines changed

5 files changed

+212
-56
lines changed

libcxx/include/optional

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public:
275275
_LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default;
276276
// Get the key function ~bad_optional_access() into the dylib
277277
~bad_optional_access() _NOEXCEPT override;
278-
const char* what() const _NOEXCEPT override;
278+
[[nodiscard]] const char* what() const _NOEXCEPT override;
279279
};
280280

281281
} // namespace std
@@ -378,7 +378,7 @@ struct __optional_storage_base : __optional_destruct_base<_Tp> {
378378
using value_type = _Tp;
379379
using __base::__base;
380380

381-
_LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; }
381+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; }
382382

383383
_LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() & noexcept { return this->__val_; }
384384
_LIBCPP_HIDE_FROM_ABI constexpr const value_type& __get() const& noexcept { return this->__val_; }
@@ -440,7 +440,7 @@ struct __optional_storage_base<_Tp, true> {
440440

441441
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; }
442442

443-
_LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; }
443+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return __value_ != nullptr; }
444444

445445
_LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const& noexcept { return *__value_; }
446446

@@ -634,7 +634,7 @@ public:
634634
# endif
635635

636636
// [optional.iterators], iterator support
637-
_LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept {
637+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept {
638638
auto& __derived_self = static_cast<optional<_Tp>&>(*this);
639639
auto __ptr = [&__derived_self]() {
640640
if constexpr (is_lvalue_reference_v<_Tp>) {
@@ -653,7 +653,7 @@ public:
653653
# endif
654654
}
655655

656-
_LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept {
656+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept {
657657
auto& __derived_self = static_cast<const optional<_Tp>&>(*this);
658658
auto* __ptr = [&__derived_self]() {
659659
if constexpr (is_lvalue_reference_v<_Tp>) {
@@ -672,10 +672,10 @@ public:
672672
# endif
673673
}
674674

675-
_LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept {
675+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept {
676676
return begin() + (static_cast<optional<_Tp>&>(*this).has_value() ? 1 : 0);
677677
}
678-
_LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept {
678+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept {
679679
return begin() + (static_cast<const optional<_Tp>&>(*this).has_value() ? 1 : 0);
680680
}
681681
# endif
@@ -946,22 +946,22 @@ public:
946946
return std::addressof(this->__get());
947947
}
948948

949-
_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
949+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
950950
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
951951
return this->__get();
952952
}
953953

954-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
954+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
955955
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
956956
return this->__get();
957957
}
958958

959-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
959+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
960960
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
961961
return std::move(this->__get());
962962
}
963963

964-
_LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
964+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
965965
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
966966
return std::move(this->__get());
967967
}
@@ -971,25 +971,25 @@ public:
971971
using __base::__get;
972972
using __base::has_value;
973973

974-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const& {
974+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const& {
975975
if (!this->has_value())
976976
std::__throw_bad_optional_access();
977977
return this->__get();
978978
}
979979

980-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
980+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
981981
if (!this->has_value())
982982
std::__throw_bad_optional_access();
983983
return this->__get();
984984
}
985985

986-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
986+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
987987
if (!this->has_value())
988988
std::__throw_bad_optional_access();
989989
return std::move(this->__get());
990990
}
991991

992-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&& {
992+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&& {
993993
if (!this->has_value())
994994
std::__throw_bad_optional_access();
995995
return std::move(this->__get());
@@ -1000,7 +1000,7 @@ public:
10001000
requires(!(is_lvalue_reference_v<_Tp> && is_function_v<__libcpp_remove_reference_t<_Tp>>) &&
10011001
!(is_lvalue_reference_v<_Tp> && is_array_v<__libcpp_remove_reference_t<_Tp>>))
10021002
# endif
1003-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
1003+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
10041004
static_assert(is_copy_constructible_v<_Tp>, "optional<T>::value_or: T must be copy constructible");
10051005
static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
10061006
return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));
@@ -1010,7 +1010,7 @@ public:
10101010
# if _LIBCPP_STD_VER >= 26
10111011
requires(!is_lvalue_reference_v<_Tp>)
10121012
# endif
1013-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
1013+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
10141014
static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");
10151015
static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
10161016
return this->has_value() ? std::move(this->__get()) : static_cast<_Tp>(std::forward<_Up>(__v));
@@ -1020,7 +1020,7 @@ public:
10201020
template <class _Up = remove_cv_t<_Tp>>
10211021
requires(is_lvalue_reference_v<_Tp> &&
10221022
!(is_function_v<__libcpp_remove_reference_t<_Tp>> || is_array_v<__libcpp_remove_reference_t<_Tp>>))
1023-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
1023+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
10241024
static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");
10251025
static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
10261026
return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));
@@ -1029,7 +1029,7 @@ public:
10291029

10301030
# if _LIBCPP_STD_VER >= 23
10311031
template <class _Func>
1032-
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
1032+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
10331033
using _Up = invoke_result_t<_Func, _Tp&>;
10341034
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10351035
"Result of f(value()) must be a specialization of std::optional");
@@ -1039,7 +1039,7 @@ public:
10391039
}
10401040

10411041
template <class _Func>
1042-
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
1042+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
10431043
using _Up = invoke_result_t<_Func, const _Tp&>;
10441044
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10451045
"Result of f(value()) must be a specialization of std::optional");
@@ -1049,7 +1049,7 @@ public:
10491049
}
10501050

10511051
template <class _Func>
1052-
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
1052+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
10531053
using _Up = invoke_result_t<_Func, _Tp&&>;
10541054
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10551055
"Result of f(std::move(value())) must be a specialization of std::optional");
@@ -1059,7 +1059,7 @@ public:
10591059
}
10601060

10611061
template <class _Func>
1062-
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
1062+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
10631063
using _Up = invoke_result_t<_Func, const _Tp&&>;
10641064
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
10651065
"Result of f(std::move(value())) must be a specialization of std::optional");
@@ -1069,7 +1069,7 @@ public:
10691069
}
10701070

10711071
template <class _Func>
1072-
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1072+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
10731073
using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;
10741074
static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
10751075
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
@@ -1081,7 +1081,7 @@ public:
10811081
}
10821082

10831083
template <class _Func>
1084-
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1084+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
10851085
using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;
10861086
static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
10871087
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
@@ -1093,7 +1093,7 @@ public:
10931093
}
10941094

10951095
template <class _Func>
1096-
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1096+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
10971097
using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;
10981098
static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
10991099
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
@@ -1105,7 +1105,7 @@ public:
11051105
}
11061106

11071107
template <class _Func>
1108-
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1108+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
11091109
using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>;
11101110
static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
11111111
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
@@ -1117,7 +1117,7 @@ public:
11171117
}
11181118

11191119
template <invocable _Func>
1120-
_LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
1120+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
11211121
requires is_copy_constructible_v<_Tp>
11221122
{
11231123
static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
@@ -1128,7 +1128,7 @@ public:
11281128
}
11291129

11301130
template <invocable _Func>
1131-
_LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
1131+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
11321132
requires is_move_constructible_v<_Tp>
11331133
{
11341134
static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
@@ -1426,8 +1426,8 @@ operator<=>(const optional<_Tp>& __x, const _Up& __v) {
14261426
# endif // _LIBCPP_STD_VER >= 20
14271427

14281428
template <class _Tp, enable_if_t< is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, int> = 0>
1429-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
1430-
swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
1429+
inline _LIBCPP_HIDE_FROM_ABI
1430+
_LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
14311431
__x.swap(__y);
14321432
}
14331433

@@ -1440,17 +1440,18 @@ template <
14401440
__make_optional_barrier_tag = __make_optional_barrier_tag{},
14411441
# endif
14421442
class _Tp>
1443-
_LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
1443+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
14441444
return optional<decay_t<_Tp>>(std::forward<_Tp>(__v));
14451445
}
14461446

14471447
template <class _Tp, class... _Args>
1448-
_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
1448+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
14491449
return optional<_Tp>(in_place, std::forward<_Args>(__args)...);
14501450
}
14511451

14521452
template <class _Tp, class _Up, class... _Args>
1453-
_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) {
1453+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp>
1454+
make_optional(initializer_list<_Up> __il, _Args&&... __args) {
14541455
return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);
14551456
}
14561457

@@ -1461,7 +1462,7 @@ struct hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > {
14611462
_LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
14621463
# endif
14631464

1464-
_LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {
1465+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {
14651466
return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
14661467
}
14671468
};

0 commit comments

Comments
 (0)