Skip to content

[java] CloseResource: False positive when a correctly-closed resource is declared without initializer #6742

Closed

Description

@Chordrain

PMD Version: 7.24.0
Category: CloseResource
Severity: Warning (Error Prone)

Summary

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)

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

public class SplitDeclBefore {
    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream pout = new ByteArrayOutputStream();
        ObjectOutputStream oout = new ObjectOutputStream(pout);
        oout.writeObject("x");
        oout.close();
    }
}

after — split declaration / assignment (false positive)

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

public class SplitDeclAfter {
    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream pout; pout = new ByteArrayOutputStream();
        ObjectOutputStream oout; oout = new ObjectOutputStream(pout);  // FALSE POSITIVE
        oout.writeObject("x");
        oout.close();
    }
}

Expected behavior

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.

Activity

  1. added
    a:false-positive
    on Jun 1, 2026
  2. 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
  3. added a commit that references this issue on Aug 6, 2026
  4. added this to the 7.27.0 milestone on Aug 6, 2026
  5. added a commit that references this issue on Aug 7, 2026
    ffc2425
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions