Skip to content

Commit 7643c27

Browse files
Mark expected, unexpected, and ALL exception types as [[nodiscard]] (#5174)
1 parent ae9d115 commit 7643c27

File tree

19 files changed

+50
-44
lines changed

19 files changed

+50
-44
lines changed

stl/inc/__msvc_ranges_tuple_formatter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ _NODISCARD constexpr const _CharT* _Choose_literal(const char* const _Str, const
7878
// It's defined here, so that both headers can use this definition.
7979
#define _STATICALLY_WIDEN(_CharT, _Literal) (_Choose_literal<_CharT>(_Literal, L##_Literal))
8080

81-
_EXPORT_STD class format_error : public runtime_error {
81+
_EXPORT_STD class _NODISCARD format_error : public runtime_error {
8282
using runtime_error::runtime_error;
8383
};
8484

stl/inc/any

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ _STL_DISABLE_CLANG_WARNINGS
2828

2929
_STD_BEGIN
3030

31-
_EXPORT_STD class bad_any_cast : public bad_cast { // thrown by failed any_cast
31+
_EXPORT_STD class _NODISCARD bad_any_cast : public bad_cast { // thrown by failed any_cast
3232
public:
3333
_NODISCARD const char* __CLR_OR_THIS_CALL what() const noexcept override {
3434
return "Bad any_cast";

stl/inc/chrono

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ namespace chrono {
17201720
sys_info second;
17211721
};
17221722

1723-
_EXPORT_STD class nonexistent_local_time : public runtime_error {
1723+
_EXPORT_STD class _NODISCARD nonexistent_local_time : public runtime_error {
17241724
public:
17251725
template <class _Duration>
17261726
nonexistent_local_time(const local_time<_Duration>& _Tp, const local_info& _Info)
@@ -1736,7 +1736,7 @@ namespace chrono {
17361736
_THROW(nonexistent_local_time{_Tp, _Info});
17371737
}
17381738

1739-
_EXPORT_STD class ambiguous_local_time : public runtime_error {
1739+
_EXPORT_STD class _NODISCARD ambiguous_local_time : public runtime_error {
17401740
public:
17411741
template <class _Duration>
17421742
ambiguous_local_time(const local_time<_Duration>& _Tp, const local_info& _Info)

stl/inc/exception

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ _STD_END
6969
#undef stdext
7070

7171
_STDEXT_BEGIN
72-
class exception;
72+
class _NODISCARD exception;
7373
_STDEXT_END
7474

7575
_STD_BEGIN
@@ -83,7 +83,7 @@ extern _CRTIMP2_PURE_IMPORT _Prhand _Raise_handler; // pointer to raise handler
8383
_STD_END
8484

8585
_STDEXT_BEGIN
86-
class exception { // base of all library exceptions
86+
class _NODISCARD exception { // base of all library exceptions
8787
public:
8888
static _STD _Prhand _Set_raise_handler(_STD _Prhand _Pnew) { // register a handler for _Raise calls
8989
const _STD _Prhand _Pold = _STD _Raise_handler;
@@ -123,7 +123,7 @@ protected:
123123
const char* _Ptr; // the message pointer
124124
};
125125

126-
class bad_exception : public exception { // base of all bad exceptions
126+
class _NODISCARD bad_exception : public exception { // base of all bad exceptions
127127
public:
128128
__CLR_OR_THIS_CALL bad_exception(const char* _Message = "bad exception") noexcept : exception(_Message) {}
129129

@@ -135,9 +135,9 @@ protected:
135135
}
136136
};
137137

138-
class bad_array_new_length;
138+
class _NODISCARD bad_array_new_length;
139139

140-
class bad_alloc : public exception { // base of all bad allocation exceptions
140+
class _NODISCARD bad_alloc : public exception { // base of all bad allocation exceptions
141141
public:
142142
__CLR_OR_THIS_CALL bad_alloc() noexcept
143143
: exception("bad allocation", 1) {} // construct from message string with no memory allocation
@@ -156,7 +156,7 @@ protected:
156156
}
157157
};
158158

159-
class bad_array_new_length : public bad_alloc {
159+
class _NODISCARD bad_array_new_length : public bad_alloc {
160160
public:
161161
bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
162162
};
@@ -390,8 +390,8 @@ _EXPORT_STD template <class _Ty>
390390
void rethrow_if_nested(const _Ty&) = delete; // requires /GR option
391391
#endif // ^^^ !defined(_CPPRTTI) ^^^
392392

393-
_EXPORT_STD class bad_variant_access
394-
: public exception { // exception for visit of a valueless variant or get<I> on a variant with index() != I
393+
_EXPORT_STD class _NODISCARD bad_variant_access : public exception {
394+
// exception for visit of a valueless variant or get<I> on a variant with index() != I
395395
public:
396396
bad_variant_access() noexcept = default;
397397

stl/inc/execution

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void _Implicitly_construct_in_place_by_binary_op_transform_deref_rhs(
161161
_Ty([&]() -> _Ty { return _Reduce_op(_STD forward<_LeftTy>(_Left), _Transform_op(*_Iter)); }());
162162
}
163163

164-
struct _Parallelism_resources_exhausted : exception {
164+
struct _NODISCARD _Parallelism_resources_exhausted : exception {
165165
_NODISCARD const char* __CLR_OR_THIS_CALL what() const noexcept override {
166166
// return pointer to message string
167167
return "Insufficient resources were available to use additional parallelism.";

stl/inc/expected

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ _STL_DISABLE_CLANG_WARNINGS
2525
_STD_BEGIN
2626

2727
_EXPORT_STD template <class _Err>
28-
class unexpected;
28+
class _NODISCARD unexpected;
2929

3030
template <class _Err>
3131
struct _Check_unexpected_argument : true_type {
@@ -39,7 +39,7 @@ struct _Check_unexpected_argument : true_type {
3939

4040
// [expected.un.general]
4141
_EXPORT_STD template <class _Err>
42-
class unexpected {
42+
class _NODISCARD unexpected {
4343
static_assert(_Check_unexpected_argument<_Err>::value);
4444

4545
template <class _Ty, class _Err2>
@@ -107,10 +107,10 @@ template <class _Err>
107107
unexpected(_Err) -> unexpected<_Err>;
108108

109109
_EXPORT_STD template <class _Err>
110-
class bad_expected_access;
110+
class _NODISCARD bad_expected_access;
111111

112112
template <>
113-
class bad_expected_access<void> : public exception {
113+
class _NODISCARD bad_expected_access<void> : public exception {
114114
public:
115115
_NODISCARD const char* __CLR_OR_THIS_CALL what() const noexcept override {
116116
return "Bad expected access";
@@ -131,7 +131,7 @@ protected:
131131
};
132132

133133
_EXPORT_STD template <class _Err>
134-
class bad_expected_access : public bad_expected_access<void> {
134+
class _NODISCARD bad_expected_access : public bad_expected_access<void> {
135135
public:
136136
explicit bad_expected_access(_Err _Unex) noexcept(is_nothrow_move_constructible_v<_Err>) // strengthened
137137
: _Unexpected(_STD move(_Unex)) {}
@@ -205,7 +205,7 @@ concept _Trivially_move_constructible_assignable_destructible =
205205
&& is_trivially_destructible_v<_Type>;
206206

207207
_EXPORT_STD template <class _Ty, class _Err>
208-
class expected {
208+
class _NODISCARD expected {
209209
private:
210210
static_assert(_Check_expected_argument<_Ty>::value);
211211
static_assert(_Check_unexpected_argument<_Err>::value);
@@ -1210,7 +1210,7 @@ concept _Expected_unary_move_assignable = is_move_assignable_v<_Err> && is_move_
12101210

12111211
template <class _Ty, class _Err>
12121212
requires is_void_v<_Ty>
1213-
class expected<_Ty, _Err> {
1213+
class _NODISCARD expected<_Ty, _Err> {
12141214
private:
12151215
static_assert(_Check_unexpected_argument<_Err>::value);
12161216

stl/inc/experimental/filesystem

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ _NODISCARD path u8path(const basic_string<char, _Traits, _Alloc>& _Str) { // mak
12841284
return path{_Path_cvt<_Char8_t, _Pchar>::_Cvt(_Str_out, _Str.c_str(), _Str.size())};
12851285
}
12861286

1287-
class filesystem_error : public system_error { // base of all filesystem-error exceptions
1287+
class _NODISCARD filesystem_error : public system_error { // base of all filesystem-error exceptions
12881288
public:
12891289
explicit filesystem_error(
12901290
const string& _Message, error_code _Errcode = make_error_code(errc::operation_not_permitted))

stl/inc/filesystem

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,7 @@ namespace filesystem {
17901790
return iterator(_Text.cend(), this);
17911791
}
17921792

1793-
_EXPORT_STD class filesystem_error : public system_error { // base of all filesystem-error exceptions
1793+
_EXPORT_STD class _NODISCARD filesystem_error : public system_error { // base of all filesystem-error exceptions
17941794
public:
17951795
filesystem_error(const string& _Message, const error_code _Errcode)
17961796
: system_error(_Errcode, _Message), _What(runtime_error::what()) {}

stl/inc/functional

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ _NODISCARD _CONSTEXPR20 _Not_fn<decay_t<_Callable>> not_fn(_Callable&& _Obj)
690690
}
691691
#endif // _HAS_CXX17
692692

693-
_EXPORT_STD class bad_function_call : public exception { // exception thrown when an empty std::function is called
693+
_EXPORT_STD class _NODISCARD bad_function_call : public exception {
694+
// exception thrown when an empty std::function is called
694695
public:
695696
bad_function_call() noexcept {}
696697

stl/inc/future

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ _NODISCARD inline const char* _Future_error_map(int _Errcode) noexcept { // conv
113113
}
114114
}
115115

116-
_EXPORT_STD class future_error : public logic_error { // future exception
116+
_EXPORT_STD class _NODISCARD future_error : public logic_error { // future exception
117117
public:
118118
explicit future_error(error_code _Errcode) // internal, TRANSITION, will be removed
119119
: logic_error(""), _Mycode(_Errcode) {}

0 commit comments

Comments
 (0)