openrewrite / rewrite-static-analysis

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

MinimumSwitchCases breaks build - does not add parentheses around boolean expression #284

Open timo-a opened 3 months ago

timo-a commented 3 months ago

What version of OpenRewrite are you using?

I am using the latest version as of 2024-04-02

How are you running OpenRewrite?

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

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-static-analysis:RELEASE -Drewrite.activeRecipes=org.openrewrite.staticanalysis.MinimumSwitchCases

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

class A {
    void foo(String bar) {
        StringBuilder sb = new StringBuilder(4);
        int i = 5;
        switch (i & 7) {
        case 1:
            sb.append("~0x");
            break;
        case 4:
            sb.append("~1y");
            break; 
        }
    }
}

What did you expect to see?

class A {
    void foo(String bar) {
        StringBuilder sb = new StringBuilder(4);
        int i = 5;
        if ((i & 7) == 1) {
            sb.append("~0x");
        } else if ((i & 7) == 4) {
            sb.append("~1y");
        }
    }
}

What did you see instead?

class A {
    void foo(String bar) {
        StringBuilder sb = new StringBuilder(4);
        int i = 5;
        if (i & 7 == 1) {
            sb.append("~0x");
        } else if (i & 7 == 4) {
            sb.append("~1y");
        }
    }
}

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

[ERROR] /[...]/jackson-core/src/test/java/tools/jackson/core/jsonptr/Fuzz51806JsonPointerParse818Test.java:[60,15] bad operand types for binary operator '&'
  first type:  int
  second type: boolean

Are you interested in contributing a fix to OpenRewrite?

no

timtebeek commented 1 month ago

Guess that's a case not handled before indeed; We can either update the produced if statements to use parentheses when any operations are involved, or choose to simply for now skip such cases to not produce any breaking code changes. Either way thanks for reporting this to us!