diff options
author | MacroFake <falke.marco@gmail.com> | 2022-05-30 16:10:32 +0200 |
---|---|---|
committer | MacroFake <falke.marco@gmail.com> | 2022-08-02 13:40:42 +0200 |
commit | fa3ea81c3e7d962715788ab5525958a532c51414 (patch) | |
tree | ffccc0675b1d06226189652773c9feb2ec3ac73b /src | |
parent | 816ca01650f4cc66a61ac2f9b0f8b74cd9cd0cf8 (diff) |
refactor: Add LIFETIMEBOUND / -Wdangling-gsl to Assert()
Diffstat (limited to 'src')
-rw-r--r-- | src/test/util_tests.cpp | 5 | ||||
-rw-r--r-- | src/util/check.h | 5 |
2 files changed, 8 insertions, 2 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index fda56ccff7..5766fff92d 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -122,6 +122,11 @@ BOOST_AUTO_TEST_CASE(util_check) // Check nested Asserts BOOST_CHECK_EQUAL(Assert((Assert(x).test() ? 3 : 0)), 3); + + // Check -Wdangling-gsl does not trigger when copying the int. (It would + // trigger on "const int&") + const int nine{*Assert(std::optional<int>{9})}; + BOOST_CHECK_EQUAL(9, nine); } BOOST_AUTO_TEST_CASE(util_criticalsection) diff --git a/src/util/check.h b/src/util/check.h index aca957925a..49f07de9dd 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -9,6 +9,7 @@ #include <config/bitcoin-config.h> #endif +#include <attributes.h> #include <tinyformat.h> #include <stdexcept> @@ -24,7 +25,7 @@ class NonFatalCheckError : public std::runtime_error /** Helper for CHECK_NONFATAL() */ template <typename T> -T&& inline_check_non_fatal(T&& val, const char* file, int line, const char* func, const char* assertion) +T&& inline_check_non_fatal(LIFETIMEBOUND T&& val, const char* file, int line, const char* func, const char* assertion) { if (!(val)) { throw NonFatalCheckError( @@ -56,7 +57,7 @@ void assertion_fail(const char* file, int line, const char* func, const char* as /** Helper for Assert()/Assume() */ template <bool IS_ASSERT, typename T> -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) +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) { if constexpr (IS_ASSERT #ifdef ABORT_ON_FAILED_ASSUME |