Skip to content

Commit 2aa4096

Browse files
authored
Add [[nodiscard]] to tl::expected (#165)
* test: Silence "non-void function does not return a value" * Add [[nodiscard]] to tl::expected Microsoft STL added [[nodiscard]] on 2024-12-13; see microsoft/STL#5174 martinmoene/expected-lite added [[nodiscard]] on 2024-12-10; see martinmoene/expected-lite#74
1 parent 17d0058 commit 2aa4096

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

include/tl/expected.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ struct is_trivially_copy_constructible<std::vector<T, A>> : std::false_type {};
133133
#endif
134134

135135
namespace tl {
136-
template <class T, class E> class expected;
136+
template <class T, class E> class [[nodiscard]] expected;
137137

138138
#ifndef TL_MONOSTATE_INPLACE_MUTEX
139139
#define TL_MONOSTATE_INPLACE_MUTEX
@@ -1276,7 +1276,8 @@ template <class E> class bad_expected_access : public std::exception {
12761276
/// has been destroyed. The initialization state of the contained object is
12771277
/// tracked by the expected object.
12781278
template <class T, class E>
1279-
class expected : private detail::expected_move_assign_base<T, E>,
1279+
class [[nodiscard]] expected :
1280+
private detail::expected_move_assign_base<T, E>,
12801281
private detail::expected_delete_ctor_base<T, E>,
12811282
private detail::expected_delete_assign_base<T, E>,
12821283
private detail::expected_default_ctor_base<T, E> {

tests/extensions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,15 +814,15 @@ struct F {
814814
TEST_CASE("14", "[issue.14]") {
815815
auto res = tl::expected<S,F>{tl::unexpect, F{}};
816816

817-
res.map_error([](F f) {
817+
(void)res.map_error([](F f) {
818818
(void)f;
819819
});
820820
}
821821

822822
TEST_CASE("32", "[issue.32]") {
823823
int i = 0;
824824
tl::expected<void, int> a;
825-
a.map([&i]{i = 42;});
825+
(void)a.map([&i]{i = 42;});
826826
REQUIRE(i == 42);
827827

828828
auto x = a.map([]{return 42;});

tests/issues.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ tl::expected<int, string> getInt2(int val) { return val; }
1313

1414
tl::expected<int, string> getInt1() { return getInt2(5).and_then(getInt3); }
1515

16-
TEST_CASE("Issue 1", "[issues.1]") { getInt1(); }
16+
TEST_CASE("Issue 1", "[issues.1]") { (void)getInt1(); }
1717

1818
tl::expected<int, int> operation1() { return 42; }
1919

@@ -22,7 +22,7 @@ tl::expected<std::string, int> operation2(int const val) { (void)val; return "Ba
2222
TEST_CASE("Issue 17", "[issues.17]") {
2323
auto const intermediate_result = operation1();
2424

25-
intermediate_result.and_then(operation2);
25+
(void)intermediate_result.and_then(operation2);
2626
}
2727

2828
struct a {};
@@ -61,7 +61,7 @@ tl::expected<int, std::string> error() {
6161
std::string maperror(std::string s) { return s + "maperror "; }
6262

6363
TEST_CASE("Issue 30", "[issues.30]") {
64-
error().map_error(maperror);
64+
(void)error().map_error(maperror);
6565
}
6666

6767
struct i31{
@@ -91,7 +91,7 @@ void errorhandling(std::string){}
9191
TEST_CASE("Issue 34", "[issues.34]") {
9292
tl::expected <int, std::string> result = voidWork ()
9393
.and_then (work2);
94-
result.map_error ([&] (std::string result) {errorhandling (result);});
94+
(void)result.map_error ([&] (std::string result) {errorhandling (result);});
9595
}
9696

9797
struct non_copyable {
@@ -101,7 +101,7 @@ struct non_copyable {
101101
};
102102

103103
TEST_CASE("Issue 42", "[issues.42]") {
104-
tl::expected<non_copyable,int>{}.map([](non_copyable) {});
104+
(void)tl::expected<non_copyable,int>{}.map([](non_copyable) {});
105105
}
106106

107107
TEST_CASE("Issue 43", "[issues.43]") {
@@ -144,12 +144,12 @@ struct move_tracker {
144144

145145
move_tracker() = default;
146146

147-
move_tracker(move_tracker const &other) noexcept {};
147+
move_tracker(move_tracker const &other) noexcept;
148148
move_tracker(move_tracker &&orig) noexcept
149149
: moved(orig.moved + 1) {}
150150

151151
move_tracker &
152-
operator=(move_tracker const &other) noexcept {};
152+
operator=(move_tracker const &other) noexcept;
153153

154154
move_tracker &operator=(move_tracker &&orig) noexcept {
155155
moved = orig.moved + 1;
@@ -221,4 +221,4 @@ TEST_CASE("Issue 145", "[issues.145]") {
221221
tl::expected<MoveOnly, std::error_code> a{};
222222
tl::expected<MoveOnly, std::error_code> b = std::move(a);
223223
a = std::move(b);
224-
}
224+
}

0 commit comments

Comments
 (0)