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

SimplifyConstantIfBranchExecution leaves unreachable code #399

Open
lambm opened this issue Nov 19, 2024 · 4 comments
Open

SimplifyConstantIfBranchExecution leaves unreachable code #399

lambm opened this issue Nov 19, 2024 · 4 comments
Labels
bug Something isn't working good first issue Good for newcomers test provided

Comments

@lambm
Copy link

lambm commented Nov 19, 2024

What version of OpenRewrite are you using?

I am using

  • org.openrewrite.recipe:rewrite-static-analysis:1.20.0
  • org.openrewrite.recipe:rewrite-recipe-bom:2.22.0

How are you running OpenRewrite?

Java test case

import org.junit.jupiter.api.Test;
import org.openrewrite.staticanalysis.SimplifyConstantIfBranchExecution;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class SimplifyTest implements RewriteTest {

    @Test
    void test() {
        rewriteRun(
                spec -> spec.recipe(new SimplifyConstantIfBranchExecution()),
                //language=java
                java(
                        """
                            public class A {
                                public boolean test() {
                                    if (true) {
                                        Object x;
                                        return true;
                                    }
                                    return false;
                                }
                            }
                        """,
                        """
                             public class A {
                                 public boolean test() {
                                     Object x;
                                     return true;
                                 }
                             }
                         """
                )
        );
    }
}

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

public class A {
    public boolean test() {
        if (true) {
            Object x;
            return true;
        }
        return false;
    }
  }

What did you expect to see?

public class A {
     public boolean test() {
         Object x;
         return true;
     }
 }

What did you see instead?

public class A {
     public boolean test() {
         {
            Object x;
            return true;
         }
         return false;
     }
 }

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

No error, just invalid output.

Are you interested in contributing a fix to OpenRewrite?

Unable to contribute.

@lambm lambm added the bug Something isn't working label Nov 19, 2024
@timtebeek
Copy link
Contributor

Thanks for the feedback @lambm ! Great to see that as a runnable example. Indeed a missed case, and would then negatively affect recipes that rely on this too, like rewrite-feature-flags. Appreciate that you're unable to contribute but still wanted to report the issue; I hope in practice at least it was rare? We'll add it to the backlog and hope to get to it soon-ish.

@AHB102
Copy link

AHB102 commented Dec 1, 2024

@timtebeek @lambm I'd like to contribute, but I'm a bit unclear on the direction. Can you provide more details?😅

@timtebeek timtebeek transferred this issue from openrewrite/rewrite Dec 1, 2024
@timtebeek
Copy link
Contributor

Appreciate the offer to help here @AHB102 ; be sure to read our contributing guidelines. From there fork and check out this project, add the above unit test to SimplifyConstantIfBranchExecutionTest.java and try to make it pass. In this case: if the if block directly contains a return statement, then we can remove any else or following statements.

@AHB102
Copy link

AHB102 commented Dec 1, 2024

@timtebeek Great! I'll start integrating this and reach out if I have any questions. 😁

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

No branches or pull requests

3 participants