diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 2a45374..5d3e6e8 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -16,8 +16,6 @@ jobs: build: name: Build and analyze runs-on: ubuntu-latest - env: - BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory permissions: contents: read steps: @@ -71,4 +69,4 @@ jobs: uses: codecov/codecov-action@13ce06bfc6bbe3ecf90edbbf1bc32fe5978ca1d3 # v5.3.1 with: token: ${{ secrets.CODECOV_TOKEN }} - file: build/coverage/coverage.lcov + files: build/coverage/coverage.lcov diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index aa0e601..246e6cf 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,5 @@ if(ENABLE_MAINTAINER_MODE AND (CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)) - string(REPLACE " " ";" COMPILE_OPTIONS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_MM}") + string(REPLACE " " ";" COMPILE_OPTIONS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_MM} -Wno-exit-time-destructors") set_directory_properties(PROPERTIES COMPILE_OPTIONS "${COMPILE_OPTIONS}") unset(COMPILE_OPTIONS) endif() diff --git a/examples/scope_action.cpp b/examples/scope_action.cpp index f74b1ff..2cc605b 100644 --- a/examples/scope_action.cpp +++ b/examples/scope_action.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "scope_action.h" @@ -18,8 +19,11 @@ void print_exit_status(std::string_view name, bool exit_status, bool did_throw) void maybe_throw() { - // NOLINTNEXTLINE(concurrency-mt-unsafe, cert-msc50-cpp) - if (std::rand() >= RAND_MAX / 2) { + static std::random_device rd; + static std::mt19937 gen(rd()); + static std::uniform_int_distribution<> dis(0, RAND_MAX); + + if (dis(gen) >= RAND_MAX / 2) { throw std::exception{}; } } @@ -31,9 +35,6 @@ int main() bool exit_status = false; bool did_throw = false; - // NOLINTNEXTLINE(cert-msc51-cpp) - std::srand(static_cast(std::time(nullptr))); - // Manual handling at "end of scope" try { maybe_throw(); diff --git a/src/scope_action.h b/src/scope_action.h index 614ee5d..663f7db 100644 --- a/src/scope_action.h +++ b/src/scope_action.h @@ -85,7 +85,7 @@ const T& conditional_forward(T&& t, std::false_type) // NOLINT(cppcoreguideline * the exit function. This can lead to surprising behavior. */ template -class [[nodiscard]] exit_action { +class [[nodiscard("The object must be used to ensure the exit function is called on scope exit.")]] exit_action { public: /** * @brief Constructs a new @a exit_action from an exit function of type @a Func. @@ -106,7 +106,7 @@ class [[nodiscard]] exit_action { */ template requires(detail::can_construct_from) - [[nodiscard]] explicit exit_action( + explicit exit_action( Func&& fn ) noexcept(std::is_nothrow_constructible_v || std::is_nothrow_constructible_v) try @@ -116,8 +116,7 @@ class [[nodiscard]] exit_action { std::bool_constant< std::is_nothrow_constructible_v && !std::is_lvalue_reference_v>() ) - ), - m_is_armed(true) + ) {} catch (...) { fn(); @@ -140,7 +139,7 @@ class [[nodiscard]] exit_action { */ template requires(detail::can_move_construct_from_noexcept) - explicit exit_action(Func&& fn) noexcept : m_exit_function(std::forward(fn)), m_is_armed(true) + explicit exit_action(Func&& fn) noexcept : m_exit_function(std::forward(fn)) {} /** @@ -209,7 +208,7 @@ class [[nodiscard]] exit_action { private: ExitFunc m_exit_function; ///< The stored exit function. - bool m_is_armed; ///< Whether this `exit_action` is active. + bool m_is_armed = true; ///< Whether this `exit_action` is active. }; /** @@ -244,7 +243,7 @@ exit_action(ExitFunc) -> exit_action; * the destruction. */ template -class [[nodiscard]] fail_action { +class [[nodiscard("The object must be used to ensure the exit function is called due to an exception.")]] fail_action { public: /** * @brief Constructs a new @a fail_action from an exit function of type @a Func. @@ -278,8 +277,7 @@ class [[nodiscard]] fail_action { std::bool_constant< std::is_nothrow_constructible_v && !std::is_lvalue_reference_v>() ) - ), - m_uncaught_exceptions_count(std::uncaught_exceptions()) + ) {} catch (...) { fn(); @@ -302,8 +300,7 @@ class [[nodiscard]] fail_action { */ template requires(detail::can_move_construct_from_noexcept) - explicit fail_action(Func&& fn) noexcept - : m_exit_function(std::forward(fn)), m_uncaught_exceptions_count(std::uncaught_exceptions()) + explicit fail_action(Func&& fn) noexcept : m_exit_function(std::forward(fn)) {} /** @@ -377,8 +374,8 @@ class [[nodiscard]] fail_action { void release() noexcept { this->m_uncaught_exceptions_count = std::numeric_limits::max(); } private: - ExitFunc m_exit_function; ///< The stored exit function. - int m_uncaught_exceptions_count; ///< The counter of uncaught exceptions. + ExitFunc m_exit_function; ///< The stored exit function. + int m_uncaught_exceptions_count = std::uncaught_exceptions(); ///< The counter of uncaught exceptions. }; /** @@ -417,7 +414,9 @@ fail_action(ExitFunc) -> fail_action; * calling the exit function. This can lead to surprising behavior. */ template -class [[nodiscard]] success_action { +class [[nodiscard( + "The object must be used to ensure the exit function is called on a clean scope exit." +)]] success_action { public: /** * @brief Constructs a new @a success_action from an exit function of type @a Func. @@ -450,8 +449,7 @@ class [[nodiscard]] success_action { std::bool_constant< std::is_nothrow_constructible_v && !std::is_lvalue_reference_v>() ) - ), - m_uncaught_exceptions_count(std::uncaught_exceptions()) + ) {} /** @@ -471,8 +469,7 @@ class [[nodiscard]] success_action { */ template requires detail::can_move_construct_from_noexcept - explicit success_action(Func&& fn) noexcept - : m_exit_function(std::forward(fn)), m_uncaught_exceptions_count(std::uncaught_exceptions()) + explicit success_action(Func&& fn) noexcept : m_exit_function(std::forward(fn)) {} /** @@ -548,8 +545,8 @@ class [[nodiscard]] success_action { void release() noexcept { this->m_uncaught_exceptions_count = std::numeric_limits::min(); } private: - ExitFunc m_exit_function; ///< The stored exit function. - int m_uncaught_exceptions_count; ///< The counter of uncaught exceptions. + ExitFunc m_exit_function; ///< The stored exit function. + int m_uncaught_exceptions_count = std::uncaught_exceptions(); ///< The counter of uncaught exceptions. }; /**