With NullAway:WarnOnGenericInferenceFailure enabled, this code now gets a warning:
import org.jspecify.annotations.*;
@NullUnmarked
class StringUtils {
static <T extends CharSequence> T defaultIfEmpty(T str, T defaultStr) {
return str == null || str.isEmpty() ? defaultStr : str;
}
}
@NullMarked
class Test {
static String f(@Nullable String s) {
return StringUtils.defaultIfEmpty(s, ""); // inference failure: type variable T constrained to be both @NonNull and @Nullable
}
}
I think this is a false positive: defaultIfEmpty's parameter and return types are unspecified-nullness and shouldn't be constraining T.
(Either .<String>defaultIfEmpty or .<@Nullable String>defaultIfEmpty is allowed with no error.)
With
NullAway:WarnOnGenericInferenceFailureenabled, this code now gets a warning:I think this is a false positive: defaultIfEmpty's parameter and return types are unspecified-nullness and shouldn't be constraining T.
(Either
.<String>defaultIfEmptyor.<@Nullable String>defaultIfEmptyis allowed with no error.)