[kotlin] Port AddReturnToLastExpressionInFunctionFix to K2

^KTIJ-29555

GitOrigin-RevId: 4de9d282bd1c6e0e2611ae2cfeac34f79ca969cb
This commit is contained in:
Andrey Cherkasov
2024-07-25 22:02:06 +00:00
committed by intellij-monorepo-bot
parent ca6499d0f3
commit f51da14444
9 changed files with 77 additions and 5 deletions
@@ -133,6 +133,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() {
registerFactory(WrongAnnotationTargetWithUseSiteTargetFixFactory.moveReceiverAnnotation)
registerFactory(IllegalEscapeFixFactory.convertToUnicodeEscape)
registerFactory(IsEnumEntryFixFactory.factory)
registerFactory(NoReturnInFunctionWithBlockBodyFixFactory.addReturnToLastExpression)
}
private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix {
@@ -0,0 +1,23 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.k2.codeinsight.fixes
import org.jetbrains.kotlin.analysis.api.fir.diagnostics.KaFirDiagnostic
import org.jetbrains.kotlin.analysis.api.types.KaErrorType
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixFactory
import org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
import org.jetbrains.kotlin.psi.KtNamedFunction
internal object NoReturnInFunctionWithBlockBodyFixFactory {
val addReturnToLastExpression = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.NoReturnInFunctionWithBlockBody ->
val namedFunction = diagnostic.psi as? KtNamedFunction ?: return@ModCommandBased emptyList()
val last = namedFunction.bodyBlockExpression?.statements?.lastOrNull() ?: return@ModCommandBased emptyList()
val lastType = last.expressionType?.takeIf { it !is KaErrorType } ?: return@ModCommandBased emptyList()
val expectedType = namedFunction.returnType.takeIf { it !is KaErrorType } ?: return@ModCommandBased emptyList()
if (!lastType.isSubtypeOf(expectedType)) return@ModCommandBased emptyList()
listOf(
AddReturnToLastExpressionInFunctionFix(namedFunction)
)
}
}
@@ -6181,7 +6181,49 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/addReturnToLastExpressionInFunction")
public static class AddReturnToLastExpressionInFunction extends AbstractHighLevelQuickFixTest {
@java.lang.Override
@org.jetbrains.annotations.NotNull
public final KotlinPluginMode getPluginMode() {
return KotlinPluginMode.K2;
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("notSubtype.kt")
public void testNotSubtype() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/notSubtype.kt");
}
@TestMetadata("nothing.kt")
public void testNothing() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/nothing.kt");
}
@TestMetadata("simpleBoolean.kt")
public void testSimpleBoolean() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/simpleBoolean.kt");
}
@TestMetadata("subtype.kt")
public void testSubtype() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/subtype.kt");
}
@TestMetadata("typeError.kt")
public void testTypeError() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/typeError.kt");
}
@TestMetadata("typeError2.kt")
public void testTypeError2() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/typeError2.kt");
}
}
@@ -3,4 +3,5 @@ fun test(): Boolean {
true
}<caret>
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
@@ -3,4 +3,5 @@ fun test(): Boolean {
return true
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
@@ -5,4 +5,5 @@ fun foo(): Any {
true
}<caret>
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
@@ -5,4 +5,5 @@ fun foo(): Any {
return true
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix
@@ -8,3 +8,5 @@
fun some(): Any {
FunctionReference::class
}<caret>
/* IGNORE_K2 */
@@ -81,7 +81,7 @@ internal fun MutableTWorkspace.generateK2FixTests() {
model("$idea/quickfix/addNoinline", pattern = pattern)
model("$idea/quickfix/addReifiedToTypeParameterOfFunctionFix", pattern = pattern)
model("$idea/quickfix/addReturnExpression", pattern = pattern)
model("$idea/quickfix/addReturnToLastExpressionInFunction", pattern = pattern, isIgnored = true)
model("$idea/quickfix/addReturnToLastExpressionInFunction", pattern = pattern)
model("$idea/quickfix/addReturnToUnusedLastExpressionInFunction", pattern = pattern, isIgnored = true)
model("$idea/quickfix/addRunBeforeLambda", pattern = pattern, isIgnored = true)
model("$idea/quickfix/addSemicolonBeforeLambdaExpression", pattern = pattern, isIgnored = true)