From b43b7c383bc1b4d549f410c59164b75c765bb345 Mon Sep 17 00:00:00 2001 From: jketema <93738568+jketema@users.noreply.github.com> Date: Fri, 30 Oct 2026 14:49:27 +0000 Subject: [PATCH 1/2] Add empty parameter pack regression test Extend the unused static variable query test with a minimal variadic sum instantiated with empty and non-empty packs. Record the spurious alert on the compiler-generated empty-pack variable reported in #22931. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../UnusedStaticVariables/UnusedStaticVariables.expected | 1 + .../Unused Entities/UnusedStaticVariables/test.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/UnusedStaticVariables.expected b/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/UnusedStaticVariables.expected index 45367233424e..130c5f098ba7 100644 --- a/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/UnusedStaticVariables.expected +++ b/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/UnusedStaticVariables.expected @@ -2,3 +2,4 @@ | test.cpp:8:12:8:21 | staticVar6 | Static variable staticVar6 is never read. | | test.cpp:10:11:10:19 | constVar8 | Static variable constVar8 is never read. | | test.cpp:12:12:12:22 | staticVar10 | Static variable staticVar10 is never read. | +| test.cpp:53:25:53:30 | values | Static variable values is never read. | diff --git a/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp b/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp index 7a18491408c6..6bf5b30e9a46 100644 --- a/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp +++ b/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp @@ -1,4 +1,4 @@ -// semmle-extractor-options: -std=c++17 +// semmle-extractor-options: -std=c++20 int globalVar; // GOOD (not static) static int staticVar1; // GOOD (used) static int staticVar2; // GOOD (used) @@ -49,3 +49,10 @@ struct WithDestructor static WithConstructor constructed; // GOOD (constructor has a side effect) static WithDestructor destructed; // GOOD (destructor has a side effect) + +static auto sum(auto... values) // $ SPURIOUS: Alert +{ + return (0 + ... + values); +} + +int globalVar2 = sum() + sum(2); From a5e0a2dad3c474943b615855a6465ab2e83dbe7b Mon Sep 17 00:00:00 2001 From: jketema <93738568+jketema@users.noreply.github.com> Date: Fri, 30 Oct 2026 14:51:33 +0000 Subject: [PATCH 2/2] Ignore compiler-generated unused static variables Exclude synthetic variables from cpp/unused-static-variable so empty parameter pack placeholders do not trigger alerts. Update the regression expectation and document the analysis change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Best Practices/Unused Entities/UnusedStaticVariables.ql | 1 + .../2026-10-30-unused-static-compiler-generated.md | 4 ++++ .../UnusedStaticVariables/UnusedStaticVariables.expected | 1 - .../Unused Entities/UnusedStaticVariables/test.cpp | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 cpp/ql/src/change-notes/2026-10-30-unused-static-compiler-generated.md diff --git a/cpp/ql/src/Best Practices/Unused Entities/UnusedStaticVariables.ql b/cpp/ql/src/Best Practices/Unused Entities/UnusedStaticVariables.ql index c67bac65bdd3..442f75027d82 100644 --- a/cpp/ql/src/Best Practices/Unused Entities/UnusedStaticVariables.ql +++ b/cpp/ql/src/Best Practices/Unused Entities/UnusedStaticVariables.ql @@ -22,6 +22,7 @@ predicate declarationHasSideEffects(Variable v) { from Variable v where v.isStatic() and + not v.isCompilerGenerated() and v.hasDefinition() and not v.isConstexpr() and not exists(VariableAccess a | a.getTarget() = v) and diff --git a/cpp/ql/src/change-notes/2026-10-30-unused-static-compiler-generated.md b/cpp/ql/src/change-notes/2026-10-30-unused-static-compiler-generated.md new file mode 100644 index 000000000000..4ede33c64a12 --- /dev/null +++ b/cpp/ql/src/change-notes/2026-10-30-unused-static-compiler-generated.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The `cpp/unused-static-variable` query no longer reports compiler-generated variables, such as placeholders for empty function parameter packs. diff --git a/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/UnusedStaticVariables.expected b/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/UnusedStaticVariables.expected index 130c5f098ba7..45367233424e 100644 --- a/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/UnusedStaticVariables.expected +++ b/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/UnusedStaticVariables.expected @@ -2,4 +2,3 @@ | test.cpp:8:12:8:21 | staticVar6 | Static variable staticVar6 is never read. | | test.cpp:10:11:10:19 | constVar8 | Static variable constVar8 is never read. | | test.cpp:12:12:12:22 | staticVar10 | Static variable staticVar10 is never read. | -| test.cpp:53:25:53:30 | values | Static variable values is never read. | diff --git a/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp b/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp index 6bf5b30e9a46..13ab913733bb 100644 --- a/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp +++ b/cpp/ql/test/query-tests/Best Practices/Unused Entities/UnusedStaticVariables/test.cpp @@ -50,7 +50,7 @@ struct WithDestructor static WithConstructor constructed; // GOOD (constructor has a side effect) static WithDestructor destructed; // GOOD (destructor has a side effect) -static auto sum(auto... values) // $ SPURIOUS: Alert +static auto sum(auto... values) { return (0 + ... + values); }