Skip to content

Commit 87d324c

Browse files
refactor(sonar): address sonar issues (#166)
1 parent 7ee861c commit 87d324c

8 files changed

Lines changed: 56 additions & 56 deletions

File tree

src/QtTrayMenu.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ QtTrayMenu::QtTrayMenu(int argc, char **argv, QObject *parent, const bool debug)
4040
// Note: The following is ugly but QApplication requires an argv containing the application name.
4141
// We might not have access to the real argc/argv here due to being called/pulled as a dependency.
4242
if (argc < 0 && argv == nullptr) {
43-
app = new QApplication(defaultArgc, defaultArgv); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
43+
app = new QApplication(defaultArgc, defaultArgv); // NOSONAR(cpp:S5025): Qt has its own integrated memory management
4444
} else {
45-
app = new QApplication(argc, argv); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
45+
app = new QApplication(argc, argv); // NOSONAR(cpp:S5025): Qt has its own integrated memory management
4646
}
4747
}
4848
#if defined(_WIN32)
@@ -59,7 +59,7 @@ QtTrayMenu::~QtTrayMenu() {
5959
// Quit QApplication
6060
QApplication::quit();
6161
// Delete app and clear references
62-
delete app; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
62+
delete app; // NOSONAR(cpp:S5025): Qt has its own integrated memory management
6363
app = nullptr; // Set to nullptr after deletion
6464
}
6565
}
@@ -147,13 +147,13 @@ void QtTrayMenu::onExitRequested() {
147147
if (trayIcon) {
148148
trayIcon->setContextMenu(nullptr);
149149
}
150-
delete trayTopMenu; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
150+
delete trayTopMenu; // NOSONAR(cpp:S5025): Qt has its own integrated memory management
151151
trayTopMenu = nullptr; // Set to nullptr after deletion
152152
}
153153
// Remove tray icon references;
154154
if (trayIcon) {
155155
trayIcon->hide();
156-
delete trayIcon; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
156+
delete trayIcon; // NOSONAR(cpp:S5025): Qt has its own integrated memory management
157157
trayIcon = nullptr; // Set to nullptr after deletion
158158
}
159159
// Unset tray structure
@@ -167,7 +167,7 @@ void QtTrayMenu::onExitRequested() {
167167

168168
void QtTrayMenu::updateMenu(struct tray_menu *items) {
169169
// Create and setup new tray menu instance
170-
const auto newTrayTopMenu = new QMenu(); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
170+
const auto newTrayTopMenu = new QMenu(); // NOSONAR(cpp:S5025): Qt has its own integrated memory management
171171
#if defined(_WIN32)
172172
connect(newTrayTopMenu, &QMenu::aboutToShow, this, []() {
173173
tray_qt::windows::sync_color_scheme();
@@ -179,7 +179,7 @@ void QtTrayMenu::updateMenu(struct tray_menu *items) {
179179
// Clear old, unused trayTopMenu instance
180180
if (trayTopMenu != nullptr) {
181181
trayTopMenu->clear(); // Remove all actions
182-
delete trayTopMenu; // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
182+
delete trayTopMenu; // NOSONAR(cpp:S5025): Qt has its own integrated memory management
183183
}
184184
// Store reference for cleanup
185185
trayTopMenu = newTrayTopMenu;
@@ -190,7 +190,7 @@ void QtTrayMenu::createMenu(struct tray_menu *items, QMenu *menu) {
190190
if (strcmp(items->text, "-") == 0) {
191191
menu->addSeparator();
192192
} else {
193-
auto *action = new QAction(QString::fromUtf8(items->text), menu); // NOSONAR(cpp:S5025) - Qt has its own integrated memory management
193+
auto *action = new QAction(QString::fromUtf8(items->text), menu); // NOSONAR(cpp:S5025): Qt has its own integrated memory management
194194
action->setDisabled(items->disabled == 1);
195195
action->setCheckable(items->checkbox == 1);
196196
action->setChecked(items->checked == 1);
@@ -257,7 +257,7 @@ void QtTrayMenu::onMenuItemTriggered() {
257257
}
258258
}
259259

260-
struct tray_menu *QtTrayMenu::getTrayMenuItem(QAction *action) { // NOSONAR(cpp:S995) - Use as defined in function interface
260+
struct tray_menu *QtTrayMenu::getTrayMenuItem(QAction *action) { // NOSONAR(cpp:S995): Use as defined in function interface
261261
return static_cast<struct tray_menu *>(action->property("tray_menu_item").value<void *>());
262262
}
263263

src/example.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static void toggle_cb(struct tray_menu *item) {
2020
tray_update(&tray);
2121
}
2222

23-
static void hello_cb(struct tray_menu *item) {
23+
static void hello_cb(struct tray_menu *item) { // NOSONAR(c:S995): Mutable parameter required by tray_menu.cb.
2424
(void) item;
2525
printf("hello cb\n");
2626
if (strcmp(tray.icon, TRAY_ICON1) == 0) {
@@ -31,13 +31,13 @@ static void hello_cb(struct tray_menu *item) {
3131
tray_update(&tray);
3232
}
3333

34-
static void quit_cb(struct tray_menu *item) {
34+
static void quit_cb(struct tray_menu *item) { // NOSONAR(c:S995): Mutable parameter required by tray_menu.cb.
3535
(void) item;
3636
printf("quit cb\n");
3737
tray_exit();
3838
}
3939

40-
static void submenu_cb(struct tray_menu *item) {
40+
static void submenu_cb(struct tray_menu *item) { // NOSONAR(c:S995): Mutable parameter required by tray_menu.cb.
4141
(void) item;
4242
printf("submenu: clicked on %s\n", item->text);
4343
tray_update(&tray);

src/tray_qt.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,27 @@ namespace tray_qt {
2121
/**
2222
* QtTrayMenu instance
2323
*/
24-
std::unique_ptr<QtTrayMenu> qt_tray_menu = nullptr; // NOSONAR(cpp:S5421) - mutable state, not const
24+
std::unique_ptr<QtTrayMenu> qt_tray_menu = nullptr; // NOSONAR(cpp:S5421): mutable state, not const
2525
/**
2626
* Logging callback for qt_message_handler
2727
*/
28-
void (*log_callback)(int, const char *) = nullptr; // NOSONAR(cpp:S5421) - mutable state, not const
28+
void (*log_callback)(int, const char *) = nullptr; // NOSONAR(cpp:S5421): mutable state, not const
2929
/**
3030
* Explicit Qt application metadata configured through the C API.
3131
*/
32-
bool app_info_configured = false; // NOSONAR(cpp:S5421) - mutable state, not const
32+
bool app_info_configured = false; // NOSONAR(cpp:S5421): mutable state, not const
3333
/**
3434
* Qt application name configured through the C API.
3535
*/
36-
QString app_name; // NOSONAR(cpp:S5421) - mutable state, not const
36+
QString app_name; // NOSONAR(cpp:S5421): mutable state, not const
3737
/**
3838
* Qt application display name configured through the C API.
3939
*/
40-
QString app_display_name; // NOSONAR(cpp:S5421) - mutable state, not const
40+
QString app_display_name; // NOSONAR(cpp:S5421): mutable state, not const
4141
/**
4242
* Qt desktop file name configured through the C API.
4343
*/
44-
QString desktop_name; // NOSONAR(cpp:S5421) - mutable state, not const
44+
QString desktop_name; // NOSONAR(cpp:S5421): mutable state, not const
4545

4646
/**
4747
* @brief Acknowledge/click current notification.
@@ -179,7 +179,7 @@ extern "C" {
179179
return tray_qt::qt_tray_menu->loop(blocking);
180180
}
181181

182-
void tray_update(struct tray *tray) { // NOSONAR(cpp:S995) - C API requires this exact mutable-pointer signature
182+
void tray_update(struct tray *tray) { // NOSONAR(cpp:S995): C API requires this exact mutable-pointer signature
183183
if (tray_qt::qt_tray_menu == nullptr) {
184184
return;
185185
}
@@ -206,7 +206,7 @@ extern "C" {
206206
tray_qt::qt_tray_menu->exit();
207207
}
208208

209-
void tray_set_log_callback(void (*cb)(int level, const char *msg)) { // NOSONAR(cpp:S5205) - C API requires a plain function pointer callback type
209+
void tray_set_log_callback(void (*cb)(int level, const char *msg)) { // NOSONAR(cpp:S5205): C API requires a plain function pointer callback type
210210
tray_qt::log_callback = cb;
211211
if (cb != nullptr) {
212212
qInstallMessageHandler(tray_qt::qt_message_handler);

tests/conftest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
// test includes
1111
#include "tests/screenshot_utils.h"
12+
1213
// Undefine the original TEST macro
13-
#undef TEST
14+
#undef TEST // NOSONAR(cpp:S959): Tray tests extend the shared fixture with screenshot support.
1415

1516
// Redefine TEST to use our BaseTest class, to automatically use our BaseTest fixture
1617
#define TEST(test_case_name, test_name) \
@@ -30,8 +31,7 @@ class BaseTest: public ::lizardbyte::common::testing::BaseTest {
3031
void SetUp() override {
3132
::lizardbyte::common::testing::BaseTest::SetUp();
3233

33-
// todo: only run this one time, instead of every time a test is run
34-
// see: https://stackoverflow.com/questions/2435277/googletest-accessing-the-environment-from-a-test
34+
// The shared test fixture caches the command-line arguments.
3535
// get command line args from the test executable
3636
testArgs_ = getArgs();
3737

tests/notification_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace {
2626
"id=$((id + 1)); "
2727
"done; "
2828
"fi";
29-
(void) std::system(close_notifications); // NOSONAR(cpp:S4721) - test-only cleanup of desktop notifications
29+
(void) std::system(close_notifications); // NOSONAR(cpp:S4721): test-only cleanup of desktop notifications
3030
}
3131
} // namespace
3232
#endif

tests/screenshot_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace {
5757
static bool dpiAware = false;
5858
std::call_once(dpiFlag, []() {
5959
using SetProcessDPIAwareFn = BOOL(WINAPI *)();
60-
auto *fn = reinterpret_cast<SetProcessDPIAwareFn>( // NOSONAR(cpp:S3630) - required for GetProcAddress function pointer cast
60+
auto *fn = reinterpret_cast<SetProcessDPIAwareFn>( // NOSONAR(cpp:S3630): required for GetProcAddress function pointer cast
6161
GetProcAddress(GetModuleHandleA("user32.dll"), "SetProcessDPIAware")
6262
);
6363
dpiAware = fn == nullptr || fn() == TRUE;
@@ -90,7 +90,7 @@ namespace {
9090
namespace screenshot {
9191

9292
inline std::filesystem::path &output_root_ref() {
93-
static std::filesystem::path g_outputRoot; // NOSONAR(cpp:S6018) - function-local static is intentional for lazy, TU-local initialization
93+
static std::filesystem::path g_outputRoot; // NOSONAR(cpp:S6018): function-local static is intentional for lazy, TU-local initialization
9494
return g_outputRoot;
9595
}
9696

tests/unit/test_tray.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@ namespace {
6767
return {};
6868
}
6969

70-
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
70+
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
7171
{.text = "7", .cb = nullptr},
7272
{.text = "-"},
7373
{.text = "8", .cb = nullptr},
7474
{.text = nullptr}
7575
};
76-
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
76+
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
7777
{.text = "5", .cb = nullptr},
7878
{.text = "6", .cb = nullptr},
7979
{.text = nullptr}
8080
};
81-
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
81+
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
8282
{.text = "THIRD", .submenu = g_submenu7_8},
8383
{.text = "FOUR", .submenu = g_submenu5_6},
8484
{.text = nullptr}
8585
};
86-
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
86+
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
8787
{.text = "Hello", .cb = nullptr},
8888
{.text = "Checked", .checked = 1, .checkbox = 1, .cb = nullptr},
8989
{.text = "Disabled", .disabled = 1},
@@ -93,15 +93,15 @@ namespace {
9393
{.text = "Quit", .cb = nullptr},
9494
{.text = nullptr}
9595
};
96-
struct tray g_testTray = { // NOSONAR(cpp:S5421) - mutable global required for shared tray state across TEST_F instances
96+
struct tray g_testTray = { // NOSONAR(cpp:S5421): mutable global required for shared tray state across TEST_F instances
9797
.icon = TRAY_ICON1,
9898
.tooltip = "TestTray",
9999
.menu = g_submenu
100100
};
101101
} // namespace
102102

103-
class TrayTest: public BaseTest { // NOSONAR(cpp:S3656) - fixture members must be protected for TEST_F-generated subclasses
104-
protected: // NOSONAR(cpp:S3656) - TEST_F generates subclasses that need access to fixture state/methods
103+
class TrayTest: public BaseTest { // NOSONAR(cpp:S3656): fixture members must be protected for TEST_F-generated subclasses
104+
protected: // NOSONAR(cpp:S3656): TEST_F generates subclasses that need access to fixture state/methods
105105
void ShutdownTray() {
106106
if (!trayRunning) {
107107
return;
@@ -147,27 +147,27 @@ class TrayTest: public BaseTest { // NOSONAR(cpp:S3656) - fixture members must
147147
capture_thread.join();
148148
}
149149

150-
bool trayRunning {false}; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
151-
struct tray &testTray = g_testTray; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
152-
struct tray_menu *submenu = g_submenu; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
153-
struct tray_menu *submenu7_8 = g_submenu7_8; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
154-
struct tray_menu *submenu5_6 = g_submenu5_6; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
155-
struct tray_menu *submenu_second = g_submenu_second; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
150+
bool trayRunning {false}; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern
151+
struct tray &testTray = g_testTray; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern
152+
struct tray_menu *submenu = g_submenu; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern
153+
struct tray_menu *submenu7_8 = g_submenu7_8; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern
154+
struct tray_menu *submenu5_6 = g_submenu5_6; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern
155+
struct tray_menu *submenu_second = g_submenu_second; // NOSONAR(cpp:S3656): protected access required by gtest TEST_F subclass pattern
156156

157157
static void hello_cb([[maybe_unused]] struct tray_menu *item) {
158158
// Mock implementation
159159
}
160160

161-
static void toggle_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type
161+
static void toggle_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172): unused param required by tray_menu.cb function pointer type
162162
g_testTray.menu[1].checked = !g_testTray.menu[1].checked;
163163
tray_update(&g_testTray);
164164
}
165165

166-
static void quit_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type
166+
static void quit_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172): unused param required by tray_menu.cb function pointer type
167167
tray_exit();
168168
}
169169

170-
static void submenu_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type
170+
static void submenu_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172): unused param required by tray_menu.cb function pointer type
171171
// Mock implementation
172172
tray_update(&g_testTray);
173173
}
@@ -477,7 +477,7 @@ TEST_F(TrayTest, TestMenuItemContext) {
477477
static int contextValue = 42;
478478
static bool contextCallbackInvoked = false;
479479

480-
auto context_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S995) - must match tray_menu.cb signature void(*)(struct tray_menu*)
480+
auto context_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S995): must match tray_menu.cb signature void(*)(struct tray_menu*)
481481
if (item->context != nullptr) {
482482
const auto *value = static_cast<const int *>(item->context);
483483
contextCallbackInvoked = (*value == 42);
@@ -516,7 +516,7 @@ TEST_F(TrayTest, TestCheckboxStates) {
516516
EXPECT_EQ(testTray.menu[1].checked, 1);
517517

518518
// Show menu open with checkbox in checked state
519-
captureMenuStateAndExit("tray_menu_checkbox_checked"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility
519+
captureMenuStateAndExit("tray_menu_checkbox_checked"); // NOSONAR(cpp:S6168): helper uses std::thread for AppleClang 17 compatibility
520520

521521
// Re-initialize tray with checkbox unchecked
522522
trayRunning = false;
@@ -526,7 +526,7 @@ TEST_F(TrayTest, TestCheckboxStates) {
526526
ASSERT_EQ(initResult, 0);
527527

528528
// Show menu open with checkbox in unchecked state
529-
captureMenuStateAndExit("tray_menu_checkbox_unchecked"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility
529+
captureMenuStateAndExit("tray_menu_checkbox_unchecked"); // NOSONAR(cpp:S6168): helper uses std::thread for AppleClang 17 compatibility
530530

531531
// Restore initial checked state
532532
testTray.menu[1].checked = 1;
@@ -600,7 +600,7 @@ TEST_F(TrayTest, TestTrayShowMenu) {
600600
ASSERT_EQ(initResult, 0);
601601

602602
// Screenshot shows the full menu open, including the SubMenu entry that leads to nested items
603-
captureMenuStateAndExit("tray_menu_shown"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility
603+
captureMenuStateAndExit("tray_menu_shown"); // NOSONAR(cpp:S6168): helper uses std::thread for AppleClang 17 compatibility
604604
}
605605

606606
TEST_F(TrayTest, TestTrayExit) {
@@ -617,7 +617,7 @@ TEST_F(TrayTest, TestMenuAppearsOnLeftClick) {
617617
trayRunning = (initResult == 0);
618618
ASSERT_EQ(initResult, 0);
619619

620-
captureMenuStateAndExit("tray_menu_left_click"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility
620+
captureMenuStateAndExit("tray_menu_left_click"); // NOSONAR(cpp:S6168): helper uses std::thread for AppleClang 17 compatibility
621621
}
622622

623623
TEST_P(TrayNotificationIconTest, TestNotificationCallbackFiredOnClick) {
@@ -660,7 +660,7 @@ TEST_F(TrayTest, TestMenuCallbackAfterNotificationUpdate) {
660660
static int callbackCount = 0;
661661
callbackCount = 0;
662662

663-
auto first_item_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type
663+
auto first_item_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S1172): unused param required by tray_menu.cb function pointer type
664664
callbackCount++;
665665
(void) item;
666666
};

0 commit comments

Comments
 (0)