Issue #15484: New check UnusedTryResourceShouldBeUnnamed#17075
Issue #15484: New check UnusedTryResourceShouldBeUnnamed#17075kkoutsilis wants to merge 1 commit into
Conversation
8b88111 to
cdd82d4
Compare
da88cb2 to
d0ba295
Compare
58c1ec8 to
bd46ce7
Compare
|
@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. |
dfeb9ff to
a09c788
Compare
|
Semaphore failure is expected due to new check. |
|
@kkoutsilis , please run diff testing. |
|
GitHub, generate report |
2a3b3e5 to
43f253c
Compare
|
Attention to |
|
GitHub, generate report |
|
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. |
Hey @romani, why is this a false positive? As far as I can tell Should we not account for try-resources that are named |
|
Found a small issue, if there are multiple unused try-resources, only one is logged (ref). Looking into fixing that |
|
It might be complicated to catch all violations at ones, we usually ok to report closest place only, do not go crazy. |
|
GitHub, generate report |
|
Best form of acceptance by users of new Check is merged PR. You can send PR to them and see how users will react on such updates. |
|
|
||
| try (AutoCloseable b = lock()) { // warn | ||
|
|
||
| } catch (Exception e) { |
There was a problem hiding this comment.
It will be better to remove extra empty lines.
Compact code input is good
| * | ||
| * <p> | ||
| * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} | ||
| * </p> |
There was a problem hiding this comment.
This paragraph and all below should be removed.
There are automatically generated, just put macros in template
| * | ||
| * @since 12.2.0 | ||
| */ | ||
| @FileStatefulCheck |
There was a problem hiding this comment.
If possible, it will be good to make it state less.
It will help a lot during migration to multi threading or checkstyle
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Please avoid unrelated changes
Move them to separate PR, if this is a typo.
|
GitHub, generate report |
|
GitHub, generate report |
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? |
|
Yes, if code is compiled, we have to work on it without exceptions. |
| 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"); | ||
| } |
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
it is compiled at https://www.jdoodle.com/online-java-compiler, so lets double focus on why local javac is not happy.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
* @compile/fail/ref=TwrForVariable3.out
This line looks like a hint that this file is not compiled
|
Github, generate report for configs in PR description |
|
Github, generate report for configs in PR description |
|
If you have time please send update To exclude such non compiled files, so it will be bother us in diff , and we will see more clean validation. |
|
https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/21f8aee60b4b792f41972b6a178c8314c85cee83_2025015606/reports/diff/openjdk21/index.html#A2 this is interesting, can you explain? This means that |
This is identified as unused as |
|
I think this is very special way to reference in try, we should ignore such code - when it is not declared in "try". |
|
@kkoutsilis , please resolve conflict |
|
Can potentially be on it soon |
|
@romani @SteLeo1602 This check has some complications similar to JavadocUtilizingTrailingSpace that I've finished. |
Can you confirm you are working on it right now? |
#19359 |

Aims to close #15484
Implemented new UnusedTryResourceShouldBeUnnamedCheck, similarly to UnusedCatchParameterShouldBeUnnamedCheck and UnusedLambdaParameterShouldBeUnnamedCheck.
New module config: https://gist.githubusercontent.com/kkoutsilis/899b361c1d74b0088fa76eeb96c34396/raw/7942207355ed3afa9ac23dd382acc0305472f2a4/config.xml