Skip to content

Commit 2a7c998

Browse files
committed
Merge bitcoin/bitcoin#25248: refactor: Add LIFETIMEBOUND / -Wdangling-gsl to Assert()
fa3ea81 refactor: Add LIFETIMEBOUND / -Wdangling-gsl to Assert() (MacroFake) Pull request description: Currently compiles clean, but I think it may still be useful. Can be tested by adding an `&`: ```diff diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 5766fff..300c1ec60f 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(util_check) // Check -Wdangling-gsl does not trigger when copying the int. (It would // trigger on "const int&") - const int nine{*Assert(std::optional<int>{9})}; + const int& nine{*Assert(std::optional<int>{9})}; BOOST_CHECK_EQUAL(9, nine); } ``` Output: ``` test/util_tests.cpp:128:29: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl] const int& nine{*Assert(std::optional<int>{9})}; ^~~~~~~~~~~~~~~~~~~~~ ./util/check.h:75:50: note: expanded from macro 'Assert' #define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val) ^~~ 1 warning generated. ACKs for top commit: jonatack: ACK fa3ea81 theuni: ACK fa3ea81 Tree-SHA512: 17dea4d75f2ee2bf6e1b6a6f6d8f439711c777df0390574e8d8edb6ac9ee807a135341e4439050bd6a15ecc4097a1ba9a7ab15d27541ebf70a4e081fa6871877
2 parents 5274f32 + fa3ea81 commit 2a7c998

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
@@ -124,6 +124,11 @@ BOOST_AUTO_TEST_CASE(util_check)
124124

125125
// Check nested Asserts
126126
BOOST_CHECK_EQUAL(Assert((Assert(x).test() ? 3 : 0)), 3);
127+
128+
// Check -Wdangling-gsl does not trigger when copying the int. (It would
129+
// trigger on "const int&")
130+
const int nine{*Assert(std::optional<int>{9})};
131+
BOOST_CHECK_EQUAL(9, nine);
127132
}
128133

129134
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)