openrewrite / rewrite-static-analysis

OpenRewrite recipes for identifying and fixing static analysis issues.
Apache License 2.0
27 stars 43 forks source link

org.openrewrite.staticanalysis.UnnecessaryThrows failing to detect thrown exception example 2 #271

Open blipper opened 4 months ago

blipper commented 4 months ago

What version of OpenRewrite are you using?

I am using 6.6.3

How are you running OpenRewrite?

What is the smallest, simplest way to reproduce the problem?

public class CloseableHttpEntityTest {

    @Mock
    private HttpEntity httpEntity;

    @Test
    public void testCloseOnNullDoNotThrow() throws IOException {
        final CloseableHttpEntity closeableHttpEntity = new CloseableHttpEntity(null);
        closeableHttpEntity.close();
    }
}

@RequiredArgsConstructor
public class CloseableHttpEntity implements Closeable {
    @Nullable
    private final HttpEntity httpEntity;

    @Override
    public void close() throws IOException {
        EntityUtils.consume(this.httpEntity);
    }
}

What did you expect to see?

    @Test
    public void testCloseOnNullDoNotThrow() throws IOException {
        final CloseableHttpEntity closeableHttpEntity = new CloseableHttpEntity(null);
        closeableHttpEntity.close();
    }

What did you see instead?

   @Test
    public void testCloseOnNullDoNotThrow() {
        final CloseableHttpEntity closeableHttpEntity = new CloseableHttpEntity(null);
        closeableHttpEntity.close();
    }

What is the full stack trace of any errors you encountered?

/workplace/mnr/InsistorRedux/src/JavelinRenderingService/tst/com/amazon/javelinrenderingservice/aax/model/CloseableHttpEntityTest.java:46: error: unreported exception IOException; must be caught or declared to be thrown
        closeableHttpEntity.close()

Are you interested in [contributing a fix to OpenRewrite]

Yes

timtebeek commented 4 months ago

Thanks for the report & offer to help. Let me know if you'd like any help setting up a unit test to allow you to explore a fix.