rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

`UnnecessaryThrows` does not detect exceptions thrown by try-with-resources

Open kthoms opened this issue 4 months ago • 3 comments

What version of OpenRewrite are you using?

I am using

  • Maven plugin v6.12.0

org.openrewrite.recipe:rewrite-static-analysis 2.16.0

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a multi module project.

<plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>6.12.0</version>
        <configuration>
          <activeRecipes>
            <recipe>org.operaton.recipe.CodeCleanup</recipe>
          </activeRecipes>
 ...
        </configuration>
</plugin>

With rewrite.yml

type: specs.openrewrite.org/v1beta/recipe
name: org.operaton.recipe.CodeCleanup
displayName: Code cleanup
description: Automatically cleanup code and common SAST issues, e.g. remove unnecessary parentheses, simplify expressions.
recipeList:
  - org.openrewrite.staticanalysis.UnnecessaryThrows

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

Image

What did you expect to see?

void customStripSpaceXSL() throws Exception {

What did you see instead?

void customStripSpaceXSL() {

Are you interested in contributing a fix to OpenRewrite?

not on this one

kthoms avatar Aug 30 '25 03:08 kthoms

hi! Thanks for the report; that's odd, since we had a similar report and related fix earlier:

  • https://github.com/openrewrite/rewrite/issues/631 https://github.com/openrewrite/rewrite-static-analysis/blob/1cdcfbd0ce088ec54dbfe0a72a796b2a767aa2df/src/test/java/org/openrewrite/staticanalysis/UnnecessaryThrowsTest.java#L88-L110

timtebeek avatar Sep 01 '25 08:09 timtebeek

Also can't seem to replicate this with a minimal unit test:

    @Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/728")
    @Test
    void necessaryThrowsFromCloseable2() {
        rewriteRun(
          //language=java
          java(
            """
              import java.io.IOException;
              import java.io.InputStream;

              class Test {
                   void readResource() throws IOException {
                      // URLClassLoader implements Closeable and throws IOException from its close() method
                      try (InputStream is = Test.class.getClassLoader().getResourceAsStream("foo.txt")) {
                      }
                  }
              }
              """
          )
        );
    }

Anything else that I've missed that would make this test fail?

Or might this be a case of missing types leading to a failure to find that thrown exception?

  • https://docs.openrewrite.org/recipes/java/search/findmissingtypes

If that latter recipe produces a hit we could add a precondition not to make changes when there are missing types.

timtebeek avatar Sep 01 '25 08:09 timtebeek

The examples look rather close to the use case. I don't know why it is changed.

To reproduce you might check:

  1. Check out https://github.com/operaton/operaton.git
  2. Open /pom.xml, search for UnnecessaryThrows and remove exclusions
  3. Perform ./mvnw -U org.openrewrite.maven:rewrite-maven-plugin:run

kthoms avatar Sep 01 '25 08:09 kthoms