Convert to block body: specify non-nullable type when body expression is platform type and overriden method is non-nullable type

#KT-12222 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-06-18 09:52:57 +09:00
committed by Dmitry Gridin
parent d37b616e1f
commit 0392fe75fc
4 changed files with 33 additions and 1 deletions
@@ -18,8 +18,10 @@ import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isError import org.jetbrains.kotlin.types.isError
import org.jetbrains.kotlin.types.isNullabilityFlexible
import org.jetbrains.kotlin.types.typeUtil.isNothing import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.types.typeUtil.isUnit import org.jetbrains.kotlin.types.typeUtil.isUnit
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody>( class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody>(
KtDeclarationWithBody::class.java, KtDeclarationWithBody::class.java,
@@ -90,6 +92,13 @@ class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody
return declaration return declaration
} }
private fun KtNamedFunction.returnType(): KotlinType? = resolveToDescriptorIfAny()?.returnType private fun KtNamedFunction.returnType(): KotlinType? {
val descriptor = resolveToDescriptorIfAny()
val returnType = descriptor?.returnType ?: return null
if (returnType.isNullabilityFlexible()
&& descriptor.overriddenDescriptors.firstOrNull()?.returnType?.isMarkedNullable == false
) return returnType.makeNotNullable()
return returnType
}
} }
} }
@@ -0,0 +1,8 @@
// WITH_RUNTIME
interface Foo {
fun foo(): String
}
class Bar : Foo {
override fun <caret>foo() = java.lang.String.valueOf(42)
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
interface Foo {
fun foo(): String
}
class Bar : Foo {
override fun foo(): String {
return java.lang.String.valueOf(42)
}
}
@@ -6947,6 +6947,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/convertToBlockBody/nothingFun.kt"); runTest("idea/testData/intentions/convertToBlockBody/nothingFun.kt");
} }
@TestMetadata("overrideWithPlatformType.kt")
public void testOverrideWithPlatformType() throws Exception {
runTest("idea/testData/intentions/convertToBlockBody/overrideWithPlatformType.kt");
}
@TestMetadata("setter.kt") @TestMetadata("setter.kt")
public void testSetter() throws Exception { public void testSetter() throws Exception {
runTest("idea/testData/intentions/convertToBlockBody/setter.kt"); runTest("idea/testData/intentions/convertToBlockBody/setter.kt");