Repository navigation
[java] Fix #6742: CloseResource false positive for a wrapped resource assigned without an initializer - #6918
Conversation
…rce assigned without an initializer getWrappedResourceType only looked at the variable initializer, so the split declaration/assignment form did not recognize the wrapping resource and was reported. Per JLS 14.4.2 both shapes are equivalent. Signed-off-by: Eljees <[email protected]>
|
Compared to main: (comment created at 2026-08-07 08:36:47+00:00 for 21203b7) |
UncleOwen
left a comment
There was a problem hiding this comment.
Please see the notes I left on your tests.
The change itself looks good. Have you thought about the other call to initializerExpressionOf (in getFirstReassigningStatementBeforeBeingClosed)? Does that need to be changed as well? I don't think it does, but would appreciate if you could confirm this.
Both were raised in review. The direct-return case duplicates the existing 'pmd#1011 CloseResource Rule ignores Constructors' test a few hundred lines up, and the nested-block one is not about pmd#2840 at all - neither belongs in a change scoped to pmd#6742. CloseResourceTest: 101 tests, 0 failures. Signed-off-by: Eljees <[email protected]>
|
Thanks — both notes were right, and your question turned out to have a more interesting answer than "no". Tests. Dropped both #2840 cases in d404adc: the direct-return one does duplicate The other The two call sites want different things: boolean isInitialized = isNotNullInitialized(variable); // true when there is no initializer at all
ASTExpression initializingExpression = initializerExpressionOf(variable); // null in that case
...
if (initializingExpression != null && !inSameIfBlock(...) && ...) return statement;For a variable declared without an initializer, Put differently: Runs for the record — same command each time, only the rule file swapped, rule classes forced to recompile:
AI-assisted (LLM used for drafting and for running the checks above); the analysis and the runs are mine. |
|
Something seems to be wrong with github actions... https://www.githubstatus.com/ |
Fixes #6742
CloseResourcerecognizes that a resource wrapped by another resource does not need to be closed onits own — but only when the wrapper is created in the variable's initializer. The runtime-equivalent
split form is flagged:
The whole wrapping decision hangs on
var.getInitializer():With an initializer,
getWrappedResourceTypefinds thenew ObjectOutputStream(...)call, picks uppoutthroughgetFirstArgumentVariableIfResource, andcheckForResourcesuses that as the effectivetype — so
shouldVarOfTypeBeClosedInMethodreturns false. Without one, the wrapping is simply invisibleand the declared type is used instead.
Per JLS §14.4.2 a local declaration with an initializer is equivalent to a declaration followed by a
separate assignment, so both shapes should reach the same verdict. This adds
initializerOrFirstAssignedValueOf(), which falls back to the value of the variable's first assignment,and uses it in
getWrappedResourceType().I deliberately left
initializerExpressionOf()itself alone: it is also used bygetFirstReassigningStatementBeforeBeingClosed(), which distinguishes initialized from uninitializedvariables on purpose (
isNotNullInitialized), and widening it there would change the"reassigned before being closed" behaviour too. If you would rather have the fallback applied
consistently in both places, say so and I will do that instead.
Before / after
Two cases added to
CloseResource.xml— the reporter's snippet in both shapes, with rule defaults:Run with
mvn -pl pmd-java test -Dtest=CloseResourceTeston temurin-25. The control case(declaration with initializer) passes before and after, so the change only affects the split form.
AI-assisted (LLM used for drafting); the change and both runs above are mine.