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.