RemoveTryCatchFailBlocks should not rewrite try blocks that return
What version of OpenRewrite are you using?
- org.openrewrite:rewrite-core:jar:8.28.1
- org.openrewrite.recipe:rewrite-testing-frameworks:jar:2.12.2
What is the smallest, simplest way to reproduce the problem?
org.openrewrite.java.testing.junit5.RemoveTryCatchFailBlocks should not try to rewrite in the case where the try {} contains a return, see the test case for an example. This will create non-compiling code since the return is now in the assertDoesNotThrow lambda. We ran into this in our code base.
@Test
void testRemoveTryCatchFailBlocksWithReturningTry() {
rewriteRun(
spec -> spec.recipe(new RemoveTryCatchFailBlocks()),
java(
"""
package com.helloworld;
import static org.junit.jupiter.api.Assertions.fail;
public class Foo {
String getFoo() {
try {
return "foo";
} catch (RuntimeException e) {
fail();
}
}
}"""));
}
Thanks yet again for the very clear runnable example; sounds like you're having fun over there cleaning up your tests. Much appreciated that you're reporting any issues that you come across, as that really helps us to improve.
In this case I think the fix is going to be very similar to what we added yesterday: a local visitor that in this case detects any J.Return, and if found we just return the unmodified input tree element.
Thanks for the pointer, I will see if I can come up with a fix for this.