rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

`InstanceOfPatternMatch` declares variable with conflicting name

Open knutwannheden opened this issue 2 years ago • 2 comments

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.

  • openrewrite/rewrite#2776

knutwannheden avatar Feb 06 '23 08:02 knutwannheden