Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.openrewrite.staticanalysis.FinalizeLocalVariables should not finalized try/catch #359

Open
blipper opened this issue Oct 10, 2024 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@blipper
Copy link
Contributor

blipper commented Oct 10, 2024

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

From checkstyle/checkstyle#3323

public class TestClass {
    public void somePublicMethod() {
        try (FileOutputStream out = new FileOutputStream("......")) {
            //...
        }
    }
}

What did you expect to see?

public class TestClass {
    public void somePublicMethod() {
        try (FileOutputStream out = new FileOutputStream("......")) {
            //...
        }
    }
}

What did you see instead?

public class TestClass {
    public void somePublicMethod() {
        try (final FileOutputStream out = new FileOutputStream("......")) {
            //...
        }
    }
}

This is an interesting one as Checkstyle will flag this as a redundant modifier via checkstyle/checkstyle#3323 RedundantModifier. The JLS spec has try/with resources variables implicitly final as per https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.3

You can see the implementation here https://github.com/checkstyle/checkstyle/blob/888a78f904ea173a4d5e71825aa63c3f35130376/src/main/java/com/puppycrawl/tools/checkstyle/checks/modifier/RedundantModifierCheck.java#L211

Are you interested in contributing a fix to OpenRewrite?

@blipper blipper added the bug Something isn't working label Oct 10, 2024
@timtebeek timtebeek added the good first issue Good for newcomers label Oct 11, 2024
@timtebeek timtebeek transferred this issue from openrewrite/rewrite-migrate-java Oct 11, 2024
@timtebeek
Copy link
Contributor

Thanks for the report! Looks like we could do something very similar to what we already do for for-loops:

if (isDeclaredInForLoopControl(getCursor())) {
return mv;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
Status: Backlog
Development

No branches or pull requests

2 participants