Merge bitcoin/bitcoin#25248: refactor: Add LIFETIMEBOUND / -Wdangling-gsl to Assert()

fa3ea81c3e7d962715788ab5525958a532c51414 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 5766fff92d..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 fa3ea81c3e7d962715788ab5525958a532c51414
  theuni:
    ACK fa3ea81c3e7d962715788ab5525958a532c51414

Tree-SHA512: 17dea4d75f2ee2bf6e1b6a6f6d8f439711c777df0390574e8d8edb6ac9ee807a135341e4439050bd6a15ecc4097a1ba9a7ab15d27541ebf70a4e081fa6871877
This commit is contained in:
fanquake 2022-11-03 10:16:27 +00:00 committed by pasta
parent 459425776c
commit 5f78859562
No known key found for this signature in database
GPG Key ID: E2F3D7916E722D38
2 changed files with 8 additions and 2 deletions

View File

@ -123,6 +123,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)

View File

@ -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(
@ -57,7 +58,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