Skip to content

RemoveTryCatchFailBlocks should not rewrite try blocks that return #547

@protocol7

Description

@protocol7

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggood first issueGood for newcomers

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions