Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ where
not declarationHasSideEffects(v) and
not exists(AsmStmt s | f = s.getEnclosingFunction()) and
not v.getAnAttribute().getName() = "unused" and
not v.getAnAttribute().getName() = "maybe_unused" and
not f.hasErrors() // Unextracted expressions may use `v`
select v, "Variable " + v.getName() + " is not used."
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ where
not v instanceof MemberVariable and
not declarationHasSideEffects(v) and
not v.getAnAttribute().hasName("used") and
not v.getAnAttribute().hasName("unused")
not v.getAnAttribute().hasName("unused") and
not v.getAnAttribute().hasName("maybe_unused")
select v, "Static variable " + v.getName() + " is never read."
4 changes: 4 additions & 0 deletions cpp/ql/src/change-notes/2026-10-29-unused-maybe-unused.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
The `cpp/unused-local-variable` and `cpp/unused-static-variable` queries no longer report variables marked with the C++17 `[[maybe_unused]]` attribute.
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,8 @@ void test_switch_initializer()
// ...
}
}

void test_maybe_unused()
{
[[maybe_unused]] int unused; // GOOD: explicitly marked as maybe_unused
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

// semmle-extractor-options: -std=c++17
int globalVar; // GOOD (not static)
static int staticVar1; // GOOD (used)
static int staticVar2; // GOOD (used)
Expand All @@ -10,6 +10,7 @@ static __attribute__((__unused__)) int staticVar7; // GOOD (unused but this is e
const int constVar8 = 8; // $ Alert // BAD (const defaults to static)
extern const int constVar9 = 9; // GOOD
static int staticVar10 = 10; // $ SPURIOUS: Alert // GOOD [FALSE POSITIVE] (referenced in a never instantiated template)
[[maybe_unused]] static int staticVar11; // GOOD (explicitly marked as maybe_unused)

void f()
{
Expand Down
Loading