Skip to content

Respect initializer side effects in unused-variable queries - #22938

Merged
jketema merged 5 commits into
mainfrom
copilot/unused-static-side-effect-test
Oct 30, 2026
Merged

jketema merged 5 commits into
mainfrom
copilot/unused-static-side-effect-test

Conversation

@jketema

@jketema jketema commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

The cpp/unused-local-variable and cpp/unused-static-variable queries now exclude variables whose initializers may have side effects, using Expr.mayBeImpure() while preserving the existing constructor/destructor exemptions.

Adds regression coverage for side-effecting local and static initializers, plus separate constructor and destructor cases for static global objects. Updates the range-for expectation because iterator dereference may have side effects, regenerates expected results, and adds a minor analysis change note.

Fixes: #22928.

Validation: both affected query tests pass with CodeQL CLI 2.27.3.

@github-actions github-actions Bot added the C++ label Oct 29, 2026
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>
@jketema
jketema force-pushed the copilot/unused-static-side-effect-test branch from bf32c2e to f985aee Compare October 29, 2026 15:13
jketema and others added 3 commits October 29, 2026 15:16
Exclude local and static variables whose initializer may be impure, preserving existing constructor and destructor exemptions. Regenerate expected results and mark the range-for case as missing because its undefined iterator dereference may have side effects.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add a minor analysis change note for suppressing alerts on variables whose initializers may have side effects.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Treat the unused range-for variable as intentionally unreported because its initializer may have side effects.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jketema jketema changed the title Test unused static variable with side-effecting initializer Respect initializer side effects in unused-variable queries Oct 29, 2026
}

for (int v2 : myContainer) // $ Alert // BAD: v2 is not used
for (int v2 : myContainer) // GOOD: iterator dereference may have side effects

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the relevant member functions in MyContainer are missing function bodies, so we don't known if there may be side-effects.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would hope that iterating through a container doesn't have side-effects in the vast majority of cases. But this is C++, people are bound to have come up with containers where they do.

@jketema
jketema marked this pull request as ready for review October 29, 2026 16:08
@jketema
jketema requested a review from a team as a code owner October 29, 2026 16:08
Copilot AI auto review requested due to automatic review settings October 29, 2026 16:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the unused-variable query changes.

No open issues 🎉

What changed in this PR

This PR reduces false positives in the C++ unused-variable queries by preserving declarations whose initializers may have side effects, addressing the initializer-related behavior in #22928.

Changes:

  • Uses Expr.mayBeImpure() in both queries while retaining existing constructor/destructor exemptions.
  • Adds regression coverage for side-effecting initializers and static-object constructors/destructors, and updates the range-for expectation.
  • Adds a minor analysis change note.
File Description
cpp/​ql/​test/​query-tests/​Best Practices/​Unused Entities/​UnusedStaticVariables/​test.cpp Adds initializer, constructor, and destructor regression cases.
cpp/​ql/​test/​query-tests/​Best Practices/​Unused Entities/​UnusedLocals/​UnusedLocals.expected Removes the expected range-for alert.
cpp/​ql/​test/​query-tests/​Best Practices/​Unused Entities/​UnusedLocals/​code2.cpp Adds an initializer regression case and updates the range-for annotation.
cpp/​ql/​src/​change-notes/​2026-10-29-unused-variable-initializers.md Documents the queries’ reduced false positives.
cpp/​ql/​src/​Best Practices/​Unused Entities/​UnusedStaticVariables.ql Excludes static variables with potentially impure initializers.
cpp/​ql/​src/​Best Practices/​Unused Entities/​UnusedLocals.ql Excludes local variables with potentially impure initializers.

Review effort: Balanced (auto)

@geoffw0 geoffw0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. ✨

}

for (int v2 : myContainer) // $ Alert // BAD: v2 is not used
for (int v2 : myContainer) // GOOD: iterator dereference may have side effects

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would hope that iterating through a container doesn't have side-effects in the vast majority of cases. But this is C++, people are bound to have come up with containers where they do.

@jketema
jketema merged commit 0a5ba1a into main Oct 30, 2026
20 checks passed
@jketema
jketema deleted the copilot/unused-static-side-effect-test branch October 30, 2026 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C++ unused-variable queries flag standard maybe_unused and side-effecting benchmark registrations

3 participants