From 4eb2fe474e49cafc2376a2025a044460c36fa16f Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 12 Oct 2017 19:36:45 +0200 Subject: [PATCH] hardcode Map#get as having unknown nullity (IDEA-176626) until we have a better solution --- .../codeInspection/dataFlow/DfaPsiUtil.java | 11 +++++++++++ .../dataFlow/fixture/MapGetWithNotNullKeys.java | 17 +++++++++++++++++ .../codeInspection/DataFlowInspection8Test.java | 2 ++ 3 files changed, 30 insertions(+) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithNotNullKeys.java 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 c41ad7d71e8c..377519baf13b 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 @@ -25,6 +25,7 @@ import com.intellij.lang.java.JavaLanguage; import com.intellij.openapi.util.Ref; import com.intellij.psi.*; import com.intellij.psi.search.LocalSearchScope; +import com.intellij.psi.search.searches.DeepestSuperMethodsSearch; import com.intellij.psi.search.searches.ReferencesSearch; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.*; @@ -103,6 +104,10 @@ public class DfaPsiUtil { return Nullness.NOT_NULL; } + if (owner instanceof PsiMethod && isMapGet((PsiMethod)owner)) { + return Nullness.UNKNOWN; + } + Nullness fromType = getTypeNullability(resultType); if (fromType != Nullness.UNKNOWN) return fromType; @@ -120,6 +125,12 @@ public class DfaPsiUtil { return Nullness.UNKNOWN; } + private static boolean isMapGet(@NotNull PsiMethod method) { + if (!"get".equals(method.getName())) return false; + PsiMethod superMethod = DeepestSuperMethodsSearch.search(method).findFirst(); + return "java.util.Map.get".equals(PsiUtil.getMemberQualifiedName(superMethod != null ? superMethod : method)); + } + @NotNull public static Nullness inferParameterNullability(@NotNull PsiParameter parameter) { PsiElement parent = parameter.getParent(); diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithNotNullKeys.java b/java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithNotNullKeys.java new file mode 100644 index 000000000000..c9d61344c22b --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/MapGetWithNotNullKeys.java @@ -0,0 +1,17 @@ +import foo.*; +import java.util.*; + +class Test { + + public void main(Map<@NotNull String, @NotNull Integer> map, HashMap<@NotNull String, @NotNull Integer> hashMap) { + Integer value = map.get("y"); + if (value == null) { + System.out.println("it's not contained"); + } + + value = hashMap.get("y"); + if (value == null) { + System.out.println("it's not contained"); + } + } +} \ No newline at end of file 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 7cd89c5f7851..dbb3aeb28832 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspection8Test.java @@ -197,6 +197,8 @@ public class DataFlowInspection8Test extends DataFlowInspectionTestCase { public void testStreamInlining() { doTest(); } public void testStreamComparatorInlining() { doTest(); } public void testStreamKnownSource() { doTest(); } + + public void testMapGetWithNotNullKeys() { doTestWithCustomAnnotations(); } public void testMethodVsExpressionTypeAnnotationConflict() { setupAmbiguousAnnotations("withTypeUse", myFixture);