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

False positive related to local variable type inference from conditional statement #6290

Closed
MichalStehlikCz opened this issue Nov 10, 2023 · 0 comments · Fixed by #6310
Closed
Assignees

Comments

@MichalStehlikCz
Copy link
Contributor

After upgrade from 3.32.0 > 3.40.0, I get warnings on following code

  public Optional<String> test(@Nullable String param) {
    var first = Optional.ofNullable(param);
    var second = first.isPresent() ? first : Optional.ofNullable(param);
    return second;
  }

It now produces

java: [return] incompatible types in return.
  type of expression: @Initialized @NonNull Optional<@Initialized @Nullable String>
  method return type: @Initialized @NonNull Optional<@Initialized @NonNull String>

which I believe is false positive.

Equivalent code with both variables using explicit type

  public Optional<String> test(@Nullable String param) {
    Optional<String> first = Optional.ofNullable(param);
    Optional<String> second = first.isPresent() ? first : Optional.ofNullable(param);
    return second;
  }

produces no warnings

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

Successfully merging a pull request may close this issue.

2 participants