From 5a1223e812b6f85fed299659f444502bb9d1cced Mon Sep 17 00:00:00 2001 From: Tianyu Geng Date: Wed, 16 Jun 2021 21:08:06 -0700 Subject: [PATCH] FIR IDE: don't add return for when expressions when converting to block There doesn't seem to be a good reason why FE1.0 does this. Hence we won't do it in FIR. --- .../intentions/HLConvertToBlockBodyIntention.kt | 7 +------ .../convertToBlockBody/ifWhenUnit.kt.after.fir | 14 ++++++++++++++ .../convertToBlockBody/whenUnit.kt.after.fir | 9 +++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 idea/testData/intentions/convertToBlockBody/ifWhenUnit.kt.after.fir create mode 100644 idea/testData/intentions/convertToBlockBody/whenUnit.kt.after.fir 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