Skip to content

Commit 2a48cb5

Browse files
jketemaCopilot
andcommitted
Add unused-variable regression cases
Cover C++17 maybe_unused locals and static variables, plus static registrations with side-effecting initializers. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 748b492 commit 2a48cb5

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

  • cpp/ql/test/query-tests/Best Practices/Unused Entities

‎cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedLocals/code2.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,8 @@ void test_switch_initializer()
176176
// ...
177177
}
178178
}
179+
180+
void test_maybe_unused()
181+
{
182+
[[maybe_unused]] int unused; // GOOD: explicitly marked as unused
183+
}

‎cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
// semmle-extractor-options: -std=c++17
3+
24
int globalVar; // GOOD (not static)
35
static int staticVar1; // GOOD (used)
46
static int staticVar2; // GOOD (used)
@@ -10,6 +12,17 @@ static __attribute__((__unused__)) int staticVar7; // GOOD (unused but this is e
1012
const int constVar8 = 8; // $ Alert // BAD (const defaults to static)
1113
extern const int constVar9 = 9; // GOOD
1214
static int staticVar10 = 10; // $ SPURIOUS: Alert // GOOD [FALSE POSITIVE] (referenced in a never instantiated template)
15+
[[maybe_unused]] static int staticVar11; // GOOD (explicitly marked as unused)
16+
17+
static int registrationCount;
18+
19+
int* registerBenchmark()
20+
{
21+
++registrationCount;
22+
return &registrationCount;
23+
}
24+
25+
static int* benchmarkRegistration = registerBenchmark(); // GOOD (initializer has a side effect)
1326

1427
void f()
1528
{

0 commit comments

Comments
 (0)