diff --git a/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/declarations/RedundantUnitReturnTypeInspection.kt b/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/declarations/RedundantUnitReturnTypeInspection.kt index 6f36fe70f062..2a6430483aed 100644 --- a/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/declarations/RedundantUnitReturnTypeInspection.kt +++ b/plugins/kotlin/code-insight/inspections-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/inspections/declarations/RedundantUnitReturnTypeInspection.kt @@ -42,9 +42,14 @@ internal class RedundantUnitReturnTypeInspection : } context(KtAnalysisSession) - override fun prepareContext(element: KtNamedFunction): TypeInfo? = when { - element.getFunctionLikeSymbol().returnType.isUnit -> TypeInfo(TypeInfo.UNIT) - else -> null + override fun prepareContext(element: KtNamedFunction): TypeInfo? { + val returnType = element.getFunctionLikeSymbol().returnType + + if (!returnType.isMarkedNullable && returnType.isUnit) { + return TypeInfo(TypeInfo.UNIT) + } + + return null } override fun createQuickFix( diff --git a/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java b/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java index 39b9d26540c2..831fc542cc94 100644 --- a/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java +++ b/plugins/kotlin/code-insight/inspections-k2/tests/test/org/jetbrains/kotlin/idea/k2/inspections/tests/K2LocalInspectionTestGenerated.java @@ -4918,6 +4918,11 @@ public abstract class K2LocalInspectionTestGenerated extends AbstractK2LocalInsp runTest("../../../idea/tests/testData/inspectionsLocal/redundantUnitReturnType/expressionBodyUnitType.kt"); } + @TestMetadata("nullableUnit.kt") + public void testNullableUnit() throws Exception { + runTest("../../../idea/tests/testData/inspectionsLocal/redundantUnitReturnType/nullableUnit.kt"); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { runTest("../../../idea/tests/testData/inspectionsLocal/redundantUnitReturnType/simple.kt"); diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index fd3b83a2d6d5..51c70751bd13 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -10742,6 +10742,11 @@ public abstract class LocalInspectionTestGenerated extends AbstractLocalInspecti runTest("testData/inspectionsLocal/redundantUnitReturnType/expressionBodyUnitType.kt"); } + @TestMetadata("nullableUnit.kt") + public void testNullableUnit() throws Exception { + runTest("testData/inspectionsLocal/redundantUnitReturnType/nullableUnit.kt"); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { runTest("testData/inspectionsLocal/redundantUnitReturnType/simple.kt"); diff --git a/plugins/kotlin/idea/tests/testData/inspectionsLocal/redundantUnitReturnType/nullableUnit.kt b/plugins/kotlin/idea/tests/testData/inspectionsLocal/redundantUnitReturnType/nullableUnit.kt new file mode 100644 index 000000000000..36e916e81f43 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/inspectionsLocal/redundantUnitReturnType/nullableUnit.kt @@ -0,0 +1,4 @@ +// PROBLEM: none +fun test(): Unit? { + return null +} \ No newline at end of file