From e2eee28d538c8188d0a7523cfc5c092946383fe1 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Tue, 9 Jun 2026 13:46:18 -0400 Subject: [PATCH] refactor(sonar): fix cpp:S112 --- src/common/file_settings_persistence.cpp | 3 ++- .../detail/json_serializer_details.h | 5 +++-- .../include/display_device/retry_scheduler.h | 11 ++++++----- src/windows/persistent_state.cpp | 12 +++++++++++- src/windows/settings_manager_general.cpp | 7 +++++-- src/windows/win_display_device_general.cpp | 5 ++++- .../general/test_file_settings_persistence.cpp | 3 ++- tests/unit/general/test_retry_scheduler.cpp | 16 ++++++++-------- .../windows/test_settings_manager_general.cpp | 7 +++++-- .../windows/test_win_display_device_general.cpp | 3 ++- 10 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/common/file_settings_persistence.cpp b/src/common/file_settings_persistence.cpp index 01f03e81..b436d3d8 100644 --- a/src/common/file_settings_persistence.cpp +++ b/src/common/file_settings_persistence.cpp @@ -9,6 +9,7 @@ #include #include #include +#include // local includes #include "display_device/logging.h" @@ -17,7 +18,7 @@ namespace display_device { FileSettingsPersistence::FileSettingsPersistence(std::filesystem::path filepath): m_filepath {std::move(filepath)} { if (m_filepath.empty()) { - throw std::runtime_error {"Empty filename provided for FileSettingsPersistence!"}; + throw std::invalid_argument {"Empty filename provided for FileSettingsPersistence!"}; } } diff --git a/src/common/include/display_device/detail/json_serializer_details.h b/src/common/include/display_device/detail/json_serializer_details.h index 1fec98a5..d0c5e319 100644 --- a/src/common/include/display_device/detail/json_serializer_details.h +++ b/src/common/include/display_device/detail/json_serializer_details.h @@ -7,6 +7,7 @@ #ifdef DD_JSON_DETAIL // system includes #include + #include // Special versions of the NLOHMANN definitions to remove the "m_" prefix in string form ('cause I like it that way ;P) #define DD_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.m_##v1; @@ -85,7 +86,7 @@ namespace display_device { const auto &map {getEnumMap(T {})}; auto it {std::find_if(std::begin(map), std::end(map), predicate)}; if (it == std::end(map)) { // GCOVR_EXCL_BR_LINE for fallthrough branch - throw std::runtime_error(error_msg); // GCOVR_EXCL_BR_LINE for fallthrough branch + throw std::out_of_range(error_msg); // GCOVR_EXCL_BR_LINE for fallthrough branch } return it; } @@ -131,7 +132,7 @@ namespace nlohmann { const bool found {(display_device::detail::variantFromJson(nlohmann_json_j, nlohmann_json_t) || ...)}; if (!found) { const std::string error {"Could not parse variant from type " + nlohmann_json_j.at("type").get() + "!"}; - throw std::runtime_error(error); + throw std::invalid_argument(error); } } }; diff --git a/src/common/include/display_device/retry_scheduler.h b/src/common/include/display_device/retry_scheduler.h index 0ba037a9..85430c9d 100644 --- a/src/common/include/display_device/retry_scheduler.h +++ b/src/common/include/display_device/retry_scheduler.h @@ -9,6 +9,7 @@ #include #include #include +#include #include // local includes @@ -147,7 +148,7 @@ namespace display_device { * @param iface Interface to be passed around to the executor functions. */ explicit RetryScheduler(std::unique_ptr iface): - m_iface {iface ? std::move(iface) : throw std::logic_error {"Nullptr interface provided in RetryScheduler!"}}, + m_iface {iface ? std::move(iface) : throw std::invalid_argument {"Nullptr interface provided in RetryScheduler!"}}, m_thread {[this]() { std::unique_lock lock {m_mutex}; while (m_keep_alive) { @@ -218,17 +219,17 @@ namespace display_device { */ void schedule(std::function exec_fn, const SchedulerOptions &options) { if (!exec_fn) { - throw std::logic_error {"Empty callback function provided in RetryScheduler::schedule!"}; + throw std::invalid_argument {"Empty callback function provided in RetryScheduler::schedule!"}; } if (options.m_sleep_durations.empty()) { - throw std::logic_error {"At least 1 sleep duration must be specified in RetryScheduler::schedule!"}; + throw std::invalid_argument {"At least 1 sleep duration must be specified in RetryScheduler::schedule!"}; } if (std::ranges::any_of(options.m_sleep_durations, [&](const auto &duration) { return duration == std::chrono::milliseconds::zero(); })) { - throw std::logic_error {"All of the durations specified in RetryScheduler::schedule must be larger than a 0!"}; + throw std::invalid_argument {"All of the durations specified in RetryScheduler::schedule must be larger than a 0!"}; } std::lock_guard lock {m_mutex}; @@ -348,7 +349,7 @@ namespace display_device { if constexpr (detail::OptionalFunction) { if (!exec_fn) { - throw std::logic_error {"Empty callback function provided in RetryScheduler::execute!"}; + throw std::invalid_argument {"Empty callback function provided in RetryScheduler::execute!"}; } } diff --git a/src/windows/persistent_state.cpp b/src/windows/persistent_state.cpp index 2ff9d5e6..7549320c 100644 --- a/src/windows/persistent_state.cpp +++ b/src/windows/persistent_state.cpp @@ -5,12 +5,22 @@ // class header include #include "display_device/windows/persistent_state.h" +// system includes +#include + // local includes #include "display_device/logging.h" #include "display_device/noop_settings_persistence.h" #include "display_device/windows/json.h" namespace display_device { + namespace { + class PersistentStateLoadException final: public std::runtime_error { + public: + using std::runtime_error::runtime_error; + }; + } // namespace + PersistentState::PersistentState(std::shared_ptr settings_persistence_api, const bool throw_on_load_error): m_settings_persistence_api {std::move(settings_persistence_api)} { if (!m_settings_persistence_api) { @@ -31,7 +41,7 @@ namespace display_device { if (!error_message.empty()) { if (throw_on_load_error) { - throw std::runtime_error {error_message}; + throw PersistentStateLoadException {error_message}; } DD_LOG(error) << error_message; diff --git a/src/windows/settings_manager_general.cpp b/src/windows/settings_manager_general.cpp index 1a29ef52..71cc5eb9 100644 --- a/src/windows/settings_manager_general.cpp +++ b/src/windows/settings_manager_general.cpp @@ -5,6 +5,9 @@ // class header include #include "display_device/windows/settings_manager.h" +// system includes +#include + // local includes #include "display_device/logging.h" #include "display_device/noop_audio_context.h" @@ -22,7 +25,7 @@ namespace display_device { m_persistence_state {std::move(persistent_state)}, m_workarounds {std::move(workarounds)} { if (!m_dd_api) { - throw std::logic_error {"Nullptr provided for WinDisplayDeviceInterface in SettingsManager!"}; + throw std::invalid_argument {"Nullptr provided for WinDisplayDeviceInterface in SettingsManager!"}; } if (!m_audio_context_api) { @@ -30,7 +33,7 @@ namespace display_device { } if (!m_persistence_state) { - throw std::logic_error {"Nullptr provided for PersistentState in SettingsManager!"}; + throw std::invalid_argument {"Nullptr provided for PersistentState in SettingsManager!"}; } DD_LOG(info) << "Provided workaround settings for SettingsManager:\n" diff --git a/src/windows/win_display_device_general.cpp b/src/windows/win_display_device_general.cpp index 8672a799..f4f25fdf 100644 --- a/src/windows/win_display_device_general.cpp +++ b/src/windows/win_display_device_general.cpp @@ -5,6 +5,9 @@ // class header include #include "display_device/windows/win_display_device.h" +// system includes +#include + // local includes #include "display_device/logging.h" #include "display_device/windows/win_api_utils.h" @@ -13,7 +16,7 @@ namespace display_device { WinDisplayDevice::WinDisplayDevice(std::shared_ptr w_api): m_w_api {std::move(w_api)} { if (!m_w_api) { - throw std::logic_error {"Nullptr provided for WinApiLayerInterface in WinDisplayDevice!"}; + throw std::invalid_argument {"Nullptr provided for WinApiLayerInterface in WinDisplayDevice!"}; } } diff --git a/tests/unit/general/test_file_settings_persistence.cpp b/tests/unit/general/test_file_settings_persistence.cpp index 92fd12c7..241b170c 100644 --- a/tests/unit/general/test_file_settings_persistence.cpp +++ b/tests/unit/general/test_file_settings_persistence.cpp @@ -1,6 +1,7 @@ // system includes #include #include +#include // local includes #include "display_device/file_settings_persistence.h" @@ -39,7 +40,7 @@ TEST_F_S(EmptyFilenameProvided) { EXPECT_THAT([]() { const display_device::FileSettingsPersistence persistence {{}}; }, - ThrowsMessage(HasSubstr("Empty filename provided for FileSettingsPersistence!"))); + ThrowsMessage(HasSubstr("Empty filename provided for FileSettingsPersistence!"))); } TEST_F_S(Store, NewFileCreated) { diff --git a/tests/unit/general/test_retry_scheduler.cpp b/tests/unit/general/test_retry_scheduler.cpp index 3808841e..0bc96a6f 100644 --- a/tests/unit/general/test_retry_scheduler.cpp +++ b/tests/unit/general/test_retry_scheduler.cpp @@ -51,14 +51,14 @@ TEST_F_S(NullptrInterfaceProvided) { EXPECT_THAT([]() { const display_device::RetryScheduler scheduler(nullptr); }, - ThrowsMessage(HasSubstr("Nullptr interface provided in RetryScheduler!"))); + ThrowsMessage(HasSubstr("Nullptr interface provided in RetryScheduler!"))); } TEST_F_S(Schedule, NullptrCallbackProvided) { EXPECT_THAT([&]() { m_impl.schedule(nullptr, {.m_sleep_durations = {0ms}}); }, - ThrowsMessage(HasSubstr("Empty callback function provided in RetryScheduler::schedule!"))); + ThrowsMessage(HasSubstr("Empty callback function provided in RetryScheduler::schedule!"))); } TEST_F_S(Schedule, NoDurations) { @@ -67,7 +67,7 @@ TEST_F_S(Schedule, NoDurations) { }, {.m_sleep_durations = {}}); }, - ThrowsMessage(HasSubstr("At least 1 sleep duration must be specified in RetryScheduler::schedule!"))); + ThrowsMessage(HasSubstr("At least 1 sleep duration must be specified in RetryScheduler::schedule!"))); } TEST_F_S(Schedule, ZeroDuration) { @@ -76,7 +76,7 @@ TEST_F_S(Schedule, ZeroDuration) { }, {.m_sleep_durations = {0ms}}); }, - ThrowsMessage(HasSubstr("All of the durations specified in RetryScheduler::schedule must be larger than a 0!"))); + ThrowsMessage(HasSubstr("All of the durations specified in RetryScheduler::schedule must be larger than a 0!"))); } TEST_F_S(Schedule, SchedulingDurations) { @@ -305,7 +305,7 @@ TEST_F_S(Schedule, ExceptionThrown, DuringImmediateCall) { EXPECT_TRUE(m_impl.isScheduled()); m_impl.schedule([](auto, auto &) { - throw std::runtime_error("Get rekt!"); + throw SchedulerStopTokenTestException {}; }, {.m_sleep_durations = {1ms}}); EXPECT_FALSE(m_impl.isScheduled()); @@ -337,7 +337,7 @@ TEST_F_S(Schedule, ExceptionThrown, DuringScheduledCall) { EXPECT_EQ(output, ""); m_impl.schedule([&first_call](auto, auto &) { if (!first_call) { - throw std::runtime_error("Get rekt!"); + throw SchedulerStopTokenTestException {}; } first_call = false; }, @@ -367,7 +367,7 @@ TEST_F_S(Execute, NonConst, NullptrCallbackProvided) { auto &non_const_impl {m_impl}; non_const_impl.execute(std::function {}); }, - ThrowsMessage(HasSubstr("Empty callback function provided in RetryScheduler::execute!"))); + ThrowsMessage(HasSubstr("Empty callback function provided in RetryScheduler::execute!"))); } TEST_F_S(Execute, Const, NullptrCallbackProvided) { @@ -376,7 +376,7 @@ TEST_F_S(Execute, Const, NullptrCallbackProvided) { auto &const_impl {m_impl}; const_impl.execute(std::function {}); }, - ThrowsMessage(HasSubstr("Empty callback function provided in RetryScheduler::execute!"))); + ThrowsMessage(HasSubstr("Empty callback function provided in RetryScheduler::execute!"))); } TEST_F_S(Execute, SchedulerNotStopped) { diff --git a/tests/unit/windows/test_settings_manager_general.cpp b/tests/unit/windows/test_settings_manager_general.cpp index 28f36ed9..71460eb7 100644 --- a/tests/unit/windows/test_settings_manager_general.cpp +++ b/tests/unit/windows/test_settings_manager_general.cpp @@ -1,3 +1,6 @@ +// system includes +#include + // local includes #include "display_device/noop_audio_context.h" #include "display_device/noop_settings_persistence.h" @@ -44,7 +47,7 @@ TEST_F_S_MOCKED(NullptrDisplayDeviceApiProvided) { EXPECT_THAT([]() { const display_device::SettingsManager settings_manager(nullptr, nullptr, nullptr, {}); }, - ThrowsMessage(HasSubstr("Nullptr provided for WinDisplayDeviceInterface in SettingsManager!"))); + ThrowsMessage(HasSubstr("Nullptr provided for WinDisplayDeviceInterface in SettingsManager!"))); } TEST_F_S_MOCKED(NoopAudioContext) { @@ -62,7 +65,7 @@ TEST_F_S_MOCKED(NullptrPersistentStateProvided) { EXPECT_THAT([this]() { const display_device::SettingsManager settings_manager(m_dd_api, nullptr, nullptr, {}); }, - ThrowsMessage(HasSubstr("Nullptr provided for PersistentState in SettingsManager!"))); + ThrowsMessage(HasSubstr("Nullptr provided for PersistentState in SettingsManager!"))); } TEST_F_S_MOCKED(EnumAvailableDevices) { diff --git a/tests/unit/windows/test_win_display_device_general.cpp b/tests/unit/windows/test_win_display_device_general.cpp index 9abd92d8..7509fb05 100644 --- a/tests/unit/windows/test_win_display_device_general.cpp +++ b/tests/unit/windows/test_win_display_device_general.cpp @@ -1,5 +1,6 @@ // system includes #include +#include // local includes #include "display_device/windows/settings_utils.h" @@ -44,7 +45,7 @@ TEST_F_S(NullptrLayerProvided) { EXPECT_THAT([]() { const auto win_dd {display_device::WinDisplayDevice {nullptr}}; }, - ThrowsMessage(HasSubstr("Nullptr provided for WinApiLayerInterface in WinDisplayDevice!"))); + ThrowsMessage(HasSubstr("Nullptr provided for WinApiLayerInterface in WinDisplayDevice!"))); } TEST_F_S(IsApiAccessAvailable) {