Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimplifyBooleanExpression causes NullPointerException because condition is negated #276

Open
timo-abele opened this issue Mar 8, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@timo-abele
Copy link
Contributor

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.23.1</version>
    <configuration>
        <activeRecipes>
          <recipe>org.openrewrite.staticanalysis.SimplifyBooleanExpression</recipe>
        </activeRecipes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-static-analysis</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>
</plugin>

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

class A {
    boolean foo(String name) {
        return !(name != null ? !name.equals(System.out.toString()) : false);
    }
}

This is transformed to return name == null ? !name.equals(authority.name) : authority.name != null; if name is null.

What did you expect to see?

no change, or negation of the cases instead of the condition:

class A {
    boolean foo(String name) {
        return name != null ? name.equals(System.out.toString()) : !false;
    }
}

What did you see instead?

class A {
    boolean foo(String name) {
        return name == null ? !name.equals(System.out.toString()) : false;
    }
}

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

Standard NPE message when name is null

@timo-abele timo-abele added the bug Something isn't working label Mar 8, 2024
@timtebeek
Copy link
Contributor

Thanks for the report! That's indeed a confusing amount of negation best left alone perhaps. 🤔

@knutwannheden
Copy link
Contributor

Also note that a ? b : false can be simplified to a && b. So we might want to add that instead.

@knutwannheden
Copy link
Contributor

I also just notice that the current recipe is incorrect, as the method invocation isn't null safe and would end up throwing an NPE. That definitely needs fixing.

@timo-abele
Copy link
Contributor Author

Also note that a ? b : false can be simplified to a && b. So we might want to add that instead.

The false is a simplification on my part, in the original it was an expression. I should have probably used a variable instead of the literal false in the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

3 participants