GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] CloseResource: False positive when a correctly-closed resource is declared without initializer · Issue #6742 · pmd/pmd

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

New issue
Copy link
New issue
Copy link
Closed
#6918
Closed
[java] CloseResource: False positive when a correctly-closed resource is declared without initializer#6742
#6918
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.27.0

Description

@Chordrain
Chordrain
opened on Jun 1, 2026
Issue body actions

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. Chordrain
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Jun 1, 2026
  2. adangel
    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. Eljees
    added a commit that references this issue on Aug 6, 2026

    [java] CloseResource: drop the two pmd#2840 test cases from this PR

    d404adc
  4. UncleOwen
    added this to the 7.27.0 milestone on Aug 6, 2026
  5. UncleOwen
    closed this as completedin #6918on Aug 7, 2026
  6. Eljees
    added a commit that references this issue on Aug 7, 2026

    [java] Fix pmd#6742: CloseResource false positive for a wrapped resou…

    This issue will close once commit ffc2425 is merged into the 'main' branch.
    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-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.27.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.