Skip to content

Commit fa3ea81

Browse files
author
MacroFake
committed
refactor: Add LIFETIMEBOUND / -Wdangling-gsl to Assert()
1 parent 816ca01 commit fa3ea81

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/test/util_tests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ BOOST_AUTO_TEST_CASE(util_check)
122122

123123
// Check nested Asserts
124124
BOOST_CHECK_EQUAL(Assert((Assert(x).test() ? 3 : 0)), 3);
125+
126+
// Check -Wdangling-gsl does not trigger when copying the int. (It would
127+
// trigger on "const int&")
128+
const int nine{*Assert(std::optional<int>{9})};
129+
BOOST_CHECK_EQUAL(9, nine);
125130
}
126131

127132
BOOST_AUTO_TEST_CASE(util_criticalsection)

src/util/check.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <config/bitcoin-config.h>
1010
#endif
1111

12+
#include <attributes.h>
1213
#include <tinyformat.h>
1314

1415
#include <stdexcept>
@@ -24,7 +25,7 @@ class NonFatalCheckError : public std::runtime_error
2425

2526
/** Helper for CHECK_NONFATAL() */
2627
template <typename T>
27-
T&& inline_check_non_fatal(T&& val, const char* file, int line, const char* func, const char* assertion)
28+
T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion)
2829
{
2930
if (!(val)) {
3031
throw NonFatalCheckError(
@@ -56,7 +57,7 @@ void assertion_fail(const char* file, int line, const char* func, const char* as
5657

5758
/** Helper for Assert()/Assume() */
5859
template <bool IS_ASSERT, typename T>
59-
T&& inline_assertion_check(T&& val, [[maybe_unused]] const char* file, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* assertion)
60+
T&& inline_assertion_check(LIFETIMEBOUND T&& val, [[maybe_unused]] const char* file, [[maybe_unused]] int line, [[maybe_unused]] const char* func, [[maybe_unused]] const char* assertion)
6061
{
6162
if constexpr (IS_ASSERT
6263
#ifdef ABORT_ON_FAILED_ASSUME

0 commit comments

Comments
 (0)