diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitReturnTypeInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitReturnTypeInspection.kt index 1ed3fd15481..88c3ebedaea 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitReturnTypeInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantUnitReturnTypeInspection.kt @@ -28,6 +28,7 @@ import org.jetbrains.kotlin.psi.KtVisitorVoid import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.types.typeUtil.isNothing import org.jetbrains.kotlin.types.typeUtil.isUnit class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() { @@ -43,7 +44,9 @@ class RedundantUnitReturnTypeInspection : AbstractKotlinInspection() { if (!function.hasBlockBody()) { val bodyExpression = function.bodyExpression if (bodyExpression != null) { - val resolvedCall = bodyExpression.getResolvedCall(bodyExpression.analyze(BodyResolveMode.PARTIAL)) + val bodyContext = bodyExpression.analyze(BodyResolveMode.PARTIAL) + if (bodyContext.getType(bodyExpression)?.isNothing() == true) return + val resolvedCall = bodyExpression.getResolvedCall(bodyContext) if (resolvedCall != null) { if (resolvedCall.candidateDescriptor.returnType?.isUnit() != true) return } diff --git a/idea/testData/inspections/redundantUnitReturnType/Test.kt b/idea/testData/inspections/redundantUnitReturnType/Test.kt index 8e51d00f657..b041cbf1817 100644 --- a/idea/testData/inspections/redundantUnitReturnType/Test.kt +++ b/idea/testData/inspections/redundantUnitReturnType/Test.kt @@ -7,4 +7,10 @@ fun bar(): Unit { fun baz(): Unit = println("ok") -fun f1(): Int = 1 \ No newline at end of file +fun f1(): Int = 1 + +fun f2(): Unit { + throw UnsupportedOperationException("") +} + +fun f3(): Unit = throw UnsupportedOperationException("") \ No newline at end of file diff --git a/idea/testData/inspections/redundantUnitReturnType/inspectionData/expected.xml b/idea/testData/inspections/redundantUnitReturnType/inspectionData/expected.xml index c61f7e1895f..9af9a7d7df3 100644 --- a/idea/testData/inspections/redundantUnitReturnType/inspectionData/expected.xml +++ b/idea/testData/inspections/redundantUnitReturnType/inspectionData/expected.xml @@ -23,6 +23,14 @@ Redundant 'Unit' return type Redundant Unit return type + + Test.kt + 12 + light_idea_test_case + + Redundant 'Unit' return type + Redundant Unit return type + WithLambda.kt 11