Skip to content

Commit ee296e1

Browse files
authored
Merge pull request #26 from oold/nodiscard
Add nodiscard attribute
2 parents ae7f42b + d910a89 commit ee296e1

4 files changed

+84
-67
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
3939
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
4040
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wpedantic \
4141
-Wno-unused")
42+
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10)
43+
# earlier versions complain of unused "nodiscard" results in unevaluated contexts
44+
# (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89070)
45+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=unused-result")
46+
endif()
47+
4248
check_cxx_compiler_flag("-Wnoexcept-type" HAS_NOEXCEPT_TYPE_WARNING)
4349
if(HAS_NOEXCEPT_TYPE_WARNING)
4450
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-noexcept-type")

catch_tests.cpp

+42-37
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <functional>
1212
#include <list>
1313
#include <memory>
14+
#include <tuple>
1415
#include <type_traits>
1516
#include <utility>
1617

@@ -49,15 +50,16 @@ TEST_CASE("Showing that direct constructor is not desirable.")
4950
with functions is treated
5051
just like an lvalue... */
5152

52-
make_scope_guard(inc); // ... but the BEST is really to use the make function
53+
std::ignore = make_scope_guard(inc); // ... but the BEST is really to use the
54+
// make function
5355
}
5456

5557
/* --- Plain functions, lvalues, rvalues, plain references, consts --- */
5658

5759
////////////////////////////////////////////////////////////////////////////////
5860
TEST_CASE("A plain function can be used to create a scope_guard.")
5961
{
60-
make_scope_guard(inc);
62+
std::ignore = make_scope_guard(inc);
6163
}
6264

6365
////////////////////////////////////////////////////////////////////////////////
@@ -103,7 +105,7 @@ TEST_CASE("An lvalue reference to a plain function can be used to create a "
103105
"scope_guard.")
104106
{
105107
auto& inc_ref = inc;
106-
make_scope_guard(inc_ref);
108+
std::ignore = make_scope_guard(inc_ref);
107109
}
108110

109111
////////////////////////////////////////////////////////////////////////////////
@@ -153,7 +155,7 @@ TEST_CASE("An lvalue const reference to a plain function can be used to create "
153155
"a scope_guard.")
154156
{
155157
const auto& inc_ref = inc;
156-
make_scope_guard(inc_ref);
158+
std::ignore = make_scope_guard(inc_ref);
157159
}
158160

159161
////////////////////////////////////////////////////////////////////////////////
@@ -208,7 +210,7 @@ type, which is treated as lvalue reference. */
208210
TEST_CASE("An rvalue reference to a plain function can be used to create a "
209211
"scope_guard.")
210212
{
211-
make_scope_guard(std::move(inc)); // rvalue ref to function treated as lvalue
213+
std::ignore = make_scope_guard(std::move(inc)); // rvalue ref to function treated as lvalue
212214
}
213215

