Skip to content

Conversation

@tkindy
Copy link
Contributor

@tkindy tkindy commented Oct 14, 2023

What's changed?

This PR improves the SimplifyConstantIfBranchExecution recipe to simplify ifs even if they have jumps (return, throw, break, or continue) in the branch we keep. The recipe will now also remove any unreachable code introduced by simplifying these ifs.

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 if simplification itself; unreachable code is a compile-time error, so if this recipe is going to simplify these ifs, I think it should do so cleanly.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've added the license header to any new files through ./gradlew licenseFormat
  • I've used the IntelliJ IDEA auto-formatter on affected files

@pstreef
Copy link
Contributor

pstreef commented Oct 23, 2023

I really like this change. It's concise and very readable. I've added another unit test for the throws keyword with try catch and it behaves as expected.

@pstreef pstreef merged commit e048128 into openrewrite:main Oct 23, 2023
@pstreef
Copy link
Contributor

pstreef commented Oct 23, 2023

Thank you for the contribution! 🎉

@timtebeek
Copy link
Member

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.

@tkindy
Copy link
Contributor Author

tkindy commented Oct 23, 2023

Thanks for the feedback and merge, all! 😄

@pstreef
Copy link
Contributor

pstreef commented Oct 23, 2023

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.

I ran it, all results look good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

if statements with literal conditions and jumps in their branches aren't simplified

4 participants