hardcode Map#get as having unknown nullity (IDEA-176626)

until we have a better solution
This commit is contained in:
peter
2017-10-12 20:02:39 +02:00
parent 6551b38303
commit 4eb2fe474e
3 changed files with 30 additions and 0 deletions
@@ -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();
@@ -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");
}
}
}
@@ -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);