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] ReturnEmptyCollectionRatherThanNull: False negative when null is returned through a local variable · Issue #6695 · pmd/pmd

[java] ReturnEmptyCollectionRatherThanNull: False negative when null is returned through a local variable #6695

New issue
Copy link
New issue
Copy link
Closed
#6941
Closed
[java] ReturnEmptyCollectionRatherThanNull: False negative when null is returned through a local variable#6695
#6941
Copy link
Labels
a:false-negativePMD doesn't flag a problematic piece of code
Milestone
7.27.0

Description

@Chordrain
Chordrain
opened on May 18, 2026
Issue body actions

Affects PMD Version: 7.24.0

Rule: ReturnEmptyCollectionRatherThanNull — https://docs.pmd-code.org/latest/pmd_rules_java_errorprone.html#returnemptycollectionratherthannull

Description:

ReturnEmptyCollectionRatherThanNull fires on a direct return null; from a method with an array/Collection return type, but it fails to fire when the exact same null value is returned through a trivial intermediate local variable (T result = null; return result;). The semantics are identical — the method still returns null on that path — so the rule should report both forms. The flow-insensitive check on the literal null token causes the rule to miss a clearly equivalent pattern, which undermines its value as a code-smell detector. Note that in the AFTER snippet PMD correctly reports VariableCanBeInlined on the indirection, proving it understands the variable is a trivial pass-through, yet the collection-null check still does not propagate through it.

Code Sample demonstrating the issue:

import java.util.List;
import java.util.ArrayList;

public class ReturnNullDemo {

    private final List<String> items = new ArrayList<>();

    // BEFORE — correctly flagged by ReturnEmptyCollectionRatherThanNull
    public int[] getNumbersBefore(boolean empty) {
        if (empty) {
            return null;            // <-- reported
        }
        return new int[] { 1, 2, 3 };
    }

    public List<String> getItemsBefore(boolean absent) {
        if (absent) {
            return null;            // <-- reported
        }
        return items;
    }

    // AFTER — semantically identical, NOT flagged (false-negative)
    public int[] getNumbersAfter(boolean empty) {
        if (empty) {
            int[] result = null;
            return result;          // <-- NOT reported, but still returns null
        }
        return new int[] { 1, 2, 3 };
    }

    public List<String> getItemsAfter(boolean absent) {
        if (absent) {
            List<String> result = null;
            return result;          // <-- NOT reported, but still returns null
        }
        return items;
    }
}

Expected outcome: ReturnEmptyCollectionRatherThanNull should report getNumbersAfter (line of return result;) and getItemsAfter (line of return result;), since both methods return null for an array/List return type.

Actual outcome: The rule reports only the two BEFORE methods; the AFTER methods (where null flows through a local variable that is immediately returned) are silently accepted.

Running PMD through: CLI

Activity

  1. Chordrain
    added
    a:false-negativePMD doesn't flag a problematic piece of code
    on May 18, 2026
  2. UncleOwen
    changed the title [-][java] ReturnEmptyCollectionRatherThanNull not detected when null is returned via a local variable[/-] [+][java] ReturnEmptyCollectionRatherThanNull false negative when null is returned via a local variable[/+] on May 24, 2026
  3. fudianchn
    added 4 commits that reference this issue on Aug 12, 2026

    [java] Fix pmd#6744: Analyze possible null return values

    1671578

    [java] Fix pmd#6744: Analyze possible null return values

    66a90e3

    [java] Fix pmd#6744: Analyze possible null return values

    11bd726

    [java] Fix pmd#6744: Analyze possible null return values

    fc96f9a
  4. UncleOwen
    added this to the 7.27.0 milestone on Aug 18, 2026
  5. UncleOwen
    added a commit that references this issue on Aug 18, 2026

    [java] Fix #6744: Analyze possible null return values (#6941)

    5d65ca4
  6. UncleOwen
    closed this as completedin #6941on Aug 18, 2026
  7. adangel
    changed the title [-][java] ReturnEmptyCollectionRatherThanNull false negative when null is returned via a local variable[/-] [+][java] ReturnEmptyCollectionRatherThanNull: False negative when null is returned via a local variable[/+] on Aug 18, 2026
  8. adangel
    changed the title [-][java] ReturnEmptyCollectionRatherThanNull: False negative when null is returned via a local variable[/-] [+][java] ReturnEmptyCollectionRatherThanNull: False negative when null is returned through a local variable[/+] on Aug 18, 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-negativePMD doesn't flag a problematic piece of code

    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.