redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.08k stars 438 forks source link

`Null type safety` warnings occur in method references that take primitive values as arguments #2716

Open mazeneko opened 2 years ago

mazeneko commented 2 years ago

When such code is written, a warning occurs. But I expected no warning here.

Is this behavior reasonable?

package example;

import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

public class App {
    public static void main(String[] args) {
        List.of(1, 2, 3)
                .stream()
                .map(BigInteger::valueOf)
                .forEach(System.out::println);

        Optional.of(1)
                .map(Math::incrementExact)
                .ifPresent(System.out::println);
    }
}

image

Environment
CsCherrYY commented 2 years ago

looks like related to https://github.com/redhat-developer/vscode-java/issues/2731

jkaye2012 commented 1 year ago

This issue significantly hampers the usefulness of null analysis, at least for me. While I do agree that the warning is often technically valid, in reality these methods end up being used in many places all over most code bases, resulting in a significant amount of warnings generated from non-user code (as we have no way to alter these method contracts without e.g. wrapping every unboxing operation ourselves).