openrewrite / rewrite-static-analysis

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

InstanceOfPatternMatch breaks code wrapped in unnecessary parentheses #307

Open nazoking opened 4 days ago

nazoking commented 4 days ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

I am using the Gradle plugin, and my project is a multi module project.

initscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2" }
    }
    dependencies {
        classpath("org.openrewrite:plugin:6.16.2")
    }
}

rootProject {
    plugins.apply(org.openrewrite.gradle.RewritePlugin)
    dependencies {
        rewrite("org.openrewrite.recipe:rewrite-spring:latest.release")
        rewrite("org.openrewrite.recipe:rewrite-migrate-java:2.18.1")
    }

    afterEvaluate {
        if (repositories.isEmpty()) {
            repositories {
                mavenCentral()
            }
        }
    }
}

and run

./gradlew --init-script rewrite.gradle rewriteRun -Drewrite.activeRecipe=org.openrewrite.staticanalysis.InstanceOfPatternMatch

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

A code snippet can be something simple like this.

            if (t instanceof Exception) {
                throw ((Exception) t);
            } else {

What did you expect to see?

            if (t instanceof Exception exception) {
                throw exception;
            } else {

What did you see instead?

            if (t instanceof Exception exception) {
                throwexception;
            } else {

Are you interested in contributing a fix to OpenRewrite?

Indicate if this is something you would like to work on, and how we can best support you in doing so.

timtebeek commented 4 days ago

Thanks for the clear example! I think this should be a relatively straightforward fix, starting with a unit test based on the above. I'd that something you'd want to contribute?