214216
////////////////////////////////////////////////////////////////////////////////
@@ -257,7 +259,7 @@ TEST_CASE("A dismissed rvalue-reference-to-plain-function-based scope_guard "
257259
TEST_CASE("A reference wrapper to a plain function can be used to create a "
258260
"scope_guard.")
259261
{
260-
make_scope_guard(std::ref(inc));
262+
std::ignore = make_scope_guard(std::ref(inc));
261263
}
262264

263265
////////////////////////////////////////////////////////////////////////////////
@@ -302,7 +304,7 @@ TEST_CASE("A dismissed reference-wrapper-to-plain-function-based scope_guard "
302304
TEST_CASE("A const reference wrapper to a plain function can be used to create "
303305
"a scope_guard.")
304306
{
305-
make_scope_guard(std::cref(inc));
307+
std::ignore = make_scope_guard(std::cref(inc));
306308
}
307309

308310
////////////////////////////////////////////////////////////////////////////////
@@ -351,7 +353,7 @@ TEST_CASE("An lvalue plain function pointer can be used to create a "
351353
"scope_guard.")
352354
{
353355
const auto fp = &inc;
354-
make_scope_guard(fp);
356+
std::ignore = make_scope_guard(fp);
355357
}
356358

357359
////////////////////////////////////////////////////////////////////////////////
@@ -399,7 +401,7 @@ TEST_CASE("A dismissed lvalue-plain-function-pointer-based scope_guard does "
399401
TEST_CASE("An rvalue plain function pointer can be used to create a "
400402
"scope_guard.")
401403
{
402-
make_scope_guard(&inc);
404+
std::ignore = make_scope_guard(&inc);
403405
}
404406

405407
////////////////////////////////////////////////////////////////////////////////
@@ -446,7 +448,7 @@ TEST_CASE("An lvalue reference to a plain function pointer can be used to "
446448
{
447449
const auto fp = &inc;
448450
const auto& fp_ref = fp;
449-
make_scope_guard(fp_ref);
451+
std::ignore = make_scope_guard(fp_ref);
450452
}
451453

452454
////////////////////////////////////////////////////////////////////////////////
@@ -498,7 +500,7 @@ TEST_CASE("An rvalue reference to a plain function pointer can be used to "
498500
"create a scope_guard.")
499501
{
500502
const auto fp = &inc;
501-
make_scope_guard(std::move(fp));
503+
std::ignore = make_scope_guard(std::move(fp));
502504
}
503505

504506
////////////////////////////////////////////////////////////////////////////////
@@ -579,7 +581,7 @@ TEST_CASE("An lvalue std::function that wraps a regular function can be used "
579581
"to create a scope_guard.")
580582
{
581583
const auto stdf = make_std_function(inc);
582-
make_scope_guard(stdf);
584+
std::ignore = make_scope_guard(stdf);
583585
}
584586

585587
////////////////////////////////////////////////////////////////////////////////
@@ -629,8 +631,8 @@ TEST_CASE("A dismissed scope_guard that was created with a "
629631
TEST_CASE("An rvalue std::function that wraps a regular function can be used "
630632
"to create a scope_guard.")
631633
{
632-
make_scope_guard(make_std_function(inc));
633-
make_scope_guard(std::function<void()>{inc});
634+
std::ignore = make_scope_guard(make_std_function(inc));
635+
std::ignore = make_scope_guard(std::function<void()>{inc});
634636
}
635637

636638
////////////////////////////////////////////////////////////////////////////////
@@ -680,7 +682,7 @@ TEST_CASE("An lvalue reference to a std::function that wraps a regular "
680682
{
681683
const auto stdf = make_std_function(inc);
682684
const auto& stdf_ref = stdf;
683-
make_scope_guard(stdf_ref);
685+
std::ignore = make_scope_guard(stdf_ref);
684686
}
685687

686688
////////////////////////////////////////////////////////////////////////////////
@@ -735,7 +737,7 @@ TEST_CASE("An rvalue reference to a std::function that wraps a regular "
735737
"function can be used to create a scope_guard.")
736738
{
737739
const auto stdf = make_std_function(inc);
738-
make_scope_guard(std::move(stdf));
740+
std::ignore = make_scope_guard(std::move(stdf));
739741
}
740742

741743
////////////////////////////////////////////////////////////////////////////////
@@ -795,7 +797,7 @@ namespace
795797
TEST_CASE("A lambda function with no capture can be used to create a "
796798
"scope_guard.")
797799
{
798-
make_scope_guard([]()noexcept{});
800+
std::ignore = make_scope_guard([]()noexcept{});
799801
}
800802

801803
////////////////////////////////////////////////////////////////////////////////
@@ -840,7 +842,8 @@ TEST_CASE("A lambda function with capture can be used to create a scope_guard.")
840842
{
841843
auto f = 0.0f;
842844
const auto i = -1;
843-
make_scope_guard([&f, i]()noexcept{ f = static_cast<float>(*&i); });
845+
std::ignore = make_scope_guard([&f, i]() noexcept
846+
{ f = static_cast<float>(*&i); });
844847
}
845848

846849
////////////////////////////////////////////////////////////////////////////////
@@ -891,7 +894,7 @@ TEST_CASE("A const lambda function with capture can be used to create a "
891894
auto f = 0.0f;
892895
const auto i = -1;
893896
const auto lambda = [&f, i]() noexcept { f = static_cast<float>(*&i); };
894-
make_scope_guard(lambda);
897+
std::ignore = make_scope_guard(lambda);
895898
}
896899

897900
////////////////////////////////////////////////////////////////////////////////
@@ -987,7 +990,7 @@ TEST_CASE("A dismissed scope_guard that was created with a "
987990
TEST_CASE("A lambda function calling a std::function can be used to create a "
988991
"scope_guard.")
989992
{
990-
make_scope_guard([]()noexcept{ make_std_function(inc)(); });
993+
std::ignore = make_scope_guard([]()noexcept{ make_std_function(inc)(); });
991994
}
992995

993996
////////////////////////////////////////////////////////////////////////////////
@@ -1019,7 +1022,7 @@ TEST_CASE("A scope_guard created with a std::function-calling lambda calls "
10191022
TEST_CASE("A std::function wrapping a lambda function can be used to create a "
10201023
"scope_guard.")
10211024
{
1022-
make_scope_guard(std::function<void()>([](){}));
1025+
std::ignore = make_scope_guard(std::function<void()>([](){}));
10231026
}
10241027

10251028
////////////////////////////////////////////////////////////////////////////////
@@ -1042,7 +1045,7 @@ TEST_CASE("A scope_guard created with a lambda-wrapping std::function calls "
10421045
TEST_CASE("A bound function can be used to create a scope_guard.")
10431046
{
10441047
auto boundf_count = 0u;
1045-
make_scope_guard(std::bind(incc, std::ref(boundf_count)));
1048+
std::ignore = make_scope_guard(std::bind(incc, std::ref(boundf_count)));
10461049
}
10471050

10481051
////////////////////////////////////////////////////////////////////////////////
@@ -1080,7 +1083,7 @@ TEST_CASE("A dismissed bound-function-based scope_guard does not execute its "
10801083
////////////////////////////////////////////////////////////////////////////////
10811084
TEST_CASE("A bound lambda can be used to create a scope_guard.")
10821085
{
1083-
make_scope_guard(std::bind([](int /*unused*/){}, 42));
1086+
std::ignore = make_scope_guard(std::bind([](int /*unused*/){}, 42));
10841087
}
10851088

10861089
////////////////////////////////////////////////////////////////////////////////
@@ -1135,7 +1138,7 @@ namespace
11351138
////////////////////////////////////////////////////////////////////////////////
11361139
TEST_CASE("A stateless custom functor can be used to create a scope_guard")
11371140
{
1138-
make_scope_guard(StatelessFunctor{});
1141+
std::ignore = make_scope_guard(StatelessFunctor{});
11391142
}
11401143

11411144
////////////////////////////////////////////////////////////////////////////////
@@ -1156,7 +1159,7 @@ TEST_CASE("A stateless-custom-functor-based scope_guard calls the functor "
11561159
TEST_CASE("A stateful custom functor can be used to create a scope_guard")
11571160
{
11581161
auto u = 123u;
1159-
make_scope_guard(StatefulFunctor{u});
1162+
std::ignore = make_scope_guard(StatefulFunctor{u});
11601163
}
11611164

11621165
////////////////////////////////////////////////////////////////////////////////
@@ -1177,7 +1180,7 @@ TEST_CASE("A stateful-custom-functor-based scope_guard calls the functor "
11771180
TEST_CASE("A const custom functor can be used to create a scope_guard")
11781181
{
11791182
const auto fun = StatelessFunctor{};
1180-
make_scope_guard(fun);
1183+
std::ignore = make_scope_guard(fun);
11811184
}
11821185

11831186
////////////////////////////////////////////////////////////////////////////////
@@ -1201,7 +1204,7 @@ TEST_CASE("An lvalue reference to a noncopyable and nonmovable functor can be "
12011204
{
12021205
nocopy_nomove ncnm{};
12031206
const auto& ncnm_ref = ncnm;
1204-
make_scope_guard(ncnm_ref);
1207+
std::ignore = make_scope_guard(ncnm_ref);
12051208
}
12061209

12071210
////////////////////////////////////////////////////////////////////////////////
@@ -1248,7 +1251,7 @@ TEST_CASE("An lvalue noncopyable and nonmovable functor can be used to create "
12481251
"a scope_guard, because it binds to an lvalue reference")
12491252
{
12501253
nocopy_nomove ncnm{};
1251-
make_scope_guard(ncnm);
1254+
std::ignore = make_scope_guard(ncnm);
12521255
}
12531256

12541257
////////////////////////////////////////////////////////////////////////////////
@@ -1273,7 +1276,7 @@ TEST_CASE("A const lvalue noncopyable and nonmovable functor can be used to "
12731276
"binds to a const lvalue reference")
12741277
{
12751278
const nocopy_nomove ncnm{};
1276-
make_scope_guard(ncnm);
1279+
std::ignore = make_scope_guard(ncnm);
12771280
}
12781281

12791282
////////////////////////////////////////////////////////////////////////////////
@@ -1383,7 +1386,7 @@ namespace
13831386
TEST_CASE("A lambda-wrapped regular method can be used to create a scope_guard")
13841387
{
13851388
regular_method_holder h{};
1386-
make_scope_guard([&h]() noexcept { h.regular_inc_method(); });
1389+
std::ignore = make_scope_guard([&h]() noexcept { h.regular_inc_method(); });
13871390
}
13881391

13891392
////////////////////////////////////////////////////////////////////////////////
@@ -1423,7 +1426,8 @@ TEST_CASE("A dismissed lambda-wrapped-regular-method-based scope_guard does "
14231426
TEST_CASE("A bound regular method can be used to create a scope_guard")
14241427
{
14251428
regular_method_holder h{};
1426-
make_scope_guard(std::bind(&regular_method_holder::regular_inc_method, h));
1429+
std::ignore = make_scope_guard(
1430+
std::bind(&regular_method_holder::regular_inc_method, h));
14271431
}
14281432

14291433
////////////////////////////////////////////////////////////////////////////////
@@ -1446,7 +1450,7 @@ TEST_CASE("A bound-regular-method-based scope_guard executes the method "
14461450
TEST_CASE("A lambda-wrapped const method can be used to create a scope_guard")
14471451
{
14481452
const const_method_holder h{};
1449-
make_scope_guard([&h]() noexcept { h.const_inc_method(); });
1453+
std::ignore = make_scope_guard([&h]() noexcept { h.const_inc_method(); });
14501454
}
14511455

14521456
////////////////////////////////////////////////////////////////////////////////
@@ -1469,7 +1473,8 @@ TEST_CASE("A lambda-wrapped-const-method-based scope_guard executes the "
14691473
TEST_CASE("A bound const method can be used to create a scope_guard")
14701474
{
14711475
const const_method_holder h{};
1472-
make_scope_guard(std::bind(&const_method_holder::const_inc_method, h));
1476+
std::ignore =
1477+
make_scope_guard(std::bind(&const_method_holder::const_inc_method, h));
14731478
}
14741479

14751480
////////////////////////////////////////////////////////////////////////////////
@@ -1509,7 +1514,7 @@ TEST_CASE("A dismissed, bound-const-method-based scope_guard does not execute "
15091514
////////////////////////////////////////////////////////////////////////////////
15101515
TEST_CASE("A static method can be used to create a scope_guard")
15111516
{
1512-
make_scope_guard(static_method_holder::static_inc_method);
1517+
std::ignore = make_scope_guard(static_method_holder::static_inc_method);
15131518
}
15141519

15151520
////////////////////////////////////////////////////////////////////////////////
@@ -1530,7 +1535,7 @@ TEST_CASE("A static-method-based scope_guard executes the static method "
15301535
TEST_CASE("A lambda-wrapped virtual method can be used to create a scope_guard")
15311536
{
15321537
virtual_method_holder h{};
1533-
make_scope_guard([&h]() noexcept { h.virtual_inc_method(); });
1538+
std::ignore = make_scope_guard([&h]() noexcept { h.virtual_inc_method(); });
15341539
}
15351540

15361541
////////////////////////////////////////////////////////////////////////////////
@@ -1640,14 +1645,14 @@ namespace
16401645
auto sfinae_tester_impl(T&& t, tag_prefered_overload&& /*ignored*/)
16411646
-> decltype(make_scope_guard(std::forward<T>(t)), std::declval<void>())
16421647
{
1643-
make_scope_guard(std::forward<T>(t));
1648+
std::ignore = make_scope_guard(std::forward<T>(t));
16441649
}
16451650

16461651
template<typename T>
16471652
void sfinae_tester_impl(T&& /*ignored*/,
16481653
... /* less specific, so 2nd choice */)
16491654
{
1650-
make_scope_guard(inc);
1655+
std::ignore = make_scope_guard(inc);
16511656
}
16521657

16531658
template<typename T>

0 commit comments

Comments
 (0)