From 8ac6db8b5a9ffd4e20ed020b52d9b569f458d611 Mon Sep 17 00:00:00 2001 From: Bartosz Sosnowski Date: Wed, 11 Sep 2019 12:38:20 +0200 Subject: [PATCH] Fix cppcheck reported issues (#333) Fixes: https://github.com/microsoft/PowerToys/issues/266 --- src/common/animation.cpp | 4 ++-- src/common/animation.h | 2 +- src/common/dpi_aware.h | 2 +- src/common/monitors.cpp | 4 ++-- src/common/tasklist_positions.cpp | 2 +- src/editor/main.cpp | 4 ++-- src/modules/example_powertoy/dllmain.cpp | 10 +++++----- src/modules/fancyzones/dll/dllmain.cpp | 2 +- src/modules/fancyzones/lib/trace.cpp | 4 ++-- src/modules/fancyzones/lib/trace.h | 2 +- src/modules/shortcut_guide/overlay_window.cpp | 3 +-- src/modules/shortcut_guide/overlay_window.h | 4 ++-- src/modules/shortcut_guide/shortcut_guide.cpp | 7 +++---- src/modules/shortcut_guide/target_state.cpp | 2 +- src/runner/general_settings.cpp | 2 +- src/runner/main.cpp | 2 +- src/runner/powertoy_module.h | 4 ++-- src/runner/settings_window.cpp | 2 +- tools/project_template/ModuleTemplate/dllmain.cpp | 8 ++++---- 19 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/common/animation.cpp b/src/common/animation.cpp index 363d5efa9dc..d034d713de5 100644 --- a/src/common/animation.cpp +++ b/src/common/animation.cpp @@ -2,7 +2,7 @@ #include "animation.h" Animation::Animation(double duration, double start, double stop) : - start_value(start), end_value(stop), duration(duration), start(std::chrono::high_resolution_clock::now()) { } + duration(duration), start_value(start), end_value(stop), start(std::chrono::high_resolution_clock::now()) { } void Animation::reset() { start = std::chrono::high_resolution_clock::now(); @@ -21,7 +21,7 @@ static double ease_out_expo(double t) { return 1 - pow(2, -8 * t); } -double Animation::apply_animation_function(double t, AnimFunctions apply_function) const { +double Animation::apply_animation_function(double t, AnimFunctions apply_function) { switch (apply_function) { case EASE_OUT_EXPO: return ease_out_expo(t); diff --git a/src/common/animation.h b/src/common/animation.h index 25384919ec2..f1068aef094 100644 --- a/src/common/animation.h +++ b/src/common/animation.h @@ -25,7 +25,7 @@ class Animation { double value(AnimFunctions apply_function) const; bool done() const; private: - double apply_animation_function(double t, AnimFunctions apply_function) const; + static double apply_animation_function(double t, AnimFunctions apply_function); std::chrono::high_resolution_clock::time_point start; double start_value, end_value, duration; }; diff --git a/src/common/dpi_aware.h b/src/common/dpi_aware.h index 52396ff16a6..e4f5eeb3fb2 100644 --- a/src/common/dpi_aware.h +++ b/src/common/dpi_aware.h @@ -7,5 +7,5 @@ class DPIAware { public: static HRESULT GetScreenDPIForWindow(HWND hwnd, UINT & dpi_x, UINT & dpi_y); - static void Convert(HMONITOR monitor_handle, int &width, int &high); + static void Convert(HMONITOR monitor_handle, int &width, int &height); }; diff --git a/src/common/monitors.cpp b/src/common/monitors.cpp index 7b59d1fac7b..a70197efbda 100644 --- a/src/common/monitors.cpp +++ b/src/common/monitors.cpp @@ -23,9 +23,9 @@ static BOOL CALLBACK get_displays_enum_cb_with_toolbar(HMONITOR monitor, HDC hdc return true; }; -std::vector MonitorInfo::GetMonitors(bool include_toolbars) { +std::vector MonitorInfo::GetMonitors(bool include_toolbar) { std::vector monitors; - EnumDisplayMonitors(NULL, NULL, include_toolbars ? get_displays_enum_cb_with_toolbar : get_displays_enum_cb, reinterpret_cast(&monitors)); + EnumDisplayMonitors(NULL, NULL, include_toolbar ? get_displays_enum_cb_with_toolbar : get_displays_enum_cb, reinterpret_cast(&monitors)); std::sort(begin(monitors), end(monitors), [](const MonitorInfo& lhs, const MonitorInfo& rhs) { return lhs.rect < rhs.rect; }); diff --git a/src/common/tasklist_positions.cpp b/src/common/tasklist_positions.cpp index f3970db30fa..4332541efc0 100644 --- a/src/common/tasklist_positions.cpp +++ b/src/common/tasklist_positions.cpp @@ -79,7 +79,7 @@ bool Tasklist::update_buttons(std::vector& buttons) { continue; // skip buttons from the same app button.keynum = buttons.back().keynum + 1; buttons.push_back(std::move(button)); - if (button.keynum == 10) + if (buttons.back().keynum == 10) break; // no more than 10 buttons } } diff --git a/src/editor/main.cpp b/src/editor/main.cpp index c140e9ee2a7..5b37576d086 100644 --- a/src/editor/main.cpp +++ b/src/editor/main.cpp @@ -112,7 +112,7 @@ void send_message_to_webview(const std::wstring& msg) { } } -void send_message_to_powertoys(const std::wstring msg) { +void send_message_to_powertoys(const std::wstring& msg) { if (current_settings_ipc != NULL) { current_settings_ipc->send(msg); } else { @@ -439,6 +439,6 @@ int start_webview_window(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpC } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { - HRESULT hrInit = CoInitialize(nullptr); + CoInitialize(nullptr); return start_webview_window(hInstance, hPrevInstance, lpCmdLine, nCmdShow); } diff --git a/src/modules/example_powertoy/dllmain.cpp b/src/modules/example_powertoy/dllmain.cpp index 59ef9fbac32..af2c52c0795 100644 --- a/src/modules/example_powertoy/dllmain.cpp +++ b/src/modules/example_powertoy/dllmain.cpp @@ -142,7 +142,7 @@ class ExamplePowertoy : public PowertoyModuleIface { MessageBox(NULL, msg.c_str(), L"Custom action call.", MB_OK | MB_TOPMOST); } } - catch (std::exception ex) { + catch (std::exception& ex) { // Improper JSON. } } @@ -180,7 +180,7 @@ class ExamplePowertoy : public PowertoyModuleIface { // Otherwise call a custom function to process the settings before saving them to disk: // save_settings(); } - catch (std::exception ex) { + catch (std::exception& ex) { // Improper JSON. } } @@ -221,7 +221,7 @@ void ExamplePowertoy::init_settings() { try { // Load and parse the settings file for this PowerToy. PowerToysSettings::PowerToyValues settings = - PowerToysSettings::PowerToyValues::load_from_settings_file(get_name()); + PowerToysSettings::PowerToyValues::load_from_settings_file(ExamplePowertoy::get_name()); // Load the bool property. if (settings.is_bool_value(L"test_bool_toggle")) { @@ -243,7 +243,7 @@ void ExamplePowertoy::init_settings() { g_settings.test_color_prop = settings.get_string_value(L"test_color_picker"); } } - catch (std::exception ex) { + catch (std::exception& ex) { // Error while loading from the settings file. Let default values stay as they are. } } @@ -282,7 +282,7 @@ void ExamplePowertoy::save_settings() { // Save the PowerToyValues JSON to the power toy settings file. values.save_to_settings_file(); } - catch (std::exception ex) { + catch (std::exception& ex) { // Couldn't save the settings. } } diff --git a/src/modules/fancyzones/dll/dllmain.cpp b/src/modules/fancyzones/dll/dllmain.cpp index 41c3e626778..299081bf2b4 100644 --- a/src/modules/fancyzones/dll/dllmain.cpp +++ b/src/modules/fancyzones/dll/dllmain.cpp @@ -197,7 +197,7 @@ class FancyZonesModule : public PowertoyModuleIface FancyZonesModule() { - m_settings = MakeFancyZonesSettings(reinterpret_cast(&__ImageBase), get_name()); + m_settings = MakeFancyZonesSettings(reinterpret_cast(&__ImageBase), FancyZonesModule::get_name()); } private: diff --git a/src/modules/fancyzones/lib/trace.cpp b/src/modules/fancyzones/lib/trace.cpp index 46503dd5035..f46a8fa5dbf 100644 --- a/src/modules/fancyzones/lib/trace.cpp +++ b/src/modules/fancyzones/lib/trace.cpp @@ -62,14 +62,14 @@ void Trace::FancyZones::ToggleZoneViewers(bool visible) noexcept TraceLoggingBoolean(visible, "Visible")); } -void Trace::FancyZones::OnKeyDown(DWORD vk, bool win, bool control, bool inMoveSize) noexcept +void Trace::FancyZones::OnKeyDown(DWORD vkCode, bool win, bool control, bool inMoveSize) noexcept { TraceLoggingWrite( g_hProvider, "FancyZones::Event::OnKeyDown", ProjectTelemetryPrivacyDataTag(ProjectTelemetryTag_ProductAndServicePerformance), TraceLoggingKeyword(PROJECT_KEYWORD_MEASURE), - TraceLoggingValue(vk, "Hotkey"), + TraceLoggingValue(vkCode, "Hotkey"), TraceLoggingBoolean(win, "WindowsKey"), TraceLoggingBoolean(control, "ControlKey"), TraceLoggingBoolean(inMoveSize, "InMoveSize")); diff --git a/src/modules/fancyzones/lib/trace.h b/src/modules/fancyzones/lib/trace.h index 0799efafa8c..2217b832378 100644 --- a/src/modules/fancyzones/lib/trace.h +++ b/src/modules/fancyzones/lib/trace.h @@ -29,7 +29,7 @@ class Trace Mouse }; - static void KeyUp(WPARAM wparam, bool isEditorMode) noexcept; + static void KeyUp(WPARAM wParam, bool isEditorMode) noexcept; static void MoveSizeEnd(_In_opt_ winrt::com_ptr activeSet) noexcept; static void CycleActiveZoneSet(_In_opt_ winrt::com_ptr activeSet, InputMode mode) noexcept; diff --git a/src/modules/shortcut_guide/overlay_window.cpp b/src/modules/shortcut_guide/overlay_window.cpp index 37988b22843..61bbc0054fd 100644 --- a/src/modules/shortcut_guide/overlay_window.cpp +++ b/src/modules/shortcut_guide/overlay_window.cpp @@ -154,7 +154,7 @@ D2D1_RECT_F D2DOverlaySVG::get_snap_right() const { } -D2DOverlayWindow::D2DOverlayWindow() : animation(0.3), total_screen({}) { +D2DOverlayWindow::D2DOverlayWindow() : total_screen({}), animation(0.3) { tasklist_thread = std::thread([&] { while (running) { // Removing causes C3538 on std::unique_lock lock(mutex); in show(..) @@ -443,7 +443,6 @@ void render_arrow(D2DSVG& arrow, TasklistButton& button, RECT window, float max_ // assume button is 25% wider than taller, +10% to make room for each of the arrows that are hidden auto render_arrow_width = (int)(button.height * 1.25f * 1.2f); auto render_arrow_height = (int)(render_arrow_width * arrow_ratio); - auto y_edge = dy == -1 ? button.y : button.y + button.height; arrow.resize(button.x + (button.width - render_arrow_width) / 2, dy == -1 ? button.y - render_arrow_height : 0, render_arrow_width, render_arrow_height, 0.95f, max_scale) diff --git a/src/modules/shortcut_guide/overlay_window.h b/src/modules/shortcut_guide/overlay_window.h index 6d387f70d71..09dd06454c8 100644 --- a/src/modules/shortcut_guide/overlay_window.h +++ b/src/modules/shortcut_guide/overlay_window.h @@ -49,7 +49,7 @@ class D2DOverlayWindow : public D2DWindow { private: void animate(int vk_code, int offset); - bool show_thumbnail(const RECT& rect_and_scale, double alpha); + bool show_thumbnail(const RECT& rect, double alpha); void hide_thumbnail(); virtual void init() override; virtual void resize() override; @@ -78,7 +78,7 @@ class D2DOverlayWindow : public D2DWindow { HTHUMBNAIL thumbnail; HWND active_window = nullptr; D2DOverlaySVG landscape, portrait; - D2DOverlaySVG* use_overlay; + D2DOverlaySVG* use_overlay = nullptr; D2DSVG no_active; std::vector arrows; std::chrono::steady_clock::time_point shown_start_time; diff --git a/src/modules/shortcut_guide/shortcut_guide.cpp b/src/modules/shortcut_guide/shortcut_guide.cpp index 607e1031ac8..3fbe8ddc1d7 100644 --- a/src/modules/shortcut_guide/shortcut_guide.cpp +++ b/src/modules/shortcut_guide/shortcut_guide.cpp @@ -70,7 +70,7 @@ void OverlayWindow::set_config(const wchar_t * config) { } _values.save_to_settings_file(); } - catch (std::exception ex) { + catch (std::exception&) { // Improper JSON. } } @@ -91,7 +91,6 @@ void OverlayWindow::disable() { if (_enabled) { winkey_popup->hide(); target_state->exit(); - int a = 0; delete target_state; delete winkey_popup; target_state = nullptr; @@ -156,7 +155,7 @@ void OverlayWindow::destroy() { void OverlayWindow::init_settings() { try { PowerToysSettings::PowerToyValues settings = - PowerToysSettings::PowerToyValues::load_from_settings_file(get_name()); + PowerToysSettings::PowerToyValues::load_from_settings_file(OverlayWindow::get_name()); if (settings.is_int_value(pressTime.name)) { pressTime.value = settings.get_int_value(pressTime.name); } @@ -164,7 +163,7 @@ void OverlayWindow::init_settings() { overlayOpacity.value = settings.get_int_value(overlayOpacity.name); } } - catch (std::exception ex) { + catch (std::exception&) { // Error while loading from the settings file. Just let default values stay as they are. } } diff --git a/src/modules/shortcut_guide/target_state.cpp b/src/modules/shortcut_guide/target_state.cpp index 6ad8fcfeab4..0ad35c3e9a2 100644 --- a/src/modules/shortcut_guide/target_state.cpp +++ b/src/modules/shortcut_guide/target_state.cpp @@ -119,7 +119,7 @@ void TargetState::handle_timeout() { std::unique_lock lock(mutex); auto wait_time = delay - (std::chrono::system_clock::now() - winkey_timestamp); if (events.empty()) - cv.wait_for(lock, delay); + cv.wait_for(lock, wait_time); if (state == Exiting) return; while (!events.empty()) { diff --git a/src/runner/general_settings.cpp b/src/runner/general_settings.cpp index 88c4f4feb26..d01e818b1b1 100644 --- a/src/runner/general_settings.cpp +++ b/src/runner/general_settings.cpp @@ -73,7 +73,7 @@ void start_initial_powertoys() { } only_enable_some_powertoys = true; } - catch (std::exception ex) { + catch (std::exception&) { // Couldn't read the general settings correctly. // Load all powertoys. only_enable_some_powertoys = false; diff --git a/src/runner/main.cpp b/src/runner/main.cpp index b4d501cd622..4edcb6a21d5 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -71,7 +71,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine Trace::EventLaunch(); result = run_message_loop(); - } catch (std::runtime_error err) { + } catch (std::runtime_error& err) { std::string err_what = err.what(); MessageBoxW(NULL, std::wstring(err_what.begin(),err_what.end()).c_str(), L"Error", MB_OK | MB_ICONERROR); result = -1; diff --git a/src/runner/powertoy_module.h b/src/runner/powertoy_module.h index c7005aab943..78a2313342f 100644 --- a/src/runner/powertoy_module.h +++ b/src/runner/powertoy_module.h @@ -10,7 +10,7 @@ class PowertoyModule; struct PowertoyModuleDeleter { - void operator()(PowertoyModuleIface* module) { + void operator()(PowertoyModuleIface* module) const { if (module) { powertoys_events().unregister_receiver(module); module->disable(); @@ -21,7 +21,7 @@ struct PowertoyModuleDeleter { struct PowertoyModuleDLLDeleter { using pointer = HMODULE; - void operator()(HMODULE handle) { + void operator()(HMODULE handle) const { FreeLibrary(handle); } }; diff --git a/src/runner/settings_window.cpp b/src/runner/settings_window.cpp index 85d7c632b81..c26813db76e 100644 --- a/src/runner/settings_window.cpp +++ b/src/runner/settings_window.cpp @@ -23,7 +23,7 @@ json::value get_power_toys_settings() { json::value powertoys_config = json::value::parse(powertoy.get_config()); result.as_object()[name] = powertoys_config; } - catch (json::json_exception ex) { + catch (json::json_exception&) { //Malformed JSON. } } diff --git a/tools/project_template/ModuleTemplate/dllmain.cpp b/tools/project_template/ModuleTemplate/dllmain.cpp index 23740db04ec..59684096f77 100644 --- a/tools/project_template/ModuleTemplate/dllmain.cpp +++ b/tools/project_template/ModuleTemplate/dllmain.cpp @@ -155,7 +155,7 @@ class $safeprojectname$ : public PowertoyModuleIface { } */ } - catch (std::exception ex) { + catch (std::exception& ex) { // Improper JSON. } } @@ -201,7 +201,7 @@ class $safeprojectname$ : public PowertoyModuleIface { // Otherwise call a custom function to process the settings before saving them to disk: // save_settings(); } - catch (std::exception ex) { + catch (std::exception& ex) { // Improper JSON. } } @@ -243,7 +243,7 @@ void $safeprojectname$::init_settings() { try { // Load and parse the settings file for this PowerToy. PowerToysSettings::PowerToyValues settings = - PowerToysSettings::PowerToyValues::load_from_settings_file(get_name()); + PowerToysSettings::PowerToyValues::load_from_settings_file($safeprojectname$::get_name()); // Load a bool property. /* @@ -273,7 +273,7 @@ void $safeprojectname$::init_settings() { } */ } - catch (std::exception ex) { + catch (std::exception& ex) { // Error while loading from the settings file. Let default values stay as they are. } }