From de3907e8cce008dc132a20f6e308c273b90c46b9 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 11 Sep 2020 09:50:57 +0900 Subject: [PATCH] Redundant nullable return type: fix false positive with non-local return #KT-41800 Fixed --- .../RedundantNullableReturnTypeInspection.kt | 9 +++++---- .../returnNullableFromLambdaInBlockBody.kt | 9 +++++++++ ...urnNullableFromLambdaInSingleExpressionBody.kt | 5 +++++ .../property/returnNullableFromLambdaInGetter.kt | 6 ++++++ .../inspections/LocalInspectionTestGenerated.java | 15 +++++++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInBlockBody.kt create mode 100644 idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInSingleExpressionBody.kt create mode 100644 idea/testData/inspectionsLocal/redundantNullableReturnType/property/returnNullableFromLambdaInGetter.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantNullableReturnTypeInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantNullableReturnTypeInspection.kt index 3506e801664..33470bfc281 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantNullableReturnTypeInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantNullableReturnTypeInspection.kt @@ -74,12 +74,13 @@ class RedundantNullableReturnTypeInspection : AbstractKotlinInspection() { val dataFlowValueFactory = getResolutionFacade().getDataFlowValueFactory() val moduleDescriptor = findModuleDescriptor() val languageVersionSettings = languageVersionSettings + val returnTypes = collectDescendantsOfType().flatMap { + it.returnedExpression.types(context, dataFlowValueFactory, moduleDescriptor, languageVersionSettings) + } return if (this is KtBlockExpression) { - collectDescendantsOfType().flatMap { - it.returnedExpression.types(context, dataFlowValueFactory, moduleDescriptor, languageVersionSettings) - } + returnTypes } else { - types(context, dataFlowValueFactory, moduleDescriptor, languageVersionSettings) + returnTypes + types(context, dataFlowValueFactory, moduleDescriptor, languageVersionSettings) } } diff --git a/idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInBlockBody.kt b/idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInBlockBody.kt new file mode 100644 index 00000000000..d0f48e20cbe --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInBlockBody.kt @@ -0,0 +1,9 @@ +// PROBLEM: none +class MyClass + +inline fun acceptMyClass(m: (MyClass?) -> T) {} + +fun one(): MyClass? { + acceptMyClass { return it } + return MyClass() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInSingleExpressionBody.kt b/idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInSingleExpressionBody.kt new file mode 100644 index 00000000000..c6a030fec11 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInSingleExpressionBody.kt @@ -0,0 +1,5 @@ +// PROBLEM: none +// WITH_RUNTIME +fun test(str: String): String? = str.run { + return null +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantNullableReturnType/property/returnNullableFromLambdaInGetter.kt b/idea/testData/inspectionsLocal/redundantNullableReturnType/property/returnNullableFromLambdaInGetter.kt new file mode 100644 index 00000000000..1c20150e6fb --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantNullableReturnType/property/returnNullableFromLambdaInGetter.kt @@ -0,0 +1,6 @@ +// PROBLEM: none +// WITH_RUNTIME +val test: Int? + get() = run { + return null + } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index abd48238dde..e1183f17a25 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -8249,6 +8249,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/overridable.kt"); } + @TestMetadata("returnNullableFromLambdaInBlockBody.kt") + public void testReturnNullableFromLambdaInBlockBody() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInBlockBody.kt"); + } + + @TestMetadata("returnNullableFromLambdaInSingleExpressionBody.kt") + public void testReturnNullableFromLambdaInSingleExpressionBody() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/returnNullableFromLambdaInSingleExpressionBody.kt"); + } + @TestMetadata("singleExpressionBody.kt") public void testSingleExpressionBody() throws Exception { runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/function/singleExpressionBody.kt"); @@ -8287,6 +8297,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/property/initializer.kt"); } + @TestMetadata("returnNullableFromLambdaInGetter.kt") + public void testReturnNullableFromLambdaInGetter() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/property/returnNullableFromLambdaInGetter.kt"); + } + @TestMetadata("var.kt") public void testVar() throws Exception { runTest("idea/testData/inspectionsLocal/redundantNullableReturnType/property/var.kt");