Skip to content

InstanceOfPatternMatch does not shadow/rename properly #483

Description

@cortlepp

What version of OpenRewrite are you using?

I am using

  • Maven plugin v6.3.2
  • rewrite-migrate-java v3.4.0

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>6.3.2</version>
    <configuration>
        <exportDatatables>true</exportDatatables>
        <activeRecipes>
            <recipe>org.openrewrite.staticanalysis.InstanceOfPatternMatch</recipe>
        </activeRecipes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-migrate-java</artifactId>
            <version>3.4.0</version>
        </dependency>
    </dependencies>
</plugin>

What is the smallest, simplest way to reproduce the problem?

package org.example;

public class Main {
    public class Bar {}
    Object bar;
    public static void main(String[] args) {
        Object o = new Object();
        if (o instanceof Bar) {
            Bar bar = (Bar)o; 
            System.out.println(bar);
        }
    }
}

What did you expect to see?

package org.example;

public class Main {
    public class Bar {}
    Object bar;
    public static void main(String[] args) {
        Object o = new Object();
        if (o instanceof Bar bar) { 
            System.out.println(bar);
        }
    }
}

What did you see instead?

package org.example;

public class Main {
    public class Bar {}
    Object bar;
    public static void main(String[] args) {
        Object o = new Object();
        if (o instanceof Bar bar1) { 
            System.out.println(bar);
        }
    }
}

Note: this also happens if the class variable is from an outer class, and even if the inner class is static and the outer variable should therefore not be visible.

What is the full stack trace of any errors you encountered?

The migration succeeds, but if you try to compile the changes afterwards javac fails with

non-static variable bar cannot be referenced from a static context

Note: depending on the setup this could also very much fail silently and thereby introduce bugs.

Are you interested in contributing a fix to OpenRewrite?

If I manage to do so with reasonable effort, yes.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions