Skip to content

Commit f985aee

Browse files
jketemaCopilot
andcommitted
Test side effects in unused-variable queries
Cover side-effecting initializers for local and static variables, recording the current alerts as spurious. Also cover constructor and destructor side effects for static global objects. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 835afbb commit f985aee

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
| code2.cpp:64:7:64:8 | v3 | Variable v3 is not used. |
77
| code2.cpp:108:11:108:12 | v2 | Variable v2 is not used. |
88
| code2.cpp:128:9:128:9 | b | Variable b is not used. |
9+
| code2.cpp:194:6:194:16 | initialized | Variable initialized is not used. |
910
| code.c:10:18:10:18 | y | Variable y is not used. |
1011
| code.c:11:18:11:18 | z | Variable z is not used. |
1112
| code.c:18:7:18:7 | x | Variable x is not used. |

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,15 @@ void test_maybe_unused()
181181
{
182182
[[maybe_unused]] int unused; // GOOD: explicitly marked as maybe_unused
183183
}
184+
185+
int globalVar;
186+
187+
int initialize()
188+
{
189+
return ++globalVar;
190+
}
191+
192+
void test_initializer_side_effect()
193+
{
194+
int initialized = initialize(); // $ SPURIOUS: Alert // GOOD (initializer has a side effect)
195+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
| test.cpp:8:12:8:21 | staticVar6 | Static variable staticVar6 is never read. |
33
| test.cpp:10:11:10:19 | constVar8 | Static variable constVar8 is never read. |
44
| test.cpp:12:12:12:22 | staticVar10 | Static variable staticVar10 is never read. |
5+
| test.cpp:38:12:38:22 | initialized | Static variable initialized is never read. |

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,23 @@ class MyTemplateClass // never instantiated
2929
// ...
3030
}
3131
};
32+
33+
int initialize()
34+
{
35+
return ++globalVar;
36+
}
37+
38+
static int initialized = initialize(); // $ SPURIOUS: Alert // GOOD (initializer has a side effect)
39+
40+
struct WithConstructor
41+
{
42+
WithConstructor() { ++globalVar; }
43+
};
44+
45+
struct WithDestructor
46+
{
47+
~WithDestructor() { ++globalVar; }
48+
};
49+
50+
static WithConstructor constructed; // GOOD (constructor has a side effect)
51+
static WithDestructor destructed; // GOOD (destructor has a side effect)

0 commit comments

Comments
 (0)