From 4509cd43cbb501bc91afc3c910171a7a3efbbf74 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 19 Sep 2016 10:58:37 +0300 Subject: [PATCH] Convert to expression body fix: do not allow `= Unit` for constructors --- .../idea/intentions/ConvertToExpressionBodyIntention.kt | 8 +++++--- .../convertToExpressionBody/constructorWithEmptyBody.kt | 5 +++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 idea/testData/intentions/convertToExpressionBody/constructorWithEmptyBody.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt index edc73396db4..eb433dda738 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt @@ -98,12 +98,14 @@ class ConvertToExpressionBodyIntention : SelfTargetingOffsetIndependentIntention private fun calcValue(declaration: KtDeclarationWithBody): KtExpression? { val body = declaration.blockExpression() ?: return null - return calcValue(body) + return calcValue(body, emptyAllowed = declaration !is KtConstructor<*>) } - private fun calcValue(body: KtBlockExpression): KtExpression? { + private fun calcValue(body: KtBlockExpression, emptyAllowed: Boolean = true): KtExpression? { val bodyStatements = body.statements - if (bodyStatements.isEmpty()) return KtPsiFactory(body).createExpression("Unit") + if (bodyStatements.isEmpty()) { + return if (emptyAllowed) KtPsiFactory(body).createExpression("Unit") else null + } val statement = bodyStatements.singleOrNull() ?: return null when (statement) { is KtReturnExpression -> { diff --git a/idea/testData/intentions/convertToExpressionBody/constructorWithEmptyBody.kt b/idea/testData/intentions/convertToExpressionBody/constructorWithEmptyBody.kt new file mode 100644 index 00000000000..590744d01a1 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/constructorWithEmptyBody.kt @@ -0,0 +1,5 @@ +// IS_APPLICABLE: false + +class C { + constructor() {} +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 3bafcd4fc93..24d11735a68 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -4903,6 +4903,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("constructorWithEmptyBody.kt") + public void testConstructorWithEmptyBody() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/constructorWithEmptyBody.kt"); + doTest(fileName); + } + @TestMetadata("declaration.kt") public void testDeclaration() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/declaration.kt");