You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] ReturnEmptyCollectionRatherThanNull: False negatives when a returned expression can evaluate to null #6744
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).
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.
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).
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:
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.
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.
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
Activity
[-][java] FN: `ReturnEmptyCollectionRatherThanNull` misses `return (true ? null : null);`[/-][+][java] ReturnEmptyCollectionRatherThanNull: False negative when returning ternary conditional[/+]adangel commentedon Jun 4, 2026
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:
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).
Chordrain commentedon Jun 10, 2026
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 returnnull. For example: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.
adangel commentedon Jun 11, 2026
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.
exactly
[java] Fix pmd#6744: Analyze possible null return values
[java] Fix pmd#6744: Analyze possible null return values
[java] Fix pmd#6744: Analyze possible null return values
[java] Fix pmd#6744: Analyze possible null return values
[java] Fix pmd#6744: Analyze possible null return values
[java] Fix pmd#6744: Analyze possible null return values
[java] Fix pmd#6744: Analyze possible null return values
[java] Fix pmd#6744: Analyze possible null return values
[java] Fix #6744: Analyze possible null return values (#6941)
[-][java] ReturnEmptyCollectionRatherThanNull: False negative when returning ternary conditional[/-][+][java] ReturnEmptyCollectionRatherThanNull: False negatives when a returned expression can evaluate to null[/+]