[GITHUB-9198] Do not remove for each variables, even if unused.#9233
Open
lahodaj wants to merge 1 commit intoapache:masterfrom
Open
[GITHUB-9198] Do not remove for each variables, even if unused.#9233lahodaj wants to merge 1 commit intoapache:masterfrom
lahodaj wants to merge 1 commit intoapache:masterfrom
Conversation
mbien
reviewed
Feb 26, 2026
java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unused.java
Outdated
Show resolved
Hide resolved
mbien
approved these changes
Feb 26, 2026
Member
There was a problem hiding this comment.
The fix itself works as far as I can tell, the new RenameToUnderscore works also.
In future we could also try update the unused detector to show unused exception params, and other params like in lambdas. I think some of those are intentionally ignored atm before they even hit the hint.
try {
int p = Integer.parseInt("1");
} catch (NumberFormatException ignore) {} Map.of().computeIfAbsent("key", k -> "value");so that ignore and k are both suggested as _ candidates (right now its only p which is valid but probably not super useful)
| }) | ||
| public class Unused { | ||
|
|
||
| private static final SourceVersion UNNAMED_VARIABLES = SourceVersion.RELEASE_22; |
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.
For code like:
the
sis unused, and there's a fix proposed to remove the variable. That obviously cannot work.There's already a check in
Unusedthat disables that fix for binding patterns/variables. It could be enhanced for the for each, but it does not feel right - theJavaFixUtilities.safelyRemoveFromParentshould do that.So, this PR tweak
JavaFixUtilities.safelyRemoveFromParentso that it won't try to remove the binding variables, and for-each variables.It also adds a new fix to change the name of the variable to
_, for source level >= 22.closes #9198
^Add meaningful description above
Click to collapse/expand PR instructions
By opening a pull request you confirm that, unless explicitly stated otherwise, the changes -
Please make sure (eg.
git log) that all commits have a valid name and email address for you in the Author field.If you're a first time contributor, see the Contributing guidelines for more information.
If you're a committer, please label the PR before pressing "Create pull request" so that the right test jobs can run.
PR approval and merge checklist:
If this PR targets the delivery branch: don't merge. (full wiki article)