Skip to content

InstanceOfPatternMatch adding invalid extra initalization #305

@mathewm3

Description

@mathewm3

What version of OpenRewrite are you using?

I am using

  • rewrite-maven-plugin 5.32.1
  • rewrite-migrate-java 2.16.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>5.32.1</version>
                <configuration>
                    <activeRecipes>
                       <recipe>org.openrewrite.java.migrate.UpgradeToJava17</recipe>
                    </activeRecipes>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.openrewrite.recipe</groupId>
                        <artifactId>rewrite-migrate-java</artifactId>
                        <version>2.16.0</version>
                    </dependency>
                </dependencies>
            </plugin>

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

        if (obj instanceof Contact) {
            if (fieldName.equals("FIRST_NAME")) {
                Contact cont = (Contact) obj;
                return getContactLink(cont, null, cont.getFirstName());
            } else if (fieldName.equals("LAST_NAME")) {
                Contact cont = (Contact) obj;
                return getContactLink(cont, null, cont.getLastName());
            } else if (fieldName.equals("FULL_NAME")) {
                Contact cont = (Contact) obj;
                return getContactLink(cont, null, cont.getDisplayName());
            }
        }

What did you expect to see?

        if (obj instanceof Contact cont) {
            if (fieldName.equals("FIRST_NAME")) {
                return getContactLink(cont, null, cont.getFirstName());
            } else if (fieldName.equals("LAST_NAME")) {
                return getContactLink(cont, null, cont.getLastName());
            } else if (fieldName.equals("FULL_NAME")) {
                return getContactLink(cont, null, cont.getDisplayName());
            }
        }

What did you see instead?

        if (obj instanceof Contact cont) {
            if (fieldName.equals("FIRST_NAME")) {
                return getContactLink(cont, null, cont.getFirstName());
            } else if (fieldName.equals("LAST_NAME")) {
                Contact cont = cont;
                return getContactLink(cont, null, cont.getLastName());
            } else if (fieldName.equals("FULL_NAME")) {
                Contact cont = cont;
                return getContactLink(cont, null, cont.getDisplayName());
            }
        }

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

stacktrace output here

Are you interested in contributing a fix to OpenRewrite?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    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