[java-dfa] Do not check nullability match for primitive methodref return types

DfaPsiUtil returns UNKNOWN nullability for primitive types, but in future we may return NOT_NULL, which is technically more correct. On the other hand, here implicit unboxing is possible, and it's already handled elsewhere, so we won't need an extra warning.
Part of IDEA-372347 Java type inference should respect nullability

GitOrigin-RevId: b864bfc4995a9b5e0e6a13cba807bd8b5b7d04ca
This commit is contained in:
Tagir Valeev
2025-05-30 12:52:09 +02:00
committed by intellij-monorepo-bot
parent 5259525948
commit 211321c88a

View File

@@ -77,10 +77,13 @@ public class MethodReferenceInstruction extends ExpressionPushingInstruction {
dfType = typedObject(returnType, DfaPsiUtil.getElementNullability(returnType, method));
}
DfaValue defaultResult = interpreter.getFactory().fromDfType(dfType);
Nullability expectedNullability = DfaPsiUtil.getTypeNullability(LambdaUtil.getFunctionalInterfaceReturnType(methodRef));
if (expectedNullability == Nullability.NOT_NULL) {
CheckNotNullInstruction.checkNotNullable(interpreter, state, defaultResult,
NullabilityProblemKind.nullableFunctionReturn.problem(methodRef, null));
PsiType type = LambdaUtil.getFunctionalInterfaceReturnType(methodRef);
if (!(type instanceof PsiPrimitiveType)) { // primitive type may be subject to boxing and handled separately
Nullability expectedNullability = DfaPsiUtil.getTypeNullability(type);
if (expectedNullability == Nullability.NOT_NULL) {
CheckNotNullInstruction.checkNotNullable(interpreter, state, defaultResult,
NullabilityProblemKind.nullableFunctionReturn.problem(methodRef, null));
}
}
if (contracts.isEmpty() || !JavaMethodContractUtil.isPure(method)) return;
Set<DfaCallState> currentStates = Collections.singleton(new DfaCallState(state.createClosureState(), callArguments, defaultResult));