Open Ao-senXiong opened 2 months ago
Thanks for the report!
Even though you provide an explicit type argument to nullsLast
in Comparator.<Integer>nullsLast
the type for comparator
should still accept @Nullable
:
https://github.com/eisop/jdk/blob/d1b063fab9b8d5141bc240ec10f6b9ebcafb9d6b/src/java.base/share/classes/java/util/Comparator.java#L413
Can you confirm that's correct? Why did you provide the explicit type argument?
The documentation for Stream#min/max say that they NPE if the min/max is null
.
It seems okay to pass a comparator that accepts null, but there is no way to say that null
should never be the maximum.
https://github.com/eisop/jdk/blob/d1b063fab9b8d5141bc240ec10f6b9ebcafb9d6b/src/java.base/share/classes/java/util/stream/Stream.java#L1244
We could add a receiver type restriction, only allowing min/max on streams that contain no null
values at all.
That might be overly conservative, but would get rid of this NPE.
It would be nice to forbid nullsLast
with max
and nullsFirst
with min
, only on nullable streams. But we would need some separate way to track that and that seems like overkill.
Do you see alternatives?
Take this example,
Command to run it, nullness checker is fine with it, but end up in NPE.