rewrite-testing-frameworks icon indicating copy to clipboard operation
rewrite-testing-frameworks copied to clipboard

RemoveTryCatchFailBlocks should not rewrite try blocks that return

Open protocol7 opened this issue 1 year ago • 2 comments

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();
                          }
                        }
                    }"""));
  }

protocol7 avatar Jul 04 '24 07:07 protocol7

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.

timtebeek avatar Jul 04 '24 08:07 timtebeek

Thanks for the pointer, I will see if I can come up with a fix for this.

protocol7 avatar Jul 05 '24 09:07 protocol7