From cd8c7920b3662aaa4fb37552f7402d4260f15f79 Mon Sep 17 00:00:00 2001 From: Simon Truscott <85295933+bobbleclank@users.noreply.github.com> Date: Fri, 25 Nov 2022 00:17:42 +1100 Subject: [PATCH 1/2] Use move construction in swap implementation (#103) * Use correct type alias in swap helper function This was not an issue since both t_is_nothrow_move_constructible and e_is_nothrow_move_constructible are type aliases for std::true_type. * Use move instead of copy in swap implementation --- include/tl/expected.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/tl/expected.hpp b/include/tl/expected.hpp index b892923..90e41b3 100644 --- a/include/tl/expected.hpp +++ b/include/tl/expected.hpp @@ -1837,12 +1837,12 @@ private: void swap_where_only_one_has_value_and_t_is_not_void( expected &rhs, move_constructing_t_can_throw, - t_is_nothrow_move_constructible) { + e_is_nothrow_move_constructible) { auto temp = std::move(rhs.err()); rhs.err().~unexpected_type(); #ifdef TL_EXPECTED_EXCEPTIONS_ENABLED try { - ::new (rhs.valptr()) T(val()); + ::new (rhs.valptr()) T(std::move(val())); val().~T(); ::new (errptr()) unexpected_type(std::move(temp)); std::swap(this->m_has_val, rhs.m_has_val); @@ -1851,7 +1851,7 @@ private: throw; } #else - ::new (rhs.valptr()) T(val()); + ::new (rhs.valptr()) T(std::move(val())); val().~T(); ::new (errptr()) unexpected_type(std::move(temp)); std::swap(this->m_has_val, rhs.m_has_val); From 2675ce3cd49f08b269b4a6c611957f3658700b76 Mon Sep 17 00:00:00 2001 From: Simon Truscott <85295933+bobbleclank@users.noreply.github.com> Date: Fri, 25 Nov 2022 00:18:39 +1100 Subject: [PATCH 2/2] Fix warnings in test (#92) * Convert tabs to spaces * Remove extra semicolons in test * Fix warnings in test -Wreturn-type -Wunused-parameter -Wunused-value * Fix -Wunused-variable warning in test The variable 'failptr' is unused. From looking at the other variables in the test case, it seems the fix is to first remove 'failptr' and then rename 'efail' to 'failptr'. * Fix -Wunused-variable warning in test * Fix -Wmacro-redefined warning in test STATIC_REQUIRE has a previous definition in catch2/catch.hpp. --- include/tl/expected.hpp | 6 +++--- tests/bases.cpp | 6 +++--- tests/extensions.cpp | 23 ++++++++++++----------- tests/issues.cpp | 6 +++--- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/include/tl/expected.hpp b/include/tl/expected.hpp index 90e41b3..65fce1f 100644 --- a/include/tl/expected.hpp +++ b/include/tl/expected.hpp @@ -825,7 +825,7 @@ struct expected_operations_base : expected_storage_base { if (rhs.m_has_val) { get() = std::forward(rhs).get(); } else { - destroy_val(); + destroy_val(); construct_error(std::forward(rhs).geterr()); } } else { @@ -858,7 +858,7 @@ struct expected_operations_base : expected_storage_base { #endif TL_EXPECTED_11_CONSTEXPR void destroy_val() { - get().~T(); + get().~T(); } }; @@ -913,7 +913,7 @@ struct expected_operations_base : expected_storage_base { #endif TL_EXPECTED_11_CONSTEXPR void destroy_val() { - //no-op + //no-op } }; diff --git a/tests/bases.cpp b/tests/bases.cpp index 7189307..fe8d6aa 100644 --- a/tests/bases.cpp +++ b/tests/bases.cpp @@ -38,9 +38,9 @@ TEST_CASE("Triviality", "[bases.triviality]") { { struct T { T(const T&){} - T(T&&) {}; - T& operator=(const T&) {} - T& operator=(T&&) {}; + T(T&&) {} + T& operator=(const T&) { return *this; } + T& operator=(T&&) { return *this; } ~T(){} }; REQUIRE(!std::is_trivially_copy_constructible>::value); diff --git a/tests/extensions.cpp b/tests/extensions.cpp index 9670f90..1dfa63b 100644 --- a/tests/extensions.cpp +++ b/tests/extensions.cpp @@ -3,13 +3,15 @@ #define TOKENPASTE(x, y) x##y #define TOKENPASTE2(x, y) TOKENPASTE(x, y) +#undef STATIC_REQUIRE #define STATIC_REQUIRE(e) \ constexpr bool TOKENPASTE2(rqure, __LINE__) = e; \ + (void)TOKENPASTE2(rqure, __LINE__); \ REQUIRE(e); TEST_CASE("Map extensions", "[extensions.map]") { auto mul2 = [](int a) { return a * 2; }; - auto ret_void = [](int a) {}; + auto ret_void = [](int a) { (void)a; }; { tl::expected e = 21; @@ -143,7 +145,7 @@ TEST_CASE("Map extensions", "[extensions.map]") { TEST_CASE("Map error extensions", "[extensions.map_error]") { auto mul2 = [](int a) { return a * 2; }; - auto ret_void = [](int a) {}; + auto ret_void = [](int a) { (void)a; }; { tl::expected e = 21; @@ -252,8 +254,8 @@ TEST_CASE("Map error extensions", "[extensions.map_error]") { } TEST_CASE("And then extensions", "[extensions.and_then]") { - auto succeed = [](int a) { return tl::expected(21 * 2); }; - auto fail = [](int a) { return tl::expected(tl::unexpect, 17); }; + auto succeed = [](int a) { (void)a; return tl::expected(21 * 2); }; + auto fail = [](int a) { (void)a; return tl::expected(tl::unexpect, 17); }; { tl::expected e = 21; @@ -370,11 +372,10 @@ TEST_CASE("And then extensions", "[extensions.and_then]") { TEST_CASE("or_else", "[extensions.or_else]") { using eptr = std::unique_ptr; - auto succeed = [](int a) { return tl::expected(21 * 2); }; - auto succeedptr = [](eptr e) { return tl::expected(21*2);}; - auto fail = [](int a) { return tl::expected(tl::unexpect, 17);}; - auto efail = [](eptr e) { *e = 17;return tl::expected(tl::unexpect, std::move(e));}; - auto failptr = [](eptr e) { return tl::expected(tl::unexpect, std::move(e));}; + auto succeed = [](int a) { (void)a; return tl::expected(21 * 2); }; + auto succeedptr = [](eptr e) { (void)e; return tl::expected(21*2);}; + auto fail = [](int a) { (void)a; return tl::expected(tl::unexpect, 17);}; + auto failptr = [](eptr e) { *e = 17;return tl::expected(tl::unexpect, std::move(e));}; auto failvoid = [](int) {}; auto failvoidptr = [](const eptr&) { /* don't consume */}; auto consumeptr = [](eptr) {}; @@ -439,7 +440,7 @@ TEST_CASE("or_else", "[extensions.or_else]") { { tl::expected e = 21; - auto ret = std::move(e).or_else(efail); + auto ret = std::move(e).or_else(failptr); REQUIRE(ret); REQUIRE(ret == 21); } @@ -569,7 +570,7 @@ TEST_CASE("14", "[issue.14]") { auto res = tl::expected{tl::unexpect, F{}}; res.map_error([](F f) { - + (void)f; }); } diff --git a/tests/issues.cpp b/tests/issues.cpp index 25db813..7db7d7b 100644 --- a/tests/issues.cpp +++ b/tests/issues.cpp @@ -15,7 +15,7 @@ TEST_CASE("Issue 1", "[issues.1]") { getInt1(); } tl::expected operation1() { return 42; } -tl::expected operation2(int const val) { return "Bananas"; } +tl::expected operation2(int const val) { (void)val; return "Bananas"; } TEST_CASE("Issue 17", "[issues.17]") { auto const intermediate_result = operation1(); @@ -67,7 +67,7 @@ struct i31{ }; TEST_CASE("Issue 31", "[issues.31]") { const tl::expected a = i31{42}; - a->i; + (void)a->i; tl::expected< void, std::string > result; tl::expected< void, std::string > result2 = result; @@ -77,7 +77,7 @@ TEST_CASE("Issue 31", "[issues.31]") { TEST_CASE("Issue 33", "[issues.33]") { tl::expected res {tl::unexpect, 0}; REQUIRE(!res); - res = res.map_error([](int i) { return 42; }); + res = res.map_error([](int i) { (void)i; return 42; }); REQUIRE(res.error() == 42); }