diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLConvertToBlockBodyIntention.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLConvertToBlockBodyIntention.kt index 9fe1f033067..a5e654a54cc 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLConvertToBlockBodyIntention.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLConvertToBlockBodyIntention.kt @@ -17,8 +17,6 @@ import org.jetbrains.kotlin.idea.fir.applicators.ApplicabilityRanges import org.jetbrains.kotlin.idea.formatter.adjustLineIndent import org.jetbrains.kotlin.idea.frontend.api.KtAnalysisSession import org.jetbrains.kotlin.idea.frontend.api.types.KtClassErrorType -import org.jetbrains.kotlin.idea.frontend.api.types.KtType -import org.jetbrains.kotlin.idea.util.resultingWhens import org.jetbrains.kotlin.idea.util.setType import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.psi.* @@ -29,7 +27,6 @@ class HLConvertToBlockBodyIntention : AbstractHLIntention(KtDeclarationWithBody::class, applicator) { class Input( - val returnType: KtType, val returnTypeIsUnit: Boolean, val returnTypeIsNothing: Boolean, val returnTypeString: String, @@ -58,7 +55,6 @@ class HLConvertToBlockBodyIntention : val returnType = declaration.getReturnKtType().approximateToSuperPublicDenotableOrSelf() val bodyType = body.getKtType() return Input( - returnType, returnType.isUnit, returnType.isNothing, returnType.render(), @@ -104,9 +100,8 @@ class HLConvertToBlockBodyIntention : private fun generateBody(body: KtExpression, input: Input, returnsValue: Boolean): KtExpression { val factory = KtPsiFactory(body) if (input.bodyTypeIsUnit && body is KtNameReferenceExpression) return factory.createEmptyBody() - val unitWhenAsResult = input.bodyTypeIsUnit && body.resultingWhens().isNotEmpty() val needReturn = returnsValue && (!input.bodyTypeIsUnit && !input.bodyTypeIsNothing) - return if (needReturn || unitWhenAsResult) { + return if (needReturn) { val annotatedExpr = body as? KtAnnotatedExpression val returnedExpr = annotatedExpr?.baseExpression ?: body val block = factory.createSingleStatementBlock(factory.createExpressionByPattern("return $0", returnedExpr)) diff --git a/idea/testData/intentions/convertToBlockBody/ifWhenUnit.kt.after.fir b/idea/testData/intentions/convertToBlockBody/ifWhenUnit.kt.after.fir new file mode 100644 index 00000000000..24beaabc860 --- /dev/null +++ b/idea/testData/intentions/convertToBlockBody/ifWhenUnit.kt.after.fir @@ -0,0 +1,14 @@ +enum class AccessMode { READ, WRITE, EXECUTE } + +fun foo() {} + +fun whenExpr(access: AccessMode, arg: Boolean) { + if (arg) {} else { + foo() + when (access) { + AccessMode.READ -> {} + AccessMode.WRITE -> {} + AccessMode.EXECUTE -> {} + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToBlockBody/whenUnit.kt.after.fir b/idea/testData/intentions/convertToBlockBody/whenUnit.kt.after.fir new file mode 100644 index 00000000000..0097f32c825 --- /dev/null +++ b/idea/testData/intentions/convertToBlockBody/whenUnit.kt.after.fir @@ -0,0 +1,9 @@ +enum class AccessMode { READ, WRITE, EXECUTE } + +fun whenExpr(access: AccessMode) { + when (access) { + AccessMode.READ -> {} + AccessMode.WRITE -> {} + AccessMode.EXECUTE -> {} + } +} \ No newline at end of file