-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
InstanceOfPatternMatch removes try-with-resource variable #4173
Comments
Thanks for the report! That would be a bug indeed; we should not make that change when the assignment is in a try-with-resources-try-block. Is this something you'd want to help fix? Even a draft PR with just a unit test added would already help. |
I forget in which Java version this was, but possibly 9, the syntax allows for an expression in try-with-resources and doesn't mandate declaring a new variable. So with an appropriate language version we can leverage that. |
@timtebeek I'll have a go at it. How to detect Java source version? |
Awesome, thanks for the offer to help! We determine the Java version when we parse the project, and store that in a marker called rewrite/rewrite-java/src/main/java/org/openrewrite/java/search/UsesJavaVersion.java Line 30 in 03c4584
In rare cases we one can also read the marker directly, but use that only when preconditions aren't an option. |
In this particular case we probably don't need anything in addition, as instanceof pattern variables were added in Java 11. So that precondition is already a given. |
Given the code
a rewrite using
InstanceOfPatternMatch
results inA fix could be to simply remove the parenthesis
but that may leave the object unclosed, so it should probably be left alone.
The text was updated successfully, but these errors were encountered: