openrewrite / rewrite-static-analysis

OpenRewrite recipes for identifying and fixing static analysis issues.
Apache License 2.0
27 stars 43 forks source link

InstanceOfPatternMatch leads to duplicate variable name in if statement with two different instanceof #174

Open ghost opened 9 months ago

ghost commented 9 months ago

What version of OpenRewrite are you using?

I am using

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>5.5.2</version>
    <configuration>
        <activeRecipes>
            <recipe>org.openrewrite.java.migrate.UpgradeToJava20</recipe>
        </activeRecipes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-migrate-java</artifactId>
            <version>2.1.0</version>
        </dependency>
    </dependencies>
</plugin>

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

if (test instanceof TestClass && test2 instanceof TestClass) {
    ...
}

What did you expect to see?

if (test instanceof TestClass t && test2 instanceof TestClass t2) {
    ...
}

What did you see instead?

if (test instanceof TestClass t && test2 instanceof TestClass t) {
    ...
}

Are you interested in contributing a fix to OpenRewrite?

timtebeek commented 9 months ago

Thanks for reporting this issue! We have a variable name utility class that should be used here to generate a unique name given the context. Should be relatively straightforward if you'd want to explore that yourself.

timtebeek commented 9 months ago

Replicated and picked up in https://github.com/openrewrite/rewrite-static-analysis/pull/175 by @KoTurk