Skip to content

incompatible types: Optional<@Nullable SomeType> cannot be converted to Optional<SomeType> #1594

Closed

Description

@throup

Description

NullAway 0.13.6 is inferring Optionals of @Nullable types, whereas NullAway 0.13.4 had correctly derived them as non-null.

We have encountered this in a large Spring Boot / gradle project, and upgrading NullAway introduces hundreds of false positives of this kind. We are using JSpecify annotations and running in JSpecify mode.

The examples we have seen, so far, are generally from Stream terminal operations.

Sample warning message

warning: [NullAway] incompatible types: Optional<@Nullable Double> cannot be converted to Optional<Double>
          .reduce(Double::sum);
                 ^
      (see http://t.uber.com/nullaway )
  1 error
  1 warning

Minimal repro

package example.nullaway;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import org.jspecify.annotations.Nullable;

public class NullAwayExample {
  // Both NullAway 0.13.4 and 0.13.6 accept this method with no violations.
  public static Optional<Double> sumNumbersAsDoubles(@Nullable Number... numbers) {
    return Arrays.stream(numbers)
        .filter(Objects::nonNull)
        .map(Number::doubleValue)
        .reduce(Double::sum);
  }

  // NullAway 0.13.4 accepts this method with no violations.
  //
  // NullAway 0.13.6 does not accept this method, and reports:
  // ```
  // warning: [NullAway] incompatible types: Optional<@Nullable Double> cannot be converted to Optional<Double>
  //          .reduce(Double::sum);
  //                 ^
  //      (see http://t.uber.com/nullaway )
  //  1 error
  //  1 warning
  // ```
  public static Optional<Double> sumDoubles(@Nullable Double... doubles) {
    return Arrays.stream(doubles)
        .filter(Objects::nonNull)
        .reduce(Double::sum);
  }

  // Both NullAway 0.13.4 and 0.13.6 accept this method with no violations.
  public static Optional<Double> sumDoublesWithExplicitMap(@Nullable Double... doubles) {
    return Arrays.stream(doubles)
        .filter(Objects::nonNull)
        .map(Objects::requireNonNull)
        .reduce(Double::sum);
  }
}

Activity

  1. lbenedetto commented on Jun 8, 2026

    @lbenedetto

    Also happens for very simple cases like this one:

    public record Repro(@Nullable String name) {
      public Optional<String> getNameOpt() {
        return Optional.ofNullable(name)
            .filter(it -> true);
      }
    }
  2. msridhar commented on Jun 8, 2026

    @msridhar
    Contributor

    Thank you for all the examples. I think this is related to #1578, but also to library models for Optional. Will prioritize fixing this one.

  3. added a commit that references this issue on Jun 11, 2026
    2b34aa8
  4. lbenedetto commented on Jun 18, 2026

    @lbenedetto

    You only fixed Streams, but this issue was also about Optional. I commented a reproducer.
    I don't think this issue should be marked fixed until Optional filtering is also fixed.

  5. msridhar commented on Jun 18, 2026

    @msridhar
    Contributor

    @lbenedetto I think your issue is a different bug than what was fixed here. Let's track in #1611.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions