IDEA-224476 False “May produce NullPointerException“ positive with Contract and method with parameters

GitOrigin-RevId: 0dddc582af0ac00cab70b2a237f11afa706477df
This commit is contained in:
Tagir Valeev
2019-10-11 06:02:29 +00:00
committed by intellij-monorepo-bot
parent 0d410a5d3c
commit b5044991da
3 changed files with 39 additions and 1 deletions
@@ -298,11 +298,16 @@ public abstract class ContractValue {
@Override
public DfaCallArguments updateArguments(DfaCallArguments arguments, boolean negated) {
DfaNullability targetNullability = DfaNullability.NOT_NULL;
int index = getNullCheckedArgument(negated).orElse(-1);
if (index == -1) {
index = getNullCheckedArgument(!negated).orElse(-1);
targetNullability = DfaNullability.NULL;
}
if (index >= 0 && index < arguments.myArguments.length) {
DfaValue arg = arguments.myArguments[index];
if (arg instanceof DfaFactMapValue) {
DfaValue newArg = ((DfaFactMapValue)arg).withFact(DfaFactType.NULLABILITY, DfaNullability.NOT_NULL);
DfaValue newArg = ((DfaFactMapValue)arg).withFact(DfaFactType.NULLABILITY, targetNullability);
if (newArg != arg) {
DfaValue[] newArguments = arguments.myArguments.clone();
newArguments[index] = newArg;
@@ -0,0 +1,32 @@
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
class TestCls {
public String broken(@NotNull Getter dowGetter) {
final DayOfWeek dowOrDefault = firstNonNull(dowGetter.getDow(""), DayOfWeek.MONDAY);
return dowOrDefault.name();
}
public String correct(@NotNull Getter dowGetter) {
final DayOfWeek dowOrDefault = firstNonNull(dowGetter.getDow(), DayOfWeek.MONDAY);
return dowOrDefault.name();
}
@Nullable
@Contract(value = "!null, _ -> param1; null, !null -> param2; null, null -> null", pure = true)
public static <T> T firstNonNull(@Nullable T value1, @Nullable T value2) {
return value1 == null ? value2 : value1;
}
public interface Getter {
@Nullable
DayOfWeek getDow();
@Nullable
DayOfWeek getDow(String param);
}
enum DayOfWeek {
MONDAY
}
}
@@ -251,6 +251,7 @@ public class DataFlowInspection8Test extends DataFlowInspectionTestCase {
public void testLambdaWritesArrayInTry() { doTest(); }
public void testManyNestedOptionals() { doTest(); }
public void testGetClass() { doTest(); }
public void testParamContract() { doTest(); }
public void testTypeUseVarArg() {
setupTypeUseAnnotations("typeUse", myFixture);
doTest();