Skip to content

Issue #15484: New check UnusedTryResourceShouldBeUnnamed#17075

Open
kkoutsilis wants to merge 1 commit into
checkstyle:masterfrom
kkoutsilis:15484
Open

Issue #15484: New check UnusedTryResourceShouldBeUnnamed#17075
kkoutsilis wants to merge 1 commit into
checkstyle:masterfrom
kkoutsilis:15484

Conversation

@kkoutsilis

@kkoutsilis kkoutsilis commented May 14, 2025

Copy link
Copy Markdown
Member

Comment thread config/pitest-suppressions/pitest-coding-1-suppressions.xml Outdated
@kkoutsilis

Copy link
Copy Markdown
Member Author

@romani Need to fix the CI, probably add more tests as there are a few mutations, but feel free to have a first look if you have some time.

@kkoutsilis kkoutsilis force-pushed the 15484 branch 2 times, most recently from dfeb9ff to a09c788 Compare May 31, 2025 10:16
@romani

romani commented May 31, 2025

Copy link
Copy Markdown
Member

Semaphore failure is expected due to new check.
javac21 must be fixed.

@romani

romani commented May 31, 2025

Copy link
Copy Markdown
Member

@kkoutsilis , please run diff testing.

@kkoutsilis

Copy link
Copy Markdown
Member Author

GitHub, generate report

@kkoutsilis kkoutsilis force-pushed the 15484 branch 2 times, most recently from 2a3b3e5 to 43f253c Compare June 1, 2025 14:02
@romani

romani commented Jun 1, 2025

Copy link
Copy Markdown
Member

https://github.com/checkstyle/contribution/tree/master/checkstyle-tester#executing-generation-using-github-action

Attention to New module config: {{URI to new_module_config.xml}}
And attention to other merged PRs description, there are bunch of examples

@kkoutsilis

Copy link
Copy Markdown
Member Author

GitHub, generate report

@github-actions

github-actions Bot commented Jun 2, 2025

Copy link
Copy Markdown
Contributor

@romani

romani commented Jun 2, 2025

Copy link
Copy Markdown
Member

Looks like false positive https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/43f253c_2025211859/reports/diff/camunda/index.html#A1

Please review your report your self first, to shorten feedback loop.
Looks like I was to quick to respond to this PR :)

@kkoutsilis

kkoutsilis commented Jun 3, 2025

Copy link
Copy Markdown
Member Author

Looks like false positive https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/43f253c_2025211859/reports/diff/camunda/index.html#A1

Please review your report your self first, to shorten feedback loop. Looks like I was to quick to respond to this PR :)

Hey @romani, why is this a false positive? As far as I can tell ignored is not used in the try body. Am I missing something?

Should we not account for try-resources that are named ignored?

@kkoutsilis

kkoutsilis commented Jun 3, 2025

Copy link
Copy Markdown
Member Author

Found a small issue, if there are multiple unused try-resources, only one is logged (ref). Looking into fixing that

@romani

romani commented Jun 3, 2025

Copy link
Copy Markdown
Member

It might be complicated to catch all violations at ones, we usually ok to report closest place only, do not go crazy.

@kkoutsilis

Copy link
Copy Markdown
Member Author

GitHub, generate report

@romani

romani commented Oct 21, 2025

Copy link
Copy Markdown
Member

Best form of acceptance by users of new Check is merged PR.
https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/f71136d_2025021825/reports/diff/camunda/index.html

You can send PR to them and see how users will react on such updates.

@romani romani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items


try (AutoCloseable b = lock()) { // warn

} catch (Exception e) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be better to remove extra empty lines.
Compact code input is good

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

*
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph and all below should be removed.
There are automatically generated, just put macros in template

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

*
* @since 12.2.0
*/
@FileStatefulCheck

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, it will be good to make it state less.
It will help a lot during migration to multi threading or checkstyle

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I might need some help with that, let me see

unused.catch.parameter=Käyttämättömän saalisparametrin "{0}" tulee olla nimeämätön.
unused.lambda.parameter=Käyttämättömän lambda-parametrin "{0}" tulee olla nimeämätön.
unused.catch.parameter=Käyttämättömän saalisparametrin ''{0}'' tulee olla nimeämätön.
unused.lambda.parameter=Käyttämättömän lambda-parametrin ''{0}'' tulee olla nimeämätön.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid unrelated changes
Move them to separate PR, if this is a typo.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@romani

romani commented Oct 21, 2025

Copy link
Copy Markdown
Member

GitHub, generate report

@github-actions

Copy link
Copy Markdown
Contributor

@romani

romani commented Oct 21, 2025

Copy link
Copy Markdown
Member

GitHub, generate report

@github-actions

Copy link
Copy Markdown
Contributor

@kkoutsilis

Copy link
Copy Markdown
Member Author

@kkoutsilis , please keep copy of all codes that caused exception to your Inputs to not rely much on diff testing. Vast majority should be in our tests.

Ones all exception eliminated, we can start review of violations for validity

Hey @romani, the code was failing when a static var was used in the try resource, and also for some test files like this one. Added this test for it. Do you think more input files are required?

@romani

romani commented Oct 22, 2025

Copy link
Copy Markdown
Member

Yes, if code is compiled, we have to work on it without exceptions.

Comment on lines +70 to +90
void test3() {
Object v2 = new Object();
Object v3 = new Object() {
public void close() {
}
};

// violation below, 'Unused try resource 'v2' should be unnamed'
try (v2) {
fail("not an AutoCloseable");
}
// violation below, 'Unused try resource 'v3' should be unnamed'
try (v3) {
fail("not an AutoCloseable although has close() method");
}
try (java.lang.Object) {
fail("not a variable access");
}
try (java.lang) {
fail("not a variable access");
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@romani Added this from https://github.com/openjdk/jdk21/blob/890adb6410dab4606a4f26a942aed02fb2f55387/test/langtools/tools/javac/TryWithResources/TwrForVariable3.java#L6, which was causing an exception before, but now CI fails. Since this code is not compilable, are you fine to remove?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From where you got this code?
We need to make sure it is compilation good at that project.
For us it is hard limitation - execution only on fully compile-able sources.

If this is not compiled sources, please remove it ,and it ok to have exception on such code in diff report.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is from jdk21 tests, link above, got it because it was causing an exception in a previous report here https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/571223a_2025223753/reports/diff/openjdk21/index.html#A229. Will remove from tests then, thanks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets pay more attnetion - https://github.com/openjdk/jdk21/blob/master/test/langtools/tools/javac/TryWithResources/TwrForVariable1.java

it looks like compiled, it isnot random crap, and even issue number is referenced.
lets look at it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is compiled at https://www.jdoodle.com/online-java-compiler, so lets double focus on why local javac is not happy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TwrForVariable1 is compiled, but I am pointing to TwrForVariable3, this does not compile for me. My understanding is that this test is expected not to compile looking at TwrForVariable3.out file

@romani romani Oct 27, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/openjdk/jdk21/blob/890adb6410dab4606a4f26a942aed02fb2f55387/test/langtools/tools/javac/TryWithResources/TwrForVariable3.java#L4

* @compile/fail/ref=TwrForVariable3.out

This line looks like a hint that this file is not compiled

@romani romani Oct 27, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compilation error is confirmed
Screenshot_20251027-090924

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove it

@kkoutsilis

Copy link
Copy Markdown
Member Author

Github, generate report for configs in PR description

@github-actions

Copy link
Copy Markdown
Contributor

@romani

romani commented Oct 30, 2025

Copy link
Copy Markdown
Member

Github, generate report for configs in PR description

@romani

romani commented Oct 30, 2025

Copy link
Copy Markdown
Member

If you have time please send update
To
https://github.com/checkstyle/test-configs/blob/9ff74105f0f9bbceeca9f3ccb8f6d28a2e548682/extractor/src/main/resources/all-projects.yml#L38
https://github.com/checkstyle/test-configs/blob/9ff74105f0f9bbceeca9f3ccb8f6d28a2e548682/extractor/src/main/resources/list-of-projects.yml#L38

To exclude such non compiled files, so it will be bother us in diff , and we will see more clean validation.
If you can grep whole jdk21 by compile/fail to give more comprehensive update of file we should not deal at all, it will awesome.

And at https://github.com/checkstyle/test-configs/blob/9ff74105f0f9bbceeca9f3ccb8f6d28a2e548682/extractor/src/main/resources/all-projects.properties#L14

@github-actions

Copy link
Copy Markdown
Contributor

@romani

romani commented Oct 30, 2025

Copy link
Copy Markdown
Member

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/21f8aee60b4b792f41972b6a178c8314c85cee83_2025015606/reports/diff/openjdk21/index.html#A2 this is interesting, can you explain?

https://github.com/openjdk/jdk21/blob/890adb6410dab4606a4f26a942aed02fb2f55387/src/java.base/share/classes/java/io/FileDescriptor.java#L333

    synchronized void closeAll(Closeable releaser) throws IOException {
        if (!closed) {
            closed = true;
            IOException ioe = null;
            try (releaser) {

This means that releaser will be closed after try. We can not put _ here.
Please add this to inputs, there should be no violation on resources that just referenced, please make input with multiple references in single try.

@kkoutsilis

Copy link
Copy Markdown
Member Author

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/21f8aee60b4b792f41972b6a178c8314c85cee83_2025015606/reports/diff/openjdk21/index.html#A2 this is interesting, can you explain?

https://github.com/openjdk/jdk21/blob/890adb6410dab4606a4f26a942aed02fb2f55387/src/java.base/share/classes/java/io/FileDescriptor.java#L333

    synchronized void closeAll(Closeable releaser) throws IOException {
        if (!closed) {
            closed = true;
            IOException ioe = null;
            try (releaser) {

This means that releaser will be closed after try. We can not put _ here. Please add this to inputs, there should be no violation on resources that just referenced, please make input with multiple references in single try.

This is identified as unused as releaser is not referenced anywhere inside the try block. Probably need to keep track of parameter definitions and exclude those from the check. Need to think about it, it's getting more complicated than I initially expected 😅

@romani

romani commented Nov 6, 2025

Copy link
Copy Markdown
Member

I think this is very special way to reference in try, we should ignore such code - when it is not declared in "try".

@romani

romani commented Nov 9, 2025

Copy link
Copy Markdown
Member

@kkoutsilis , please resolve conflict

@SteLeo1602

Copy link
Copy Markdown
Contributor

Can potentially be on it soon

@aclfe

aclfe commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

@romani @SteLeo1602 This check has some complications similar to JavadocUtilizingTrailingSpace that I've finished.
I am on it

@SteLeo1602

Copy link
Copy Markdown
Contributor

@romani @SteLeo1602 This check has some complications similar to JavadocUtilizingTrailingSpace that I've finished. I am on it

Can you confirm you are working on it right now?

@aclfe

aclfe commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Can you confirm you are working on it right now?

#19359
The check is finished, it needs reviewing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New Check UnusedTryResourceShouldBeUnnamed

5 participants