Skip to content

False positive with Optional.or method and JSpecify #1611

Closed

Description

@damianmalczewski

Hi!

While trying to update NullAway from 0.13.4 to 0.13.7, there's an issue with JSpecify and handling Optional.or(Supplier<Optional<T>>) method.

I've prepared a following sample:

@NullMarked
public class Main {

    public static Optional<String> getKey(@Nullable String key) {
        return Optional.ofNullable(key).or(() -> Optional.ofNullable(System.getenv("DEFAULT_KEY")));
    }

    public static void main() {
        Optional<String> code = getKey("TEST_KEY");
        code.ifPresent(s -> System.out.println("Value: " + s));
    }
}
Execution failed for task ':compileJava' (registered by plugin class 'org.gradle.api.plugins.JavaBasePlugin').
> Compilation failed; see the compiler output below.
  /home/damian/repo/nullaway-optional-issue/src/main/java/org/example/Main.java:11: error: [NullAway] incompatible types: Optional<@Nullable String> cannot be converted to Optional<String>
          return Optional.ofNullable(key).or(() -> Optional.ofNullable(System.getenv("DEFAULT_KEY")));
                                            ^
      (see http://t.uber.com/nullaway )
  1 error

I would assume that in this case, the point of or method is to also allow Optional that might also be empty.

I've prepared a sample project (update nullaway version in build.gradle.kts to see when it passes and when fails) - https://github.com/damianmalczewski/nullaway-optional-issue.

Activity

  1. msridhar commented on Jun 17, 2026

    @msridhar
    Contributor

    Thanks for the report! I think this might be related to #950 but when I get a bit of time I'll dig more and confirm; maybe we can do a narrower fix.

  2. lbenedetto commented on Jun 18, 2026

    @lbenedetto

    This seems related to #1594. There seem to be various ways which Nullaway can be tricked into thinking an Optional<@Nullable Object> is possible, even though it is impossible under all circumstances.

  3. deleted a comment from dyrpsf on Jun 19, 2026
  4. added a commit that references this issue on Jun 26, 2026
    f40a9fa
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