Skip to content

Commit 63ea393

Browse files
[libc++][optional] Applied [[nodiscard]] (#170045)
`[[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 --------- Co-authored-by: William Tran-Viet <wtranviet@proton.me>
1 parent c3e7a1a commit 63ea393

File tree

5 files changed

+195
-56
lines changed

5 files changed

+195
-56
lines changed

libcxx/include/optional

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public:
340340
_LIBCPP_HIDE_FROM_ABI bad_optional_access& operator=(const bad_optional_access&) _NOEXCEPT = default;
341341
// Get the key function ~bad_optional_access() into the dylib
342342
~bad_optional_access() _NOEXCEPT override;
343-
const char* what() const _NOEXCEPT override;
343+
[[__nodiscard__]] const char* what() const _NOEXCEPT override;
344344
};
345345

346346
} // namespace std
@@ -443,7 +443,7 @@ struct __optional_storage_base : __optional_destruct_base<_Tp> {
443443
using value_type = _Tp;
444444
using __base::__base;
445445

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

448448
_LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() & noexcept { return this->__val_; }
449449
_LIBCPP_HIDE_FROM_ABI constexpr const value_type& __get() const& noexcept { return this->__val_; }
@@ -505,7 +505,7 @@ struct __optional_storage_base<_Tp, true> {
505505

506506
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept { __value_ = nullptr; }
507507

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

510510
_LIBCPP_HIDE_FROM_ABI constexpr value_type& __get() const& noexcept { return *__value_; }
511511

@@ -699,7 +699,7 @@ public:
699699
# endif
700700

701701
// [optional.iterators], iterator support
702-
_LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept {
702+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator begin() noexcept {
703703
auto& __derived_self = static_cast<optional<_Tp>&>(*this);
704704
auto __ptr = [&__derived_self]() {
705705
if constexpr (is_lvalue_reference_v<_Tp>) {
@@ -718,7 +718,7 @@ public:
718718
# endif
719719
}
720720

721-
_LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept {
721+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept {
722722
auto& __derived_self = static_cast<const optional<_Tp>&>(*this);
723723
auto* __ptr = [&__derived_self]() {
724724
if constexpr (is_lvalue_reference_v<_Tp>) {
@@ -737,10 +737,10 @@ public:
737737
# endif
738738
}
739739

740-
_LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept {
740+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr iterator end() noexcept {
741741
return begin() + (static_cast<optional<_Tp>&>(*this).has_value() ? 1 : 0);
742742
}
743-
_LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept {
743+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept {
744744
return begin() + (static_cast<const optional<_Tp>&>(*this).has_value() ? 1 : 0);
745745
}
746746
# endif
@@ -1011,22 +1011,22 @@ public:
10111011
return std::addressof(this->__get());
10121012
}
10131013

1014-
_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
1014+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
10151015
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
10161016
return this->__get();
10171017
}
10181018

1019-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
1019+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
10201020
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
10211021
return this->__get();
10221022
}
10231023

1024-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
1024+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
10251025
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
10261026
return std::move(this->__get());
10271027
}
10281028

1029-
_LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
1029+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
10301030
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
10311031
return std::move(this->__get());
10321032
}
@@ -1036,25 +1036,25 @@ public:
10361036
using __base::__get;
10371037
using __base::has_value;
10381038

1039-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const& {
1039+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const& {
10401040
if (!this->has_value())
10411041
std::__throw_bad_optional_access();
10421042
return this->__get();
10431043
}
10441044

1045-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
1045+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
10461046
if (!this->has_value())
10471047
std::__throw_bad_optional_access();
10481048
return this->__get();
10491049
}
10501050

1051-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
1051+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
10521052
if (!this->has_value())
10531053
std::__throw_bad_optional_access();
10541054
return std::move(this->__get());
10551055
}
10561056

1057-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&& {
1057+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&& {
10581058
if (!this->has_value())
10591059
std::__throw_bad_optional_access();
10601060
return std::move(this->__get());
@@ -1065,7 +1065,7 @@ public:
10651065
requires(!(is_lvalue_reference_v<_Tp> && is_function_v<__libcpp_remove_reference_t<_Tp>>) &&
10661066
!(is_lvalue_reference_v<_Tp> && is_array_v<__libcpp_remove_reference_t<_Tp>>))
10671067
# endif
1068-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
1068+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
10691069
static_assert(is_copy_constructible_v<_Tp>, "optional<T>::value_or: T must be copy constructible");
10701070
static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
10711071
return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));
@@ -1075,7 +1075,7 @@ public:
10751075
# if _LIBCPP_STD_VER >= 26
10761076
requires(!is_lvalue_reference_v<_Tp>)
10771077
# endif
1078-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
1078+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
10791079
static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");
10801080
static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
10811081
return this->has_value() ? std::move(this->__get()) : static_cast<_Tp>(std::forward<_Up>(__v));
@@ -1085,7 +1085,7 @@ public:
10851085
template <class _Up = remove_cv_t<_Tp>>
10861086
requires(is_lvalue_reference_v<_Tp> &&
10871087
!(is_function_v<__libcpp_remove_reference_t<_Tp>> || is_array_v<__libcpp_remove_reference_t<_Tp>>))
1088-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
1088+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
10891089
static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");
10901090
static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
10911091
return this->has_value() ? this->__get() : static_cast<_Tp>(std::forward<_Up>(__v));
@@ -1094,7 +1094,7 @@ public:
10941094

10951095
# if _LIBCPP_STD_VER >= 23
10961096
template <class _Func>
1097-
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
1097+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
10981098
using _Up = invoke_result_t<_Func, _Tp&>;
10991099
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
11001100
"Result of f(value()) must be a specialization of std::optional");
@@ -1104,7 +1104,7 @@ public:
11041104
}
11051105

11061106
template <class _Func>
1107-
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
1107+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
11081108
using _Up = invoke_result_t<_Func, const _Tp&>;
11091109
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
11101110
"Result of f(value()) must be a specialization of std::optional");
@@ -1114,7 +1114,7 @@ public:
11141114
}
11151115

11161116
template <class _Func>
1117-
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
1117+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
11181118
using _Up = invoke_result_t<_Func, _Tp&&>;
11191119
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
11201120
"Result of f(std::move(value())) must be a specialization of std::optional");
@@ -1124,7 +1124,7 @@ public:
11241124
}
11251125

11261126
template <class _Func>
1127-
_LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
1127+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
11281128
using _Up = invoke_result_t<_Func, const _Tp&&>;
11291129
static_assert(__is_std_optional<remove_cvref_t<_Up>>::value,
11301130
"Result of f(std::move(value())) must be a specialization of std::optional");
@@ -1134,7 +1134,7 @@ public:
11341134
}
11351135

11361136
template <class _Func>
1137-
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
1137+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
11381138
using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;
11391139
static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
11401140
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
@@ -1146,7 +1146,7 @@ public:
11461146
}
11471147

11481148
template <class _Func>
1149-
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
1149+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
11501150
using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;
11511151
static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
11521152
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
@@ -1158,7 +1158,7 @@ public:
11581158
}
11591159

11601160
template <class _Func>
1161-
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
1161+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
11621162
using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;
11631163
static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
11641164
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
@@ -1170,7 +1170,7 @@ public:
11701170
}
11711171

11721172
template <class _Func>
1173-
_LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
1173+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
11741174
using _Up = remove_cvref_t<invoke_result_t<_Func, const _Tp&&>>;
11751175
static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
11761176
static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
@@ -1182,7 +1182,7 @@ public:
11821182
}
11831183

11841184
template <invocable _Func>
1185-
_LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
1185+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
11861186
requires is_copy_constructible_v<_Tp>
11871187
{
11881188
static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
@@ -1193,7 +1193,7 @@ public:
11931193
}
11941194

11951195
template <invocable _Func>
1196-
_LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
1196+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
11971197
requires is_move_constructible_v<_Tp>
11981198
{
11991199
static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
@@ -1491,8 +1491,8 @@ operator<=>(const optional<_Tp>& __x, const _Up& __v) {
14911491
# endif // _LIBCPP_STD_VER >= 20
14921492

14931493
template <class _Tp, enable_if_t< is_move_constructible_v<_Tp> && is_swappable_v<_Tp>, int> = 0>
1494-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
1495-
swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
1494+
inline _LIBCPP_HIDE_FROM_ABI
1495+
_LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
14961496
__x.swap(__y);
14971497
}
14981498

@@ -1505,17 +1505,18 @@ template <
15051505
__make_optional_barrier_tag = __make_optional_barrier_tag{},
15061506
# endif
15071507
class _Tp>
1508-
_LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
1508+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
15091509
return optional<decay_t<_Tp>>(std::forward<_Tp>(__v));
15101510
}
15111511

15121512
template <class _Tp, class... _Args>
1513-
_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
1513+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
15141514
return optional<_Tp>(in_place, std::forward<_Args>(__args)...);
15151515
}
15161516

15171517
template <class _Tp, class _Up, class... _Args>
1518-
_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) {
1518+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp>
1519+
make_optional(initializer_list<_Up> __il, _Args&&... __args) {
15191520
return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);
15201521
}
15211522

@@ -1526,7 +1527,7 @@ struct hash< __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>> > {
15261527
_LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
15271528
# endif
15281529

1529-
_LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {
1530+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI size_t operator()(const optional<_Tp>& __opt) const {
15301531
return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
15311532
}
15321533
};

0 commit comments

Comments
 (0)