Repository navigation
Java: Exclude unnamed variables from java/local-variable-is-never-read - #22994
Merged
aschackmull merged 1 commit intoNov 3, 2026
Merged
Conversation
An unnamed variable (`_`, Java 22) cannot be read, so reporting it is always a false positive. Fixes github#22993. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the targeted Java 22 unnamed-variable query update.
No open issues 🎉
What changed in this PR
Updates the Java local-variable-is-never-read query to ignore Java 22 unnamed variables, which cannot be read and should not produce alerts.
Changes:
- Excludes anonymous local variable declarations from query results.
- Adds Java 22 regression coverage for unnamed variables in patterns, enhanced
forloops, and local declarations. - Configures the test directory for Java 22 compilation.
- Documents the behavior change.
| File | Description |
|---|---|
java/ql/test/query-tests/UnreadLocal/UnreadLocal/UnnamedVariables.java |
Adds Java 22 regression cases for unnamed variables. |
java/ql/test/query-tests/UnreadLocal/options |
Enables Java 22 compilation for the test directory. |
java/ql/src/Violations of Best Practice/Dead Code/UnreadLocal.ql |
Excludes unnamed variables from query results. |
java/ql/src/change-notes/2026-11-03-unread-local-unnamed-variables.md |
Documents the query behavior change. |
This pull request is ready to be approved.
Review effort: Lite (auto)
aschackmull
approved these changes
Nov 3, 2026
aschackmull
left a comment
Contributor
There was a problem hiding this comment.
LGTM, thanks for the contribution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22993.
java/local-variable-is-never-readreports unnamed variables (_, Java 22), for examplecase Foo _ ->. An unnamed variable cannot be read, so every such alert is a false positive.The query now skips variables whose declaration
isAnonymous(). The source version already decides which variables that matches, so the query needs no version check of its own. Before Java 9,_is an ordinary identifier: the extractor keeps its name, and an unread_is still reported. Java 9 to 20 reject_as a name. From Java 22, or Java 21 with preview features,_declares an unnamed variable, which the extractor gives an empty name.UnnamedVariables.javacovers unnamed variables in switch type patterns, record patterns, an enhancedforloop, and a local declaration. The newoptionsfile compiles the test directory with--release 22, asprintAstandpattern-switch/cfgalready do. None of the new cases produce a result, so the three.expectedfiles in the directory are unchanged.Tested locally with CodeQL CLI 2.27.4. Without the query change,
UnreadLocal.qlreffails with five unexpected alerts, one per case inUnnamedVariables.java. With it, all three tests in the directory pass. A separate run with--release 8confirmed that an unreadint _ = 5;is still reported.🤖 Generated with Claude Code