Skip to content

[java] ReturnEmptyCollectionRatherThanNull: False negatives when a returned expression can evaluate to null #6744

Closed

Description

@Chordrain

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

Summary

PMD's ReturnEmptyCollectionRatherThanNull rule flags return null; statements inside methods whose declared return type is a Collection (or one of its subtypes) or an array. The matcher only recognizes a bare return <NullLiteral>; and does not look through a constant-condition ternary.

Wrapping the null in a constant-true ternary — return (true ? null : null); — silences the rule (false negative), even though the method still unconditionally returns the null literal on every execution path (the ternary's value is null per JLS §15.25).

Reproducer

before — bare return null; (1 finding)

import java.util.List;

public class ReturnNullBefore {
    public List<String> getList() {
        return null;                       // flagged
    }
}

after — ternary-wrapped null (0 findings)

import java.util.List;

public class ReturnNullAfter {
    public List<String> getList() {
        return (true ? null : null);       // FALSE NEGATIVE: not flagged
    }
}

Expected behavior

ReturnEmptyCollectionRatherThanNull should report the same violation on BEFORE and AFTER. Both methods are declared to return List<String> and both return the null literal on every path; the constant-true ternary (true ? null : null) is a literal-null expression per JLS §15.25.

Actual behavior

  • return null; → 1 finding: Return an empty collection rather than null. (correct).
  • return (true ? null : null); → 0 findings, even though the method still unconditionally returns null.

The matcher inspects only a bare return <NullLiteral>; AST and misses null returns wrapped in a parenthesised / constant-condition ternary.

Activity

  1. added
    a:false-negative
    on Jun 1, 2026
  2. changed the title [-][java] FN: `ReturnEmptyCollectionRatherThanNull` misses `return (true ? null : null);`[/-] [+][java] ReturnEmptyCollectionRatherThanNull: False negative when returning ternary conditional[/+] on Jun 4, 2026
  3. adangel commented on Jun 4, 2026

    @adangel
    Member

    the constant-true ternary (true ? null : null)

    This looks contrived. Did you see actual code that uses this construct? (It would qualify in a rule on its own - unnecessarily complicated way of writing null)

    I'm not sure, if the rule should concentrate on this very specific, special case.

    What about:

    return someCondition() ? aList : null;
    

    Is this already covered? I don't think so.

    The rule should probably be improved to check, whether null would be returned via a ternary operator (regardless of the condition).

  4. Chordrain commented on Jun 10, 2026

    @Chordrain
    Author

    Fair point. My intention is not to argue that (true ? null : null) is a realistic pattern by itself. It was simply the smallest reproducer I found.

    The broader issue seems to be that the rule only detects a bare return null; and does not inspect conditional expressions that may return null. For example:

    import java.util.List;
    
    public class Sample {
        public List<String> getList() {
            List<String> aList;
            return false ? aList : null;
        }
    }

    This also produces no warning.

    So the problem appears to be wider than the constant-true ternary case: null-returning branches within conditional expressions are currently missed by the rule.

  5. adangel commented on Jun 11, 2026

    @adangel
    Member

    It was simply the smallest reproducer I found.

    Sure. The concern I have is, that this might be misleading, if someone tries to fix this - one might concentrate only on this simple case and miss the underlying problem.

    So the problem appears to be wider than the constant-true ternary case: null-returning branches within conditional expressions are currently missed by the rule.

    exactly

  6. added 8 commits that reference this issue on Aug 11, 2026
    a9f1d2f
    a2299fb
    c0491ba
    bae7841
    1671578
    66a90e3
    11bd726
    fc96f9a
  7. added this to the 7.27.0 milestone on Aug 18, 2026
  8. added a commit that references this issue on Aug 18, 2026
    5d65ca4
  9. changed the title [-][java] ReturnEmptyCollectionRatherThanNull: False negative when returning ternary conditional[/-] [+][java] ReturnEmptyCollectionRatherThanNull: False negatives when a returned expression can evaluate to null[/+] 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