Repository navigation
Description
Affects PMD Version: 7.24.0
Rule: ReturnEmptyCollectionRatherThanNull — https://docs.pmd-code.org/latest/pmd_rules_java_errorprone.html#returnemptycollectionratherthannull
Description:
ReturnEmptyCollectionRatherThanNull fires on a direct return null; from a method with an array/Collection return type, but it fails to fire when the exact same null value is returned through a trivial intermediate local variable (T result = null; return result;). The semantics are identical — the method still returns null on that path — so the rule should report both forms. The flow-insensitive check on the literal null token causes the rule to miss a clearly equivalent pattern, which undermines its value as a code-smell detector. Note that in the AFTER snippet PMD correctly reports VariableCanBeInlined on the indirection, proving it understands the variable is a trivial pass-through, yet the collection-null check still does not propagate through it.
Code Sample demonstrating the issue:
import java.util.List;
import java.util.ArrayList;
public class ReturnNullDemo {
private final List<String> items = new ArrayList<>();
// BEFORE — correctly flagged by ReturnEmptyCollectionRatherThanNull
public int[] getNumbersBefore(boolean empty) {
if (empty) {
return null; // <-- reported
}
return new int[] { 1, 2, 3 };
}
public List<String> getItemsBefore(boolean absent) {
if (absent) {
return null; // <-- reported
}
return items;
}
// AFTER — semantically identical, NOT flagged (false-negative)
public int[] getNumbersAfter(boolean empty) {
if (empty) {
int[] result = null;
return result; // <-- NOT reported, but still returns null
}
return new int[] { 1, 2, 3 };
}
public List<String> getItemsAfter(boolean absent) {
if (absent) {
List<String> result = null;
return result; // <-- NOT reported, but still returns null
}
return items;
}
}Expected outcome: ReturnEmptyCollectionRatherThanNull should report getNumbersAfter (line of return result;) and getItemsAfter (line of return result;), since both methods return null for an array/List return type.
Actual outcome: The rule reports only the two BEFORE methods; the AFTER methods (where null flows through a local variable that is immediately returned) are silently accepted.
Running PMD through: CLI
Activity
[-][java] ReturnEmptyCollectionRatherThanNull not detected when null is returned via a local variable[/-][+][java] ReturnEmptyCollectionRatherThanNull false negative when null is returned via a local variable[/+][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 null is returned via a local variable[/-][+][java] ReturnEmptyCollectionRatherThanNull: False negative when null is returned via a local variable[/+][-][java] ReturnEmptyCollectionRatherThanNull: False negative when null is returned via a local variable[/-][+][java] ReturnEmptyCollectionRatherThanNull: False negative when null is returned through a local variable[/+]