Skip to content

False-positive warning on Objects.requireNonNullElse #1493

Open

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

Activity

  1. msridhar commented on Mar 21, 2026

    @msridhar
    Contributor

    Thanks for the report @ADsty. This should be fixed when we get the JSpecify JDK models incorporated (#950). I've added a (currently ignored) test case for it in #1496. I'm hoping we can get the new JDK models in soon-ish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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