Skip to content
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 declares variable with conflicting name #334

Open
knutwannheden opened this issue Feb 6, 2023 · 2 comments
Open
Labels
bug Something isn't working

Comments

@knutwannheden
Copy link
Contributor

knutwannheden commented Feb 6, 2023

The InstanceOfPatternMatch declares a pattern variable with a conflicting name if a variable with that name already exists in the if body after the corresponding type cast as in the following example:

void test(Object o) {
  if (o instanceof String) {
    System.out.println((String) o);
    String string = "x";
  }
}

Currently the recipe will change this to the following illegal code:

void test(Object o) {
  if (o instanceof String string) {
    System.out.println(string);
    String string = "x";
  }
}

Fixing openrewrite/rewrite#2776 should address this issue and the corresponding test case can be enabled again.

@knutwannheden knutwannheden added the bug Something isn't working label Feb 6, 2023
@koppor
Copy link
Contributor

koppor commented Apr 30, 2023

Same with

if ((f1 instanceof Integer) && (f2 instanceof Integer)) {

rewritten to

if ((f1 instanceof Integer integer) && (f2 instanceof Integer integer)) {

@timtebeek timtebeek transferred this issue from openrewrite/rewrite Sep 5, 2024
@timtebeek
Copy link
Contributor

@koppor The second case should see some improvement already after

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

3 participants