Skip to content

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

Closed

Description

@Chordrain

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. added
    a:false-negative
    on May 18, 2026
  2. 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. added 4 commits that reference this issue on Aug 12, 2026
  4. added this to the 7.27.0 milestone on Aug 18, 2026
  5. added a commit that references this issue on Aug 18, 2026
  6. 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
  7. 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-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions