You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] CloseResource: False positive when a correctly-closed resource is declared without initializer #6742
PMD's CloseResource rule reports local AutoCloseable variables that may leave scope without being closed. The rule's verdict depends on the syntactic shape of the variable's first write rather than on its data flow:
T x = new R(); ... x.close(); is correctly recognized as closed → no finding.
The runtime-equivalent split form T x; x = new R(); ... x.close(); is falsely flagged, even though close() is still called on every path.
Per JLS §14.4.2 a local declaration with an initializer is exactly equivalent to a declaration followed by a separate assignment, so the two forms are semantically identical and should receive the same verdict.
Reproducer
before — declaration-with-initializer (no finding)
CloseResource should produce the same finding set on BEFORE and AFTER. In both, every closeable local is closed via close() before the method exits, so neither should be reported.
Actual behavior
T x = new R(); ... x.close(); → 0 findings (correct).
T x; x = new R(); ... x.close(); → 1 finding: Ensure that resources like this ObjectOutputStream object are closed after use, despite the oout.close() call.
The rule appears to register a closeable local only when it sees a same-line declarator-with-initializer; a local initialized via a separate assignment is not associated with its later close(), producing a false positive.
changed the title [-][java] FP: `CloseResource` flags a correctly-closed resource when its declaration is split from its initializer[/-][+][java] CloseResource: False positive when a correctly-closed resource is declared without initializer[/+]on Jun 4, 2026
Activity
[-][java] FP: `CloseResource` flags a correctly-closed resource when its declaration is split from its initializer[/-][+][java] CloseResource: False positive when a correctly-closed resource is declared without initializer[/+][java] CloseResource: drop the two pmd#2840 test cases from this PR
[java] Fix pmd#6742: CloseResource false positive for a wrapped resou…