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 negatives when a returned expression can evaluate to null · Issue #6744 · pmd/pmd

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

New issue
Copy link
New issue
Copy link
Closed
#6941
Closed
[java] ReturnEmptyCollectionRatherThanNull: False negatives when a returned expression can evaluate to null#6744
#6941
Copy link
Labels
a:false-negativePMD doesn't flag a problematic piece of code
Milestone
7.27.0

Description

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

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. Chordrain
    added
    a:false-negativePMD doesn't flag a problematic piece of code
    on Jun 1, 2026
  2. adangel
    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
    adangel
    on Jun 4, 2026
    Member
    More actions

    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
    Chordrain
    on Jun 10, 2026
    Author
    More actions

    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
    adangel
    on Jun 11, 2026
    Member
    More actions

    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. fudianchn
    added 8 commits that reference this issue on Aug 11, 2026

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

    This issue will close once commit a9f1d2f is merged into the 'main' branch.
    a9f1d2f

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

    This issue will close once commit a2299fb is merged into the 'main' branch.
    a2299fb

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

    This issue will close once commit c0491ba is merged into the 'main' branch.
    c0491ba

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

    This issue will close once commit bae7841 is merged into the 'main' branch.
    bae7841

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

    This issue will close once commit 1671578 is merged into the 'main' branch.
    1671578

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

    This issue will close once commit 66a90e3 is merged into the 'main' branch.
    66a90e3

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

    This issue will close once commit 11bd726 is merged into the 'main' branch.
    11bd726

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

    This issue will close once commit fc96f9a is merged into the 'main' branch.
    fc96f9a
  7. UncleOwen
    added this to the 7.27.0 milestone on Aug 18, 2026
  8. UncleOwen
    added a commit that references this issue on Aug 18, 2026

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

    This issue will close once commit 5d65ca4 is merged into the 'main' branch.
    5d65ca4
  9. UncleOwen
    closed this as completedin #6941on Aug 18, 2026
  10. adangel
    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-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.