typetools / typetools/checker-framework

False positive when assigning result of function with type parameters to var

Open
#6,741 0 comments 0 reactions 1 assignee View on GitHub

@smillst is already working on this.

Since Aug 8, 2024.

False Positive (false warning or imprecision)
Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

Might be related to issue #6675

public class Test {

  public static <T> List<T> getInParameter(T value, Class<T> type) {
    return new ArrayList<>(5);
  }

  List<?> getInParameter() {
    var value = 5;
    var type = Integer.class;
    var result = getInParameter(value, type);
    return result;
  }
}

results in

C:\Projects\pvyscomponents\java\libs\kernel\repository\api\src\main\java\com\provys\repository\Test.java
java: [type.argument] incompatible type argument for type parameter E of List.
  found   : @UnknownInitialization @NonNull Integer
  required: @Initialized @Nullable Object
C:\Projects\pvyscomponents\java\libs\kernel\repository\api\src\main\java\com\provys\repository\Test.java:16:32
java: [assignment] incompatible types in assignment.
  found   : @Initialized @NonNull List<@Initialized @NonNull Integer>
  required: @UnknownInitialization @Nullable List<@UnknownInitialization @NonNull Integer>

It should compile without problem.

It compiled in 3.42.0, fails in 3.46.0

If explicit type is specified for target
List<Integer> result = getInParameter(value, type);
it compiles without problem

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.