Report nullable Map.get if map values are nullable

Part of IDEA-236740

GitOrigin-RevId: d507993271d302a7a5fb2e3d1aacb3274cea79b1
This commit is contained in:
Tagir Valeev
2020-04-27 10:33:18 +00:00
committed by intellij-monorepo-bot
parent 95e0050d31
commit 8fbbd7a3de
3 changed files with 8 additions and 2 deletions
@@ -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);
@@ -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").<warning descr="Method invocation 'intValue' may produce 'NullPointerException'">intValue</warning>();
hashMap.get("y").<warning descr="Method invocation 'intValue' may produce 'NullPointerException'">intValue</warning>();
}
public void main2(Map<@NotNull String, @NotNull Integer> map, HashMap<@NotNull String, @NotNull Integer> hashMap) {
Integer value = map.remove("y");
if (value == null) {
@@ -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() {