IDEA-176701 Missing warning when collection with null elements is returned by method that does not allow null elements

This commit is contained in:
peter
2017-08-14 19:13:11 +02:00
parent 75179fcfef
commit ec5345c20a
3 changed files with 21 additions and 1 deletions
@@ -278,7 +278,8 @@ public class NullableStuffInspectionBase extends BaseJavaBatchLocalInspectionToo
for (int i = 0; i <= 1; i++) {
PsiType expectedArg = PsiUtil.substituteTypeParameter(expectedType, CommonClassNames.JAVA_UTIL_MAP, i, false);
PsiType assignedArg = PsiUtil.substituteTypeParameter(assignedType, CommonClassNames.JAVA_UTIL_MAP, i, false);
if (isNullityConflict(expectedArg, assignedArg)) {
if (isNullityConflict(expectedArg, assignedArg) ||
expectedArg != null && assignedArg != null && isNullableNotNullCollectionConflict(place, expectedArg, assignedArg)) {
return true;
}
}
@@ -0,0 +1,14 @@
import typeUse.*;
import java.util.*;
class Test {
public @NotNull Map<@NotNull String, @NotNull ArrayList<@NotNull String>> getDataBroken() {
ArrayList<@Nullable String> arrayList = new ArrayList<>();
arrayList.add(null);
@NotNull Map<@NotNull String, @NotNull ArrayList<@Nullable String>> map = new HashMap<>();
map.put("x", arrayList);
return <warning descr="Assigning a collection of nullable elements into a collection of non-null elements">map</warning>;
}
}
@@ -207,6 +207,11 @@ public class NullableStuffInspectionTest extends LightCodeInsightFixtureTestCase
doTest();
}
public void testPassingNullableMapValueWhereNotNullIsExpected() {
DataFlowInspection8Test.setupTypeUseAnnotations("typeUse", myFixture);
doTest();
}
public void testNotNullCollectionItemWithNullableSuperType() {
DataFlowInspection8Test.setupTypeUseAnnotations("typeUse", myFixture);
doTest();