Skip to content

Commit 24b6e60

Browse files
jketemaCopilot
andcommitted
Respect maybe_unused in unused-variable queries
Exclude variables annotated with the standard maybe_unused attribute from the unused-local and unused-static-variable queries, and update the regression expectations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6e75b96 commit 24b6e60

6 files changed

Lines changed: 5 additions & 5 deletions

File tree

‎cpp/ql/src/Best Practices/Unused Entities/UnusedLocals.ql‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ where
5757
not declarationHasSideEffects(v) and
5858
not exists(AsmStmt s | f = s.getEnclosingFunction()) and
5959
not v.getAnAttribute().getName() = "unused" and
60+
not v.getAnAttribute().getName() = "maybe_unused" and
6061
not f.hasErrors() // Unextracted expressions may use `v`
6162
select v, "Variable " + v.getName() + " is not used."

‎cpp/ql/src/Best Practices/Unused Entities/UnusedStaticVariables.ql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ where
2626
not v instanceof MemberVariable and
2727
not declarationHasSideEffects(v) and
2828
not v.getAnAttribute().hasName("used") and
29-
not v.getAnAttribute().hasName("unused")
29+
not v.getAnAttribute().hasName("unused") and
30+
not v.getAnAttribute().hasName("maybe_unused")
3031
select v, "Static variable " + v.getName() + " is never read."

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
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:182:23:182:28 | unused | Variable unused is not used. |
109
| code.c:10:18:10:18 | y | Variable y is not used. |
1110
| code.c:11:18:11:18 | z | Variable z is not used. |
1211
| 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,5 @@ void test_switch_initializer()
179179

180180
void test_maybe_unused()
181181
{
182-
[[maybe_unused]] int unused; // $ SPURIOUS: Alert // GOOD: false positive for maybe_unused
182+
[[maybe_unused]] int unused; // GOOD: explicitly marked as maybe_unused
183183
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
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:13:29:13:39 | staticVar11 | Static variable staticVar11 is never read. |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static __attribute__((__unused__)) int staticVar7; // GOOD (unused but this is e
1010
const int constVar8 = 8; // $ Alert // BAD (const defaults to static)
1111
extern const int constVar9 = 9; // GOOD
1212
static int staticVar10 = 10; // $ SPURIOUS: Alert // GOOD [FALSE POSITIVE] (referenced in a never instantiated template)
13-
[[maybe_unused]] static int staticVar11; // $ SPURIOUS: Alert // GOOD (false positive for maybe_unused)
13+
[[maybe_unused]] static int staticVar11; // GOOD (explicitly marked as maybe_unused)
1414

1515
void f()
1616
{

0 commit comments

Comments
 (0)