From 8fbbd7a3dea5002841a9cee39f630158744665a5 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 27 Apr 2020 17:17:35 +0700 Subject: [PATCH] Report nullable Map.get if map values are nullable Part of IDEA-236740 GitOrigin-RevId: d507993271d302a7a5fb2e3d1aacb3274cea79b1 --- .../com/intellij/codeInspection/dataFlow/DfaPsiUtil.java | 2 +- ...WithNotNullKeys.java => MapGetWithValueNullability.java} | 6 ++++++ .../java/codeInspection/DataFlowInspection8Test.java | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) rename java/java-tests/testData/inspection/dataFlow/fixture/{MapGetWithNotNullKeys.java => MapGetWithValueNullability.java} (65%) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaPsiUtil.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaPsiUtil.java index ea771598b218..844bcdd86b20 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaPsiUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DfaPsiUtil.java @@ -104,7 +104,7 @@ public class DfaPsiUtil { } if (owner instanceof PsiMethod && isMapMethodWithUnknownNullity((PsiMethod)owner)) { - return Nullability.UNKNOWN; + return getTypeNullability(resultType) == Nullability.NULLABLE ? Nullability.NULLABLE : Nullability.UNKNOWN; } Nullability fromType = getTypeNullability(resultType); diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithNotNullKeys.java b/java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithValueNullability.java similarity index 65% rename from java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithNotNullKeys.java rename to java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithValueNullability.java index 44ed68952ae2..98c1f62f48f8 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithNotNullKeys.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithValueNullability.java @@ -8,6 +8,7 @@ class Test { if (value == null) { System.out.println("it's not contained"); } + map.get("z").intValue(); value = hashMap.get("y"); if (value == null) { @@ -15,6 +16,11 @@ class Test { } } + public void main1(Map<@NotNull String, @Nullable Integer> map, HashMap<@NotNull String, @Nullable Integer> hashMap) { + map.get("y").intValue(); + hashMap.get("y").intValue(); + } + public void main2(Map<@NotNull String, @NotNull Integer> map, HashMap<@NotNull String, @NotNull Integer> hashMap) { Integer value = map.remove("y"); if (value == null) { diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java index f64d1e892354..94ca0ab68e25 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java @@ -197,7 +197,7 @@ public class DataFlowInspection8Test extends DataFlowInspectionTestCase { public void testStreamCustomSumMethod() { doTest(); } public void testStreamReduceLogicalAnd() { doTest(); } - public void testMapGetWithNotNullKeys() { doTestWithCustomAnnotations(); } + public void testMapGetWithValueNullability() { doTestWithCustomAnnotations(); } public void testInferNestedForeachNullability() { doTestWithCustomAnnotations(); } public void testMethodVsExpressionTypeAnnotationConflict() {