Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a @NonNull value where nullness checker expects @Nullable is sometimes an error #4964

Closed
AlexLloyd0 opened this issue Nov 15, 2021 · 1 comment
Assignees

Comments

@AlexLloyd0
Copy link

I'm trying to pass a @NonNull value where the nullness checker expects @Nullable. This seems safe to me but I get an error.

commands: checker-framework-3.19.0/checker/bin/javac -processor nullness NullnessCheckerDemo.java

inputs:
NullnessCheckerDemo.java:

package com.google.nullnesscheckerdemo;

import java.util.Optional;

final class NullnessCheckerDemo {
  private static <T extends Object> Optional<T> demo(Optional<Container<T>> maybeContainer) {
    return Optional.<T>empty().or(() -> maybeContainer.map(container -> container.element));
  }

  class Container<T extends Object> {
    public final T element;

    Container(T element) {
      this.element = element;
    }
  }
}

Removing any one of the generic, the container or the Optional make the test pass.

outputs:

NullnessCheckerDemo.java:7: error: [return] incompatible types in return.
    return Optional.<T>empty().or(() -> maybeContainer.map(container -> container.element));
                                                                                 ^
  type of expression: T extends @Initialized @NonNull Object
  method return type: T extends @Initialized @Nullable Object
1 error

expectation:
no error

@smillst smillst self-assigned this Nov 22, 2021
@smillst
Copy link
Member

smillst commented May 21, 2024

This was fixed with #979.

@smillst smillst closed this as completed May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants