-
Notifications
You must be signed in to change notification settings - Fork 92
Simplify ifs with literal conditions even if they jump
#193
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
Conversation
This isn't necessary; the visitor already look for jumps. I thought about keeping it as an optimization, but I don't think it actually optimizes anything since the visitor has to rediscover the jumps anyways.
src/main/java/org/openrewrite/staticanalysis/RemoveUnreachableCodeVisitor.java
Outdated
Show resolved
Hide resolved
|
I really like this change. It's concise and very readable. I've added another unit test for the |
|
Thank you for the contribution! 🎉 |
src/main/java/org/openrewrite/staticanalysis/RemoveUnreachableCodeVisitor.java
Show resolved
Hide resolved
|
Great work all around! Probably best to run this against the default set through Moderne, just to spot check that it works well there. Well already have an impressive collection of tests here, just looking to make sure we haven't missed anything. |
|
Thanks for the feedback and merge, all! 😄 |
I ran it, all results look good! |
What's changed?
This PR improves the
SimplifyConstantIfBranchExecutionrecipe to simplifyifs even if they have jumps (return,throw,break, orcontinue) in the branch we keep. The recipe will now also remove any unreachable code introduced by simplifying theseifs.I added numerous unit tests to demonstrate the new behavior.
What's your motivation?
Fixes #192
Have you considered any alternatives or workarounds?
I considered implementing the unreachable code elimination as another recipe. However, it feels like it's tightly coupled to the
ifsimplification itself; unreachable code is a compile-time error, so if this recipe is going to simplify theseifs, I think it should do so cleanly.Checklist
./gradlew licenseFormat