Skip to content

False-positive warning on Objects.requireNonNullElse #1493

@ADsty

Description

@ADsty

In my project I want to use NullAway Gradle plugin with warnOnGenericInferenceFailure feature enabled, but I am getting warnings on Objects.requireNonNullElse whenever I use it as return value of method. For example, this method:

public String nonNullElse(@Nullable String nullableString, String defaultValue) {
    return Objects.requireNonNullElse(nullableString, defaultValue);
}

would result in a warning:

[NullAway] Failed to infer type argument nullability for call Objects.requireNonNullElse(nullableString, defaultValue): Contradictory nullability for T: NONNULL vs. NULLABLE

The only workaround for now is to write something like this:

public String nonNullElse(@Nullable String nullableString, String defaultValue) {
    return Objects.<String>requireNonNullElse(nullableString, defaultValue);
}

which is redundant as it should be obvious that Objects.requireNonNullElse never returns null in this situation

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions