From 1e5fb91d7dcd86812853c7af540698b76e5c87e5 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 19 Sep 2016 11:45:38 +0300 Subject: [PATCH] Minor refactoring: convert to expression body --- .../idea/intentions/ConvertToExpressionBodyIntention.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt index 34def459137..63793b39c60 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt @@ -37,6 +37,7 @@ class ConvertToExpressionBodyIntention( KtDeclarationWithBody::class.java, "Convert to expression body" ) { override fun isApplicableTo(element: KtDeclarationWithBody): Boolean { + if (element is KtConstructor<*>) return false val value = calcValue(element) ?: return false return !value.anyDescendantOfType( canGoInside = { it !is KtFunctionLiteral && it !is KtNamedFunction && it !is KtPropertyAccessor } @@ -100,13 +101,13 @@ class ConvertToExpressionBodyIntention( private fun calcValue(declaration: KtDeclarationWithBody): KtExpression? { val body = declaration.blockExpression() ?: return null - return calcValue(body, emptyAllowed = declaration !is KtConstructor<*> && convertEmptyToUnit) + return calcValue(body) } - private fun calcValue(body: KtBlockExpression, emptyAllowed: Boolean = true): KtExpression? { + private fun calcValue(body: KtBlockExpression): KtExpression? { val bodyStatements = body.statements if (bodyStatements.isEmpty()) { - return if (emptyAllowed) KtPsiFactory(body).createExpression("Unit") else null + return if (convertEmptyToUnit) KtPsiFactory(body).createExpression("Unit") else null } val statement = bodyStatements.singleOrNull() ?: return null when (statement) {