Skip to content

Fix #1517: UnnecessaryLocalRule for variable in a try-catch-with-resources statement#1518

Draft
subha0319 wants to merge 1 commit intoyegor256:masterfrom
subha0319:master
Draft

Fix #1517: UnnecessaryLocalRule for variable in a try-catch-with-resources statement#1518
subha0319 wants to merge 1 commit intoyegor256:masterfrom
subha0319:master

Conversation

@subha0319
Copy link

Fixes #1517

The Problem:
UnnecessaryLocalRule was incorrectly flagging variables declared in a try-with-resources block as unnecessary if they were only referenced once inside the block. These variables cannot be inlined because they are required by Java syntax to ensure the resource is auto-closed.

The Solution:
Modified UnnecessaryLocalRule.java to check if the evaluated ASTVariableDeclarator is a descendant of an ASTResource node. If it is, the visitor returns early and ignores the variable.

Checklist

  • Updated UnnecessaryLocalRule.java to ignore ASTResource variables.
  • Added a valid test case representing the try-with-resources scenario.
  • Ran mvn clean install locally and all static analysis checks and tests pass successfully.

@subha0319 subha0319 marked this pull request as draft February 26, 2026 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnnecessaryLocalRule for variable in a try-catch-with-resources statement

1 participant