From ec5345c20a6d3cc2c35a72f2ec86046d4a78ec0d Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 14 Aug 2017 18:12:57 +0200 Subject: [PATCH] IDEA-176701 Missing warning when collection with null elements is returned by method that does not allow null elements --- .../nullable/NullableStuffInspectionBase.java | 3 ++- ...singNullableMapValueWhereNotNullIsExpected.java | 14 ++++++++++++++ .../NullableStuffInspectionTest.java | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/nullableProblems/PassingNullableMapValueWhereNotNullIsExpected.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java b/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java index 31db87c104ca..67e2056e331e 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/nullable/NullableStuffInspectionBase.java @@ -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; } } diff --git a/java/java-tests/testData/inspection/nullableProblems/PassingNullableMapValueWhereNotNullIsExpected.java b/java/java-tests/testData/inspection/nullableProblems/PassingNullableMapValueWhereNotNullIsExpected.java new file mode 100644 index 000000000000..6e3e07e6c9b2 --- /dev/null +++ b/java/java-tests/testData/inspection/nullableProblems/PassingNullableMapValueWhereNotNullIsExpected.java @@ -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 map; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionTest.java index 5c9ce08e5639..646a91679ecc 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/NullableStuffInspectionTest.java @@ -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();