Skip to content

[java] ReturnEmptyCollectionRatherThanNull: False negative when a null value is assigned to a local that is later returned #3880

Closed

Description

@MetaBF

Affects PMD Version:
6.44.0+
7.0.0

Rule: ReturnEmptyCollectionRatherThanNull

Description:
Currently, the rule ReturnEmptyCollectionRatherThanNull detects return null statements, which I think is not enough, because it is not the only way that a function can produce a null value. In the following example, function get() will return null (if flag is false), I think it should be warned.

Code Sample demonstrating the issue:

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

public class ExampleReturnEmptyArrayRatherThanNull {

    public boolean flag;
    public List<String> get(){
        List<String> res = null; // line 8
        if(flag) { 
            res = new ArrayList<>();
            res.add("");
        }
        return res; // line 13, false-negative
    }
}

Expected outcome:

PMD should report a violation at line 13, but doesn't. This is a false-negative.

Running PMD through: [CLI]

Activity

  1. added
    a:false-negative
    on Mar 29, 2022
  2. added
    needs:pmd7-revalidation
    on Mar 17, 2024
  3. removed
    needs:pmd7-revalidation
    on Mar 26, 2024
  4. added 4 commits that reference this issue on Aug 12, 2026
  5. added this to the 7.27.0 milestone on Aug 18, 2026
  6. added a commit that references this issue on Aug 18, 2026
  7. changed the title [-][java] ReturnEmptyCollectionRatherThanNull should disallow potential null return [/-] [+][java] ReturnEmptyCollectionRatherThanNull: False negative on potential null return[/+] on Aug 18, 2026
  8. changed the title [-][java] ReturnEmptyCollectionRatherThanNull: False negative on potential null return[/-] [+][java] ReturnEmptyCollectionRatherThanNull: False negative when a null value is assigned to a local that is later returned[/+] 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