Convert to expression body fix: do not allow = Unit for constructors

This commit is contained in:
Mikhail Glukhikh
2016-09-19 10:58:37 +03:00
parent 24c7efd323
commit 4509cd43cb
3 changed files with 16 additions and 3 deletions
@@ -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 -> {
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
class C {
constructor()<caret> {}
}
@@ -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");