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 in the Optional Checker #6848

Open
jyoo980 opened this issue Oct 8, 2024 · 0 comments
Open

False positive in the Optional Checker #6848

jyoo980 opened this issue Oct 8, 2024 · 0 comments

Comments

@jyoo980
Copy link
Contributor

jyoo980 commented Oct 8, 2024

Overview

The updated (to-be-released) Optional Checker has increased precision in its analysis of Optional values resulting from operations on container types. For example, it is able to deduce that the terminal .get() operation below is safe (i.e., will not throw a NoSuchElementException):

@NonEmpty Stream<Integer> lens = List.of("foo").stream()
    .map(String::length);
// Calling `Stream.max` on a non-empty container will always yield a present `Optional`
lens.max(Integer::compare).get();

Issue

The updated Optional Checker suffers false positives when the Optional-resulting operation depends on the type of the earliest leftmost receiver whose type is @PolyNonEmpty.

For example, the following minimal example (also attached as Main.java) should cleanly type-check:

import java.util.Optional;
import java.util.List;

import org.checkerframework.checker.nonempty.qual.*;

class Main {

  class Container {

    @NonEmpty List<String> getStrs() {
      throw new Error();
    }
  }

  void foo() {
    Container container = new Container();
    container.getStrs().stream().findAny().get();
  }
}

But it fails with the error message:

Main.java:17: error: [method.invocation] call to get() not allowed on the given receiver.
    container.getStrs().stream().findAny().get();
                                              ^
  found   : @MaybePresent Optional<@MaybePresent String>
  required: @Present Optional<@MaybePresent String>
1 error

Steps to Reproduce

Execute the test case with the following command:

javacheck -processor nonempty -ArunAsOptionalChecker -AassumePure Main.java

Expected Behavior: Main.java should cleanly type-check.

Actual Behavior: Main.java does not cleanly type-check.

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

No branches or pull requests

1 participant