diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToBlockBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToBlockBodyIntention.kt index 8ff388f2ca7..d78401f8b84 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToBlockBodyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToBlockBodyIntention.kt @@ -19,8 +19,10 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.core.setType import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.BindingContext @@ -81,7 +83,15 @@ class ConvertToBlockBodyIntention : SelfTargetingIntention generateBody(declaration.isGetter) + is KtPropertyAccessor -> { + val parent = declaration.parent + if (parent is KtProperty && parent.typeReference == null) { + val descriptor = parent.resolveToDescriptorIfAny() + (descriptor as? CallableDescriptor)?.returnType?.let { parent.setType(it) } + } + + generateBody(declaration.isGetter) + } else -> throw RuntimeException("Unknown declaration type: $declaration") } diff --git a/idea/testData/intentions/convertToBlockBody/getterTypeInferred.kt b/idea/testData/intentions/convertToBlockBody/getterTypeInferred.kt new file mode 100644 index 00000000000..328941e0811 --- /dev/null +++ b/idea/testData/intentions/convertToBlockBody/getterTypeInferred.kt @@ -0,0 +1 @@ +val foo get() = "abc" diff --git a/idea/testData/intentions/convertToBlockBody/getterTypeInferred.kt.after b/idea/testData/intentions/convertToBlockBody/getterTypeInferred.kt.after new file mode 100644 index 00000000000..7249e365513 --- /dev/null +++ b/idea/testData/intentions/convertToBlockBody/getterTypeInferred.kt.after @@ -0,0 +1,4 @@ +val foo: String + get() { + return "abc" + } diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index ffbee40d147..5ae9c43d843 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -6479,6 +6479,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("getterTypeInferred.kt") + public void testGetterTypeInferred() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToBlockBody/getterTypeInferred.kt"); + doTest(fileName); + } + @TestMetadata("getterWithThrow.kt") public void testGetterWithThrow() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToBlockBody/getterWithThrow.kt");