typetools / typetools/checker-framework

Nullable type unnecessarily inferred converting List<? extends T> to List<? extends T>

Open
#2,722 0 comments 0 reactions 1 assignee View on GitHub

@wmdietl is already working on this.

Since Dec 16, 2019.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

More from playing around with #2719. This one is only a false positive and might well be a known issue. (I was going to say it could be part of #979, but javac 7 is able to infer that get(0) returns a String, so I would naively expect the Checker Framework to infer that it returns a non-null String.)

import static java.util.Arrays.asList;

import java.util.List;

class Foo {
  void foo() {
    passThrough(asList("x")).get(0).length();
  }

  <T> List<? extends T> passThrough(List<? extends T> object) {
    return object;
  }
}
$ checker/bin/javac -processor org.checkerframework.checker.nullness.NullnessChecker Foo.java 
Foo.java:7: error: [dereference.of.nullable] dereference of possibly-null reference passThrough(asList("x")).get(0)
    passThrough(asList("x")).get(0).length();
                                ^
1 error

Interestingly, if I change foo() to the following, then it compiles:

  String foo() {
    return passThrough(asList("x")).get(0);
  }

And of course it also compiles if I pass an explicit <String> argument to either asList or passThrough.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.