From f51da1444453fe7897e005703111d7ca3be1fa25 Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Thu, 25 Jul 2024 20:36:36 +0400 Subject: [PATCH] [kotlin] Port AddReturnToLastExpressionInFunctionFix to K2 ^KTIJ-29555 GitOrigin-RevId: 4de9d282bd1c6e0e2611ae2cfeac34f79ca969cb --- .../fixes/KotlinK2QuickFixRegistrar.kt | 1 + ...ReturnInFunctionWithBlockBodyFixFactory.kt | 23 ++++++++++ .../fixes/HighLevelQuickFixTestGenerated.java | 42 +++++++++++++++++++ .../simpleBoolean.kt | 3 +- .../simpleBoolean.kt.after | 3 +- .../subtype.kt | 3 +- .../subtype.kt.after | 3 +- .../typeError2.kt | 2 + .../codeinsight/GenerateK2QuickFixTests.kt | 2 +- 9 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/NoReturnInFunctionWithBlockBodyFixFactory.kt diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt index 419805ed7318..39e64d64f083 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt @@ -133,6 +133,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() { registerFactory(WrongAnnotationTargetWithUseSiteTargetFixFactory.moveReceiverAnnotation) registerFactory(IllegalEscapeFixFactory.convertToUnicodeEscape) registerFactory(IsEnumEntryFixFactory.factory) + registerFactory(NoReturnInFunctionWithBlockBodyFixFactory.addReturnToLastExpression) } private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix { diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/NoReturnInFunctionWithBlockBodyFixFactory.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/NoReturnInFunctionWithBlockBodyFixFactory.kt new file mode 100644 index 000000000000..ef050f2e00e7 --- /dev/null +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/NoReturnInFunctionWithBlockBodyFixFactory.kt @@ -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) + ) + } +} diff --git a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java index cf60b9b64ae7..e754001c5dbf 100644 --- a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java +++ b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java @@ -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"); + } + } diff --git a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/simpleBoolean.kt b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/simpleBoolean.kt index fd3f332d0949..f5298fa42905 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/simpleBoolean.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/simpleBoolean.kt @@ -3,4 +3,5 @@ fun test(): Boolean { true } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/simpleBoolean.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/simpleBoolean.kt.after index 1e2e978e0723..a7246e8c61bc 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/simpleBoolean.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/simpleBoolean.kt.after @@ -3,4 +3,5 @@ fun test(): Boolean { return true } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/subtype.kt b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/subtype.kt index 945cc8f36b6f..636554e9c370 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/subtype.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/subtype.kt @@ -5,4 +5,5 @@ fun foo(): Any { true } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/subtype.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/subtype.kt.after index c85767217d72..6203232ca2d0 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/subtype.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/subtype.kt.after @@ -5,4 +5,5 @@ fun foo(): Any { return true } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddReturnToLastExpressionInFunctionFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/typeError2.kt b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/typeError2.kt index 3a8ba1793810..373224cce890 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/typeError2.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/addReturnToLastExpressionInFunction/typeError2.kt @@ -8,3 +8,5 @@ fun some(): Any { FunctionReference::class } + +/* IGNORE_K2 */ \ No newline at end of file diff --git a/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt b/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt index 2b1ef3634b68..dcdc359c3f9f 100644 --- a/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt +++ b/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt @@ -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)