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 for closeable initialized with (T) null · Issue #6743 · pmd/pmd

[java] CloseResource: False positive for closeable initialized with (T) null #6743

New issue
Copy link
New issue
Copy link
Closed
#6748
Closed
[java] CloseResource: False positive for closeable initialized with (T) null#6743
#6748
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
7.26.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. Its verdict changes depending on whether a closeable local is initialized with the bare null literal null or with a cast null (T) null:

  • Connection c = null; → no finding.
  • Connection c = (Connection) null; → false positive on both the connection and statement declarations, reported as 'connection' is reassigned, but the original instance is not closed, even though the "original instance" is the null reference and the variable is later closed in finally.

A cast on the null literal is a no-op per JLS §5.5: both forms initialize the local with the null reference of static type T. The two are runtime-equivalent and should receive the same verdict.

Reproducer

before — bare null (no finding)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CastNullBefore {
    public static void method() {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = DriverManager.getConnection("blah");
            statement = connection.createStatement();
        } catch (SQLException e) {
            System.err.println("Error: " + e);
        } finally {
            if (statement != null) try { statement.close(); } catch (SQLException e) {}
            if (connection != null) try { connection.close(); } catch (SQLException e) {}
        }
    }
}

after — (T) null (false positive)

public class CastNullAfter {
    public static void method() {
        Connection connection = (Connection) null;   // FALSE POSITIVE
        Statement statement = (Statement) null;
        try {
            connection = DriverManager.getConnection("blah");
            statement = connection.createStatement();
        } catch (SQLException e) {
            System.err.println("Error: " + e);
        } finally {
            if (statement != null) try { statement.close(); } catch (SQLException e) {}
            if (connection != null) try { connection.close(); } catch (SQLException e) {}
        }
    }
}

Expected behavior

CloseResource should produce the same finding set on BEFORE and AFTER, since Connection c = null; and Connection c = (Connection) null; differ only by a no-op cast on a null literal. Both reassign a real resource in try and close it in finally, so neither should be reported.

Actual behavior

  • Connection c = null; → 0 findings (correct).
  • Connection c = (Connection) null; → 2 findings: 'connection' is reassigned, but the original instance is not closed and the same for statement. PMD treats the (T) null initializer as a real resource instance that is then "lost" when the variable is reassigned inside try, even though the original value is the null reference (nothing to close) and the real resource is closed in finally.

The rule's verdict is sensitive to the syntactic shape of the initializer (null vs (T) null) rather than its value.

Activity

  1. Chordrain
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Jun 1, 2026
  2. lukasgraef
    added a commit that references this issue on Jun 3, 2026

    [doc] Update release notes (pmd#6743)

    cb2910e
  3. adangel
    changed the title [-][java] FP: `CloseResource` flags a closeable initialized with `(T) null` but not with bare `null`[/-] [+][java] CloseResource: False positive on closeable initialized with `(T) null` but not with bare `null`[/+] on Jun 4, 2026
  4. adangel
    changed the title [-][java] CloseResource: False positive on closeable initialized with `(T) null` but not with bare `null`[/-] [+][java] CloseResource: False positive for closeable initialized with (T) null[/+] on Jun 4, 2026
  5. UncleOwen
    added a commit that references this issue on Jun 4, 2026

    [java] Fix #6743: CloseResource: False positive for closeable initial…

    This issue will close once commit 4900e7e is merged into the 'main' branch.
    4900e7e
  6. UncleOwen
    closed this as completedin #6748on Jun 4, 2026
  7. adangel
    added this to the 7.26.0 milestone on Jun 29, 2026
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.26.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.