Skip to content

Commit 835afbb

Browse files
authored
Merge pull request #22932 from github/copilot/issue-22928-test-cases
Respect maybe_unused in unused-variable queries
2 parents c4d028d + 7b47e9b commit 835afbb

5 files changed

Lines changed: 14 additions & 2 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."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
The `cpp/unused-local-variable` and `cpp/unused-static-variable` queries no longer report variables marked with the C++17 `[[maybe_unused]]` attribute.

‎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 maybe_unused
183+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
// semmle-extractor-options: -std=c++17
22
int globalVar; // GOOD (not static)
33
static int staticVar1; // GOOD (used)
44
static int staticVar2; // GOOD (used)
@@ -10,6 +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; // GOOD (explicitly marked as maybe_unused)
1314

1415
void f()
1516
{

0 commit comments

Comments
 (0)