Skip to content

Do not add final to a try-with-resources resource - #1232

Merged
greg-at-moderne merged 1 commit into
openrewrite:mainfrom
sullis:lombok-val-try-with-resources
Sep 3, 2026
Merged

Do not add final to a try-with-resources resource#1232
greg-at-moderne merged 1 commit into
openrewrite:mainfrom
sullis:lombok-val-try-with-resources

Conversation

@sullis

@sullis sullis commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

LombokValToFinalVar rewrote val to final var unconditionally. Resources declared in a try-with-resources statement are already implicitly final, so the added final modifier is redundant there.

This checks whether the variable declaration's parent is a J.Try.Resource and, if so, emits plain var instead of final var.

// before
try (val writer = new StringWriter()) {
    writer.append(aaa);
}

// after
try (var writer = new StringWriter()) {
    writer.append(aaa);
}

Adds a test covering the try-with-resources case.

🤖 Generated with Claude Code

@sullis

sullis commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Ready for review @greg-at-moderne

@greg-at-moderne
greg-at-moderne self-requested a review September 2, 2026 21:02
@sullis

sullis commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

CI build is green.

@greg-at-moderne greg-at-moderne 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.

Another great contribution. Thank you!

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Sep 3, 2026
@greg-at-moderne
greg-at-moderne force-pushed the lombok-val-try-with-resources branch from fcc1c8a to bfc708f Compare September 3, 2026 11:43
@greg-at-moderne
greg-at-moderne merged commit 61192e1 into openrewrite:main Sep 3, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants