From 2f03169db478fae1d2213b3e91c9cacc5348b094 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Tue, 9 Jun 2026 10:55:49 -0400 Subject: [PATCH] refactor(sonar): fix cpp:S6004 --- src/common/logging.cpp | 5 ++--- src/common/types.cpp | 4 ++-- src/windows/settings_manager_apply.cpp | 14 ++++++-------- src/windows/settings_utils.cpp | 3 +-- src/windows/win_api_layer.cpp | 18 ++++++------------ src/windows/win_api_utils.cpp | 15 +++++---------- src/windows/win_display_device_hdr.cpp | 3 +-- src/windows/win_display_device_modes.cpp | 6 ++---- src/windows/win_display_device_primary.cpp | 3 +-- src/windows/win_display_device_topology.cpp | 6 ++---- tests/fixtures/fixtures.cpp | 3 +-- tests/fixtures/test_utils.cpp | 3 +-- 12 files changed, 30 insertions(+), 53 deletions(-) diff --git a/src/common/logging.cpp b/src/common/logging.cpp index 5e1aaa07..2df39d74 100644 --- a/src/common/logging.cpp +++ b/src/common/logging.cpp @@ -20,12 +20,11 @@ namespace display_device { namespace { std::tm threadSafeLocaltime(const std::time_t &time) { #if defined(_MSC_VER) // MSVCRT (2005+): std::localtime is threadsafe - const auto tm_ptr {std::localtime(&time)}; + if (const auto tm_ptr {std::localtime(&time)}; tm_ptr) { #else // POSIX std::tm buffer; - const auto tm_ptr {localtime_r(&time, &buffer)}; + if (const auto tm_ptr {localtime_r(&time, &buffer)}; tm_ptr) { #endif // _MSC_VER - if (tm_ptr) { return *tm_ptr; } return {}; diff --git a/src/common/types.cpp b/src/common/types.cpp index 19a01a56..bb6e820b 100644 --- a/src/common/types.cpp +++ b/src/common/types.cpp @@ -46,8 +46,8 @@ namespace display_device { } // ---- Verify fixed header - static const std::vector fixed_header {std::byte {0x00}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0x00}}; - if (!std::equal(std::begin(fixed_header), std::end(fixed_header), std::begin(data))) { + if (static const std::array fixed_header {std::byte {0x00}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0x00}}; + !std::equal(std::begin(fixed_header), std::end(fixed_header), std::begin(data))) { DD_LOG(warning) << "EDID data does not contain fixed header."; return std::nullopt; } diff --git a/src/windows/settings_manager_apply.cpp b/src/windows/settings_manager_apply.cpp index 989e85ed..50c136b2 100644 --- a/src/windows/settings_manager_apply.cpp +++ b/src/windows/settings_manager_apply.cpp @@ -53,8 +53,7 @@ namespace display_device { boost::scope::scope_exit topology_prep_guard {[this, topology = topology_before_changes, was_captured = m_audio_context_api->isCaptured(), &release_context]() { // It is possible that during topology preparation, some settings will be reverted for the modified topology. // To keel it simple, these settings will not be restored! - const auto result {m_dd_api->setTopology(topology)}; - if (!result) { + if (const auto result {m_dd_api->setTopology(topology)}; !result) { DD_LOG(error) << "Failed to revert back to topology in the topology guard!"; if (release_context) { // We are currently in the topology for which the context was captured. @@ -240,9 +239,8 @@ namespace display_device { if (ensure_primary) { const auto original_primary_device {cached_primary_device.empty() ? current_primary_device : cached_primary_device}; - const auto &new_primary_device {device_to_configure}; - if (!try_change(new_primary_device, "Changing primary display to:\n", "Failed to apply new configuration, because a new primary device could not be set!")) { + if (const auto &new_primary_device {device_to_configure}; !try_change(new_primary_device, "Changing primary display to:\n", "Failed to apply new configuration, because a new primary device could not be set!")) { // Error already logged return false; } @@ -301,9 +299,9 @@ namespace display_device { if (change_required) { const bool configuring_primary_devices {config.m_device_id.empty()}; const auto original_display_modes {cached_display_modes.empty() ? current_display_modes : cached_display_modes}; - const auto new_display_modes {win_utils::computeNewDisplayModes(config.m_resolution, config.m_refresh_rate, configuring_primary_devices, device_to_configure, additional_devices_to_configure, original_display_modes)}; - if (!try_change(new_display_modes, "Changing display modes to:\n", "Failed to apply new configuration, because new display modes could not be set!")) { + if (const auto new_display_modes {win_utils::computeNewDisplayModes(config.m_resolution, config.m_refresh_rate, configuring_primary_devices, device_to_configure, additional_devices_to_configure, original_display_modes)}; + !try_change(new_display_modes, "Changing display modes to:\n", "Failed to apply new configuration, because new display modes could not be set!")) { // Error already logged return false; } @@ -357,9 +355,9 @@ namespace display_device { if (change_required) { const bool configuring_primary_devices {config.m_device_id.empty()}; const auto original_hdr_states {cached_hdr_states.empty() ? current_hdr_states : cached_hdr_states}; - const auto new_hdr_states {win_utils::computeNewHdrStates(config.m_hdr_state, configuring_primary_devices, device_to_configure, additional_devices_to_configure, original_hdr_states)}; - if (!try_change(new_hdr_states, "Changing HDR states to:\n", "Failed to apply new configuration, because new HDR states could not be set!")) { + if (const auto new_hdr_states {win_utils::computeNewHdrStates(config.m_hdr_state, configuring_primary_devices, device_to_configure, additional_devices_to_configure, original_hdr_states)}; + !try_change(new_hdr_states, "Changing HDR states to:\n", "Failed to apply new configuration, because new HDR states could not be set!")) { // Error already logged return false; } diff --git a/src/windows/settings_utils.cpp b/src/windows/settings_utils.cpp index 5dd0d46f..1fd84279 100644 --- a/src/windows/settings_utils.cpp +++ b/src/windows/settings_utils.cpp @@ -315,8 +315,7 @@ namespace display_device::win_utils { if (hdr_state) { const auto try_update_new_state = [&new_states, &hdr_state](const std::string &device_id) { - const auto current_state {new_states[device_id]}; - if (!current_state) { + if (const auto current_state {new_states[device_id]}; !current_state) { return; } diff --git a/src/windows/win_api_layer.cpp b/src/windows/win_api_layer.cpp index 64bb3ddd..63b4777a 100644 --- a/src/windows/win_api_layer.cpp +++ b/src/windows/win_api_layer.cpp @@ -139,8 +139,7 @@ namespace display_device { target_name.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME; target_name.header.size = sizeof(target_name); - LONG result {DisplayConfigGetDeviceInfo(&target_name.header)}; - if (result != ERROR_SUCCESS) { + if (LONG result {DisplayConfigGetDeviceInfo(&target_name.header)}; result != ERROR_SUCCESS) { DD_LOG(error) << w_api.getErrorString(result) << " failed to get target device name!"; return {}; } @@ -252,8 +251,7 @@ namespace display_device { std::optional>> getInstanceIdAndEdid(const WinApiLayerInterface &w_api, const std::wstring &device_path) { static const GUID monitor_guid {0xe6f07b5f, 0xee97, 0x4a90, {0xb0, 0x76, 0x33, 0xf5, 0x7b, 0xf4, 0xea, 0xa7}}; - HDEVINFO dev_info_handle {SetupDiGetClassDevsW(&monitor_guid, nullptr, nullptr, DIGCF_DEVICEINTERFACE)}; - if (dev_info_handle) { + if (HDEVINFO dev_info_handle {SetupDiGetClassDevsW(&monitor_guid, nullptr, nullptr, DIGCF_DEVICEINTERFACE)}; dev_info_handle) { const auto dev_info_handle_cleanup { boost::scope::scope_exit([&dev_info_handle, &w_api]() { if (!SetupDiDestroyDeviceInfoList(dev_info_handle)) { @@ -353,8 +351,7 @@ namespace display_device { condition_mask = VerSetConditionMask(condition_mask, VER_MINORVERSION, VER_GREATER_EQUAL); // Minor version condition condition_mask = VerSetConditionMask(condition_mask, VER_BUILDNUMBER, VER_GREATER_EQUAL); // Build number condition - BOOL result {VerifyVersionInfoA(&os_version_info, VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, condition_mask)}; - if (result == FALSE) { + if (BOOL result {VerifyVersionInfoA(&os_version_info, VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, condition_mask)}; result == FALSE) { DD_LOG(verbose) << w_api.getErrorString(static_cast(GetLastError())) << " \"is_W11_24H2_OrAbove\" returned false."; return false; } @@ -444,8 +441,7 @@ namespace display_device { } std::vector device_id_data; - auto instance_id_and_edid {getInstanceIdAndEdid(*this, device_path)}; - if (instance_id_and_edid) { + if (auto instance_id_and_edid {getInstanceIdAndEdid(*this, device_path)}; instance_id_and_edid) { // Instance ID is unique in the system and persists restarts, but not driver re-installs. // It looks like this: // DISPLAY\ACI27EC\5&4FD2DE4&5&UID4352 (also used in the device path it seems) @@ -556,8 +552,7 @@ namespace display_device { source_name.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME; source_name.header.size = sizeof(source_name); - LONG result {DisplayConfigGetDeviceInfo(&source_name.header)}; - if (result != ERROR_SUCCESS) { + if (LONG result {DisplayConfigGetDeviceInfo(&source_name.header)}; result != ERROR_SUCCESS) { DD_LOG(error) << getErrorString(result) << " failed to get display name!"; return {}; } @@ -661,8 +656,7 @@ namespace display_device { return FALSE; } - MONITORINFOEXA monitor_info {sizeof(MONITORINFOEXA)}; - if (GetMonitorInfoA(monitor, &monitor_info)) { + if (MONITORINFOEXA monitor_info {sizeof(MONITORINFOEXA)}; GetMonitorInfoA(monitor, &monitor_info)) { if (data->m_display_name == monitor_info.szDevice) { data->m_width = monitor_info.rcMonitor.right - monitor_info.rcMonitor.left; return FALSE; diff --git a/src/windows/win_api_utils.cpp b/src/windows/win_api_utils.cpp index 66082419..470d1711 100644 --- a/src/windows/win_api_utils.cpp +++ b/src/windows/win_api_utils.cpp @@ -199,8 +199,7 @@ namespace display_device::win_utils { return std::nullopt; } - const auto display_name {w_api.getDisplayName(path)}; - if (display_name.empty()) { + if (const auto display_name {w_api.getDisplayName(path)}; display_name.empty()) { return std::nullopt; } @@ -239,8 +238,7 @@ namespace display_device::win_utils { continue; } - const auto prev_device_id_for_path_it {paths_to_ids.find(device_info->m_device_path)}; - if (prev_device_id_for_path_it != std::end(paths_to_ids)) { + if (const auto prev_device_id_for_path_it {paths_to_ids.find(device_info->m_device_path)}; prev_device_id_for_path_it != std::end(paths_to_ids)) { if (prev_device_id_for_path_it->second != device_info->m_device_id) { DD_LOG(error) << "Duplicate display device id found: " << device_info->m_device_id << " (device path: " << device_info->m_device_path << ")"; return {}; @@ -256,8 +254,7 @@ namespace display_device::win_utils { paths_to_ids[device_info->m_device_path] = device_info->m_device_id; } - auto path_data_it {path_data.find(device_info->m_device_id)}; - if (path_data_it != std::end(path_data)) { + if (auto path_data_it {path_data.find(device_info->m_device_id)}; path_data_it != std::end(path_data)) { if (path_data_it->second.m_adapter_id != path.sourceInfo.adapterId) { // Sanity check, should not be possible since adapter in embedded in the device path DD_LOG(error) << "Device path " << device_info->m_device_path << " has different adapters!"; @@ -297,8 +294,7 @@ namespace display_device::win_utils { UINT32 group_id {0}; StringUnorderedMap> used_source_ids_per_adapter; const auto is_source_id_already_used = [&used_source_ids_per_adapter](const LUID &adapter_id, UINT32 source_id) { - auto entry_it {used_source_ids_per_adapter.find(toString(adapter_id))}; - if (entry_it != std::end(used_source_ids_per_adapter)) { + if (auto entry_it {used_source_ids_per_adapter.find(toString(adapter_id))}; entry_it != std::end(used_source_ids_per_adapter)) { return entry_it->second.contains(source_id); } @@ -308,8 +304,7 @@ namespace display_device::win_utils { for (const auto &group : new_topology) { StringUnorderedMap used_source_ids_per_adapter_per_group; const auto get_already_used_source_id_in_group = [&used_source_ids_per_adapter_per_group](const LUID &adapter_id) -> std::optional { - auto entry_it {used_source_ids_per_adapter_per_group.find(toString(adapter_id))}; - if (entry_it != std::end(used_source_ids_per_adapter_per_group)) { + if (auto entry_it {used_source_ids_per_adapter_per_group.find(toString(adapter_id))}; entry_it != std::end(used_source_ids_per_adapter_per_group)) { return entry_it->second; } diff --git a/src/windows/win_display_device_hdr.cpp b/src/windows/win_display_device_hdr.cpp index 2ea09b4b..bdda85f7 100644 --- a/src/windows/win_display_device_hdr.cpp +++ b/src/windows/win_display_device_hdr.cpp @@ -118,8 +118,7 @@ namespace display_device { return {}; } - HdrStateMapNoOpt changed_states; - if (!doSetHdrStates(*m_w_api, *display_data, states_without_opt, &changed_states)) { + if (HdrStateMapNoOpt changed_states; !doSetHdrStates(*m_w_api, *display_data, states_without_opt, &changed_states)) { if (!changed_states.empty()) { doSetHdrStates(*m_w_api, *display_data, changed_states, nullptr); // return value does not matter } diff --git a/src/windows/win_display_device_modes.cpp b/src/windows/win_display_device_modes.cpp index 9b3656b8..68ca0abc 100644 --- a/src/windows/win_display_device_modes.cpp +++ b/src/windows/win_display_device_modes.cpp @@ -92,8 +92,7 @@ namespace display_device { flags |= SDC_ALLOW_CHANGES; } - const LONG result {w_api.setDisplayConfig(display_data->m_paths, display_data->m_modes, flags)}; - if (result != ERROR_SUCCESS) { + if (const LONG result {w_api.setDisplayConfig(display_data->m_paths, display_data->m_modes, flags)}; result != ERROR_SUCCESS) { DD_LOG(error) << w_api.getErrorString(result) << " failed to set display mode!"; return false; } @@ -203,8 +202,7 @@ namespace display_device { return true; }; - auto current_modes {getCurrentDisplayModes(device_ids)}; - if (!current_modes.empty()) { + if (auto current_modes {getCurrentDisplayModes(device_ids)}; !current_modes.empty()) { if (all_modes_match(current_modes)) { return true; } diff --git a/src/windows/win_display_device_primary.cpp b/src/windows/win_display_device_primary.cpp index ed2f52d2..06da2b61 100644 --- a/src/windows/win_display_device_primary.cpp +++ b/src/windows/win_display_device_primary.cpp @@ -98,8 +98,7 @@ namespace display_device { } const UINT32 flags {SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE}; - const LONG result {m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags)}; - if (result != ERROR_SUCCESS) { + if (const LONG result {m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags)}; result != ERROR_SUCCESS) { DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!"; return false; } diff --git a/src/windows/win_display_device_topology.cpp b/src/windows/win_display_device_topology.cpp index 72bbdee1..1aaf3ce5 100644 --- a/src/windows/win_display_device_topology.cpp +++ b/src/windows/win_display_device_topology.cpp @@ -33,8 +33,7 @@ namespace display_device { } UINT32 flags {SDC_APPLY | SDC_TOPOLOGY_SUPPLIED | SDC_ALLOW_PATH_ORDER_CHANGES | SDC_VIRTUAL_MODE_AWARE}; - LONG result {w_api.setDisplayConfig(paths, {}, flags)}; - if (result == ERROR_GEN_FAILURE) { + if (LONG result {w_api.setDisplayConfig(paths, {}, flags)}; result == ERROR_GEN_FAILURE) { DD_LOG(warning) << w_api.getErrorString(result) << " failed to change topology using the topology from Windows DB! Asking Windows to create the topology."; flags = SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_ALLOW_CHANGES /* This flag is probably not needed, but who knows really... (not MSDOCS at least) */ | SDC_VIRTUAL_MODE_AWARE | SDC_SAVE_TO_DATABASE; @@ -160,8 +159,7 @@ namespace display_device { } if (doSetTopology(*m_w_api, new_topology, *original_data)) { - const auto updated_topology {getCurrentTopology()}; - if (isTopologyValid(updated_topology)) { + if (const auto updated_topology {getCurrentTopology()}; isTopologyValid(updated_topology)) { if (isTopologyTheSame(new_topology, updated_topology)) { return true; } else { diff --git a/tests/fixtures/fixtures.cpp b/tests/fixtures/fixtures.cpp index 44020c02..7ac07e0c 100644 --- a/tests/fixtures/fixtures.cpp +++ b/tests/fixtures/fixtures.cpp @@ -53,8 +53,7 @@ const std::vector &BaseTest::getArgs() const { } std::optional BaseTest::getArgWithMatchingPattern(const std::string &pattern, bool remove_match) const { - const auto &args {getArgs()}; - if (!args.empty()) { + if (const auto &args {getArgs()}; !args.empty()) { const std::regex re_pattern {pattern}; // We are skipping the first arg which is always binary name/path. diff --git a/tests/fixtures/test_utils.cpp b/tests/fixtures/test_utils.cpp index c2909982..6d2ec80f 100644 --- a/tests/fixtures/test_utils.cpp +++ b/tests/fixtures/test_utils.cpp @@ -43,8 +43,7 @@ namespace ut_consts { bool testRegex(const std::string &input, const std::string &pattern) { std::regex regex(pattern); - std::smatch match; - if (!std::regex_match(input, match, regex)) { + if (std::smatch match; !std::regex_match(input, match, regex)) { std::cout << "Regex test failed:\n" << " Input : " << input << "\n" << " Pattern: " << pattern << std::endl;