diff --git a/src/QtTrayMenu.cpp b/src/QtTrayMenu.cpp index cb35cf55..74d9e284 100644 --- a/src/QtTrayMenu.cpp +++ b/src/QtTrayMenu.cpp @@ -40,9 +40,9 @@ QtTrayMenu::QtTrayMenu(int argc, char **argv, QObject *parent, const bool debug) // Note: The following is ugly but QApplication requires an argv containing the application name. // We might not have access to the real argc/argv here due to being called/pulled as a dependency. if (argc < 0 && argv == nullptr) { - app = new QApplication(defaultArgc, defaultArgv); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management + app = new QApplication(defaultArgc, defaultArgv); // NOSONAR(cpp:S5025): Qt has its own integrated memory management } else { - app = new QApplication(argc, argv); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management + app = new QApplication(argc, argv); // NOSONAR(cpp:S5025): Qt has its own integrated memory management } } #if defined(_WIN32) @@ -59,7 +59,7 @@ QtTrayMenu::~QtTrayMenu() { // Quit QApplication QApplication::quit(); // Delete app and clear references - delete app; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management + delete app; // NOSONAR(cpp:S5025): Qt has its own integrated memory management app = nullptr; // Set to nullptr after deletion } } @@ -147,13 +147,13 @@ void QtTrayMenu::onExitRequested() { if (trayIcon) { trayIcon->setContextMenu(nullptr); } - delete trayTopMenu; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management + delete trayTopMenu; // NOSONAR(cpp:S5025): Qt has its own integrated memory management trayTopMenu = nullptr; // Set to nullptr after deletion } // Remove tray icon references; if (trayIcon) { trayIcon->hide(); - delete trayIcon; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management + delete trayIcon; // NOSONAR(cpp:S5025): Qt has its own integrated memory management trayIcon = nullptr; // Set to nullptr after deletion } // Unset tray structure @@ -167,7 +167,7 @@ void QtTrayMenu::onExitRequested() { void QtTrayMenu::updateMenu(struct tray_menu *items) { // Create and setup new tray menu instance - const auto newTrayTopMenu = new QMenu(); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management + const auto newTrayTopMenu = new QMenu(); // NOSONAR(cpp:S5025): Qt has its own integrated memory management #if defined(_WIN32) connect(newTrayTopMenu, &QMenu::aboutToShow, this, []() { tray_qt::windows::sync_color_scheme(); @@ -179,7 +179,7 @@ void QtTrayMenu::updateMenu(struct tray_menu *items) { // Clear old, unused trayTopMenu instance if (trayTopMenu != nullptr) { trayTopMenu->clear(); // Remove all actions - delete trayTopMenu; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management + delete trayTopMenu; // NOSONAR(cpp:S5025): Qt has its own integrated memory management } // Store reference for cleanup trayTopMenu = newTrayTopMenu; @@ -190,7 +190,7 @@ void QtTrayMenu::createMenu(struct tray_menu *items, QMenu *menu) { if (strcmp(items->text, "-") == 0) { menu->addSeparator(); } else { - auto *action = new QAction(QString::fromUtf8(items->text), menu); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management + auto *action = new QAction(QString::fromUtf8(items->text), menu); // NOSONAR(cpp:S5025): Qt has its own integrated memory management action->setDisabled(items->disabled == 1); action->setCheckable(items->checkbox == 1); action->setChecked(items->checked == 1); @@ -257,7 +257,7 @@ void QtTrayMenu::onMenuItemTriggered() { } } -struct tray_menu *QtTrayMenu::getTrayMenuItem(QAction *action) { // NOSONAR(cpp:S995) - Use as defined in function interface +struct tray_menu *QtTrayMenu::getTrayMenuItem(QAction *action) { // NOSONAR(cpp:S995): Use as defined in function interface return static_cast(action->property("tray_menu_item").value()); } diff --git a/src/example.c b/src/example.c index a4322553..6be432f7 100644 --- a/src/example.c +++ b/src/example.c @@ -20,7 +20,7 @@ static void toggle_cb(struct tray_menu *item) { tray_update(&tray); } -static void hello_cb(struct tray_menu *item) { +static void hello_cb(struct tray_menu *item) { // NOSONAR(c:S995): Mutable parameter required by tray_menu.cb. (void) item; printf("hello cb\n"); if (strcmp(tray.icon, TRAY_ICON1) == 0) { @@ -31,13 +31,13 @@ static void hello_cb(struct tray_menu *item) { tray_update(&tray); } -static void quit_cb(struct tray_menu *item) { +static void quit_cb(struct tray_menu *item) { // NOSONAR(c:S995): Mutable parameter required by tray_menu.cb. (void) item; printf("quit cb\n"); tray_exit(); } -static void submenu_cb(struct tray_menu *item) { +static void submenu_cb(struct tray_menu *item) { // NOSONAR(c:S995): Mutable parameter required by tray_menu.cb. (void) item; printf("submenu: clicked on %s\n", item->text); tray_update(&tray); diff --git a/src/tray_qt.cpp b/src/tray_qt.cpp index 079b3cc8..ebe34af6 100644 --- a/src/tray_qt.cpp +++ b/src/tray_qt.cpp @@ -21,27 +21,27 @@ namespace tray_qt { /** * QtTrayMenu instance */ - std::unique_ptr qt_tray_menu = nullptr; // NOSONAR(cpp:S5421) - mutable state, not const + std::unique_ptr qt_tray_menu = nullptr; // NOSONAR(cpp:S5421): mutable state, not const /** * Logging callback for qt_message_handler */ - void (*log_callback)(int, const char *) = nullptr; // NOSONAR(cpp:S5421) - mutable state, not const + void (*log_callback)(int, const char *) = nullptr; // NOSONAR(cpp:S5421): mutable state, not const /** * Explicit Qt application metadata configured through the C API. */ - bool app_info_configured = false; // NOSONAR(cpp:S5421) - mutable state, not const + bool app_info_configured = false; // NOSONAR(cpp:S5421): mutable state, not const /** * Qt application name configured through the C API. */ - QString app_name; // NOSONAR(cpp:S5421) - mutable state, not const + QString app_name; // NOSONAR(cpp:S5421): mutable state, not const /** * Qt application display name configured through the C API. */ - QString app_display_name; // NOSONAR(cpp:S5421) - mutable state, not const + QString app_display_name; // NOSONAR(cpp:S5421): mutable state, not const /** * Qt desktop file name configured through the C API. */ - QString desktop_name; // NOSONAR(cpp:S5421) - mutable state, not const + QString desktop_name; // NOSONAR(cpp:S5421): mutable state, not const /** * @brief Acknowledge/click current notification. @@ -179,7 +179,7 @@ extern "C" { return tray_qt::qt_tray_menu->loop(blocking); } - void tray_update(struct tray *tray) { // NOSONAR(cpp:S995) - C API requires this exact mutable-pointer signature + void tray_update(struct tray *tray) { // NOSONAR(cpp:S995): C API requires this exact mutable-pointer signature if (tray_qt::qt_tray_menu == nullptr) { return; } @@ -206,7 +206,7 @@ extern "C" { tray_qt::qt_tray_menu->exit(); } - void tray_set_log_callback(void (*cb)(int level, const char *msg)) { // NOSONAR(cpp:S5205) - C API requires a plain function pointer callback type + void tray_set_log_callback(void (*cb)(int level, const char *msg)) { // NOSONAR(cpp:S5205): C API requires a plain function pointer callback type tray_qt::log_callback = cb; if (cb != nullptr) { qInstallMessageHandler(tray_qt::qt_message_handler); diff --git a/tests/conftest.cpp b/tests/conftest.cpp index 933cd9fe..eab36db7 100644 --- a/tests/conftest.cpp +++ b/tests/conftest.cpp @@ -9,8 +9,9 @@ // test includes #include "tests/screenshot_utils.h" + // Undefine the original TEST macro -#undef TEST +#undef TEST // NOSONAR(cpp:S959): Tray tests extend the shared fixture with screenshot support. // Redefine TEST to use our BaseTest class, to automatically use our BaseTest fixture #define TEST(test_case_name, test_name) \ @@ -30,8 +31,7 @@ class BaseTest: public ::lizardbyte::common::testing::BaseTest { void SetUp() override { ::lizardbyte::common::testing::BaseTest::SetUp(); - // todo: only run this one time, instead of every time a test is run - // see: https://stackoverflow.com/questions/2435277/googletest-accessing-the-environment-from-a-test + // The shared test fixture caches the command-line arguments. // get command line args from the test executable testArgs_ = getArgs(); diff --git a/tests/notification_utils.cpp b/tests/notification_utils.cpp index 0d32a712..0bc35568 100644 --- a/tests/notification_utils.cpp +++ b/tests/notification_utils.cpp @@ -26,7 +26,7 @@ namespace { "id=$((id + 1)); " "done; " "fi"; - (void) std::system(close_notifications); // NOSONAR(cpp:S4721) - test-only cleanup of desktop notifications + (void) std::system(close_notifications); // NOSONAR(cpp:S4721): test-only cleanup of desktop notifications } } // namespace #endif diff --git a/tests/screenshot_utils.cpp b/tests/screenshot_utils.cpp index a7c85488..f74a2090 100644 --- a/tests/screenshot_utils.cpp +++ b/tests/screenshot_utils.cpp @@ -57,7 +57,7 @@ namespace { static bool dpiAware = false; std::call_once(dpiFlag, []() { using SetProcessDPIAwareFn = BOOL(WINAPI *)(); - auto *fn = reinterpret_cast( // NOSONAR(cpp:S3630) - required for GetProcAddress function pointer cast + auto *fn = reinterpret_cast( // NOSONAR(cpp:S3630): required for GetProcAddress function pointer cast GetProcAddress(GetModuleHandleA("user32.dll"), "SetProcessDPIAware") ); dpiAware = fn == nullptr || fn() == TRUE; @@ -90,7 +90,7 @@ namespace { namespace screenshot { inline std::filesystem::path &output_root_ref() { - static std::filesystem::path g_outputRoot; // NOSONAR(cpp:S6018) - function-local static is intentional for lazy, TU-local initialization + static std::filesystem::path g_outputRoot; // NOSONAR(cpp:S6018): function-local static is intentional for lazy, TU-local initialization return g_outputRoot; } diff --git a/tests/unit/test_tray.cpp b/tests/unit/test_tray.cpp index 35db24d7..115899b7 100644 --- a/tests/unit/test_tray.cpp +++ b/tests/unit/test_tray.cpp @@ -67,23 +67,23 @@ namespace { return {}; } - struct tray_menu g_submenu7_8[] = { // NOSONAR(cpp:S5945, cpp:S5421) - C-style array with null sentinel required by tray C API; mutable for runtime callback assignment + struct tray_menu g_submenu7_8[] = { // NOSONAR(cpp:S5945,cpp:S5421): C-style array with null sentinel required by tray C API; mutable for runtime callback assignment {.text = "7", .cb = nullptr}, {.text = "-"}, {.text = "8", .cb = nullptr}, {.text = nullptr} }; - struct tray_menu g_submenu5_6[] = { // NOSONAR(cpp:S5945, cpp:S5421) - C-style array with null sentinel required by tray C API; mutable for runtime callback assignment + struct tray_menu g_submenu5_6[] = { // NOSONAR(cpp:S5945,cpp:S5421): C-style array with null sentinel required by tray C API; mutable for runtime callback assignment {.text = "5", .cb = nullptr}, {.text = "6", .cb = nullptr}, {.text = nullptr} }; - struct tray_menu g_submenu_second[] = { // NOSONAR(cpp:S5945, cpp:S5421) - C-style array with null sentinel required by tray C API; mutable for runtime callback assignment + struct tray_menu g_submenu_second[] = { // NOSONAR(cpp:S5945,cpp:S5421): C-style array with null sentinel required by tray C API; mutable for runtime callback assignment {.text = "THIRD", .submenu = g_submenu7_8}, {.text = "FOUR", .submenu = g_submenu5_6}, {.text = nullptr} }; - struct tray_menu g_submenu[] = { // NOSONAR(cpp:S5945, cpp:S5421) - C-style array with null sentinel required by tray C API; mutable for runtime callback assignment + struct tray_menu g_submenu[] = { // NOSONAR(cpp:S5945,cpp:S5421): C-style array with null sentinel required by tray C API; mutable for runtime callback assignment {.text = "Hello", .cb = nullptr}, {.text = "Checked", .checked = 1, .checkbox = 1, .cb = nullptr}, {.text = "Disabled", .disabled = 1}, @@ -93,15 +93,15 @@ namespace { {.text = "Quit", .cb = nullptr}, {.text = nullptr} }; - struct tray g_testTray = { // NOSONAR(cpp:S5421) - mutable global required for shared tray state across TEST_F instances + struct tray g_testTray = { // NOSONAR(cpp:S5421): mutable global required for shared tray state across TEST_F instances .icon = TRAY_ICON1, .tooltip = "TestTray", .menu = g_submenu }; } // namespace -class TrayTest: public BaseTest { // NOSONAR(cpp:S3656) - fixture members must be protected for TEST_F-generated subclasses -protected: // NOSONAR(cpp:S3656) - TEST_F generates subclasses that need access to fixture state/methods +class TrayTest: public BaseTest { // NOSONAR(cpp:S3656): fixture members must be protected for TEST_F-generated subclasses +protected: // NOSONAR(cpp:S3656): TEST_F generates subclasses that need access to fixture state/methods void ShutdownTray() { if (!trayRunning) { return; @@ -147,27 +147,27 @@ class TrayTest: public BaseTest { // NOSONAR(cpp:S3656) - fixture members must capture_thread.join(); } - bool trayRunning {false}; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern - struct tray &testTray = g_testTray; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern - struct tray_menu *submenu = g_submenu; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern - struct tray_menu *submenu7_8 = g_submenu7_8; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern - struct tray_menu *submenu5_6 = g_submenu5_6; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern - struct tray_menu *submenu_second = g_submenu_second; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern + bool trayRunning {false}; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern + struct tray &testTray = g_testTray; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern + struct tray_menu *submenu = g_submenu; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern + struct tray_menu *submenu7_8 = g_submenu7_8; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern + struct tray_menu *submenu5_6 = g_submenu5_6; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern + struct tray_menu *submenu_second = g_submenu_second; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern static void hello_cb([[maybe_unused]] struct tray_menu *item) { // Mock implementation } - static void toggle_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type + static void toggle_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172): unused param required by tray_menu.cb function pointer type g_testTray.menu[1].checked = !g_testTray.menu[1].checked; tray_update(&g_testTray); } - static void quit_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type + static void quit_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172): unused param required by tray_menu.cb function pointer type tray_exit(); } - static void submenu_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type + static void submenu_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172): unused param required by tray_menu.cb function pointer type // Mock implementation tray_update(&g_testTray); } @@ -477,7 +477,7 @@ TEST_F(TrayTest, TestMenuItemContext) { static int contextValue = 42; static bool contextCallbackInvoked = false; - auto context_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S995) - must match tray_menu.cb signature void(*)(struct tray_menu*) + auto context_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S995): must match tray_menu.cb signature void(*)(struct tray_menu*) if (item->context != nullptr) { const auto *value = static_cast(item->context); contextCallbackInvoked = (*value == 42); @@ -516,7 +516,7 @@ TEST_F(TrayTest, TestCheckboxStates) { EXPECT_EQ(testTray.menu[1].checked, 1); // Show menu open with checkbox in checked state - captureMenuStateAndExit("tray_menu_checkbox_checked"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility + captureMenuStateAndExit("tray_menu_checkbox_checked"); // NOSONAR(cpp:S6168): helper uses std::thread for AppleClang 17 compatibility // Re-initialize tray with checkbox unchecked trayRunning = false; @@ -526,7 +526,7 @@ TEST_F(TrayTest, TestCheckboxStates) { ASSERT_EQ(initResult, 0); // Show menu open with checkbox in unchecked state - captureMenuStateAndExit("tray_menu_checkbox_unchecked"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility + captureMenuStateAndExit("tray_menu_checkbox_unchecked"); // NOSONAR(cpp:S6168): helper uses std::thread for AppleClang 17 compatibility // Restore initial checked state testTray.menu[1].checked = 1; @@ -600,7 +600,7 @@ TEST_F(TrayTest, TestTrayShowMenu) { ASSERT_EQ(initResult, 0); // Screenshot shows the full menu open, including the SubMenu entry that leads to nested items - captureMenuStateAndExit("tray_menu_shown"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility + captureMenuStateAndExit("tray_menu_shown"); // NOSONAR(cpp:S6168): helper uses std::thread for AppleClang 17 compatibility } TEST_F(TrayTest, TestTrayExit) { @@ -617,7 +617,7 @@ TEST_F(TrayTest, TestMenuAppearsOnLeftClick) { trayRunning = (initResult == 0); ASSERT_EQ(initResult, 0); - captureMenuStateAndExit("tray_menu_left_click"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility + captureMenuStateAndExit("tray_menu_left_click"); // NOSONAR(cpp:S6168): helper uses std::thread for AppleClang 17 compatibility } TEST_P(TrayNotificationIconTest, TestNotificationCallbackFiredOnClick) { @@ -660,7 +660,7 @@ TEST_F(TrayTest, TestMenuCallbackAfterNotificationUpdate) { static int callbackCount = 0; callbackCount = 0; - auto first_item_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type + auto first_item_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S1172): unused param required by tray_menu.cb function pointer type callbackCount++; (void) item; }; diff --git a/tests/unit/test_tray_qt.cpp b/tests/unit/test_tray_qt.cpp index 65d11354..4d8eb75f 100644 --- a/tests/unit/test_tray_qt.cpp +++ b/tests/unit/test_tray_qt.cpp @@ -50,8 +50,8 @@ namespace { } } // namespace -class TrayQtCoverageTest: public BaseTest { // NOSONAR(cpp:S3656) - fixture members/methods are accessed by TEST_F-generated subclasses -protected: // NOSONAR(cpp:S3656) - TEST_F requires protected fixture visibility +class TrayQtCoverageTest: public BaseTest { // NOSONAR(cpp:S3656): fixture members/methods are accessed by TEST_F-generated subclasses +protected: // NOSONAR(cpp:S3656): TEST_F requires protected fixture visibility void SetUp() override { BaseTest::SetUp(); @@ -67,7 +67,7 @@ class TrayQtCoverageTest: public BaseTest { // NOSONAR(cpp:S3656) - fixture mem menuItems = {{{.text = "Clickable", .cb = menu_item_cb}, {.text = "-"}, {.text = "Submenu", .submenu = submenuItems.data()}, {.text = "Disabled", .disabled = 1, .cb = menu_item_cb}, {.text = "Second Clickable", .cb = menu_item_cb}, {.text = nullptr}}}; trayDataStorage.assign(sizeof(struct tray), std::byte {0}); - trayData = reinterpret_cast(trayDataStorage.data()); // NOSONAR(cpp:S3630) - required to map a C flexible-array struct over raw storage + trayData = reinterpret_cast(trayDataStorage.data()); // NOSONAR(cpp:S3630): required to map a C flexible-array struct over raw storage trayData->icon = "icon.png"; trayData->tooltip = "Qt Tray Coverage"; trayData->notification_icon = nullptr; @@ -77,7 +77,7 @@ class TrayQtCoverageTest: public BaseTest { // NOSONAR(cpp:S3656) - fixture mem trayData->menu = menuItems.data(); const int iconPathCount = 0; - std::memcpy(const_cast(&trayData->iconPathCount), &iconPathCount, sizeof(iconPathCount)); // NOSONAR(cpp:S859) - required to initialize const member in C struct allocated via raw buffer + std::memcpy(const_cast(&trayData->iconPathCount), &iconPathCount, sizeof(iconPathCount)); // NOSONAR(cpp:S859): required to initialize const member in C struct allocated via raw buffer } void TearDown() override { @@ -285,7 +285,7 @@ TEST_F(TrayQtCoverageTest, ResolveTrayIconFromIconPathArray) { const size_t iconCount = 2; const size_t bufSize = sizeof(struct tray) + iconCount * sizeof(const char *); std::vector buf(bufSize, std::byte {0}); - auto *iconPathTray = reinterpret_cast(buf.data()); // NOSONAR(cpp:S3630) - reinterpret_cast is required to map a C flexible-array struct over raw storage + auto *iconPathTray = reinterpret_cast(buf.data()); // NOSONAR(cpp:S3630): reinterpret_cast is required to map a C flexible-array struct over raw storage iconPathTray->icon = "missing-icon-name"; iconPathTray->tooltip = "Icon path fallback"; @@ -296,11 +296,11 @@ TEST_F(TrayQtCoverageTest, ResolveTrayIconFromIconPathArray) { iconPathTray->menu = menuItems.data(); const auto countVal = static_cast(iconCount); - std::memcpy(const_cast(&iconPathTray->iconPathCount), &countVal, sizeof(countVal)); // NOSONAR(cpp:S859) - const member initialization is required for this C interop allocation pattern + std::memcpy(const_cast(&iconPathTray->iconPathCount), &countVal, sizeof(countVal)); // NOSONAR(cpp:S859): const member initialization is required for this C interop allocation pattern const char *badIcon = "missing-icon-name"; const char *goodIcon = "icon.png"; - std::memcpy(const_cast(&iconPathTray->allIconPaths[0]), &badIcon, sizeof(badIcon)); // NOSONAR(cpp:S859) - required to initialize const flexible-array entries - std::memcpy(const_cast(&iconPathTray->allIconPaths[1]), &goodIcon, sizeof(goodIcon)); // NOSONAR(cpp:S859) - required to initialize const flexible-array entries + std::memcpy(const_cast(&iconPathTray->allIconPaths[0]), &badIcon, sizeof(badIcon)); // NOSONAR(cpp:S859): required to initialize const flexible-array entries + std::memcpy(const_cast(&iconPathTray->allIconPaths[1]), &goodIcon, sizeof(goodIcon)); // NOSONAR(cpp:S859): required to initialize const flexible-array entries const int initResult = tray_init(iconPathTray); trayRunning = (initResult == 0);