From 181dd432ca1f5bf9751e305b57627184085e585c Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Wed, 29 Jan 2020 15:07:27 +0700 Subject: [PATCH] RestrictReturnStatementTargetMigrationInspection: add quickfix #KT-36262 Fixed --- .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 ++ .../idea/quickfix/RemoveReturnLabelFix.kt | 3 ++- .../.inspection | 1 + .../simple.kt | 8 +++++++ .../simple2.kt | 7 ++++++ .../simple2.kt.after | 7 ++++++ .../LocalInspectionTestGenerated.java | 23 +++++++++++++++++++ 7 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/.inspection create mode 100644 idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple.kt create mode 100644 idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple2.kt create mode 100644 idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index d978daaa6fd..c388dd73d46 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -627,6 +627,8 @@ class QuickFixRegistrar : QuickFixContributor { CONSTRUCTOR_IN_OBJECT.registerFactory(ChangeObjectToClassFix) REDUNDANT_LABEL_WARNING.registerFactory(RemoveRedundantLabelFix) + NOT_A_FUNCTION_LABEL.registerFactory(RemoveReturnLabelFix) + NOT_A_FUNCTION_LABEL_WARNING.registerFactory(RemoveReturnLabelFix) ACCIDENTAL_OVERRIDE.registerFactory(MakePrivateAndOverrideMemberFix.AccidentalOverrideFactory) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveReturnLabelFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveReturnLabelFix.kt index 95f72b060e4..ad95fa1519c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveReturnLabelFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveReturnLabelFix.kt @@ -23,7 +23,8 @@ class RemoveReturnLabelFix(element: KtReturnExpression) : KotlinQuickFixAction? { val returnExpression = diagnostic.psiElement as? KtReturnExpression ?: return null - return RemoveReturnLabelFix(returnExpression) + return if (returnExpression.labeledExpression == null) null + else RemoveReturnLabelFix(returnExpression) } } } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/.inspection b/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/.inspection new file mode 100644 index 00000000000..57b0556676c --- /dev/null +++ b/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/.inspection @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.inspections.migration.RestrictReturnStatementTargetMigrationInspection diff --git a/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple.kt b/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple.kt new file mode 100644 index 00000000000..ae2eff083d5 --- /dev/null +++ b/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple.kt @@ -0,0 +1,8 @@ +// LANGUAGE_VERSION: 1.4 +// PROBLEM: none +// ERROR: Target label does not denote a function + +fun testValLabelInReturn() { + L@ val fn = { return@L } + fn() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple2.kt b/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple2.kt new file mode 100644 index 00000000000..e0f30323f68 --- /dev/null +++ b/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple2.kt @@ -0,0 +1,7 @@ +// LANGUAGE_VERSION: 1.4 +// SKIP_ERRORS_AFTER + +fun testValLabelInReturn() { + L@ val fn = { return@L } + fn() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple2.kt.after b/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple2.kt.after new file mode 100644 index 00000000000..8d024d01e4f --- /dev/null +++ b/idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple2.kt.after @@ -0,0 +1,7 @@ +// LANGUAGE_VERSION: 1.4 +// SKIP_ERRORS_AFTER + +fun testValLabelInReturn() { + L@ val fn = { return } + fn() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 5585db3f7e1..b1caf88262b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -10950,6 +10950,29 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { } } + @TestMetadata("idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RestrictReturnStatementTargetMigration extends AbstractLocalInspectionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInRestrictReturnStatementTargetMigration() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + runTest("idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple.kt"); + } + + @TestMetadata("simple2.kt") + public void testSimple2() throws Exception { + runTest("idea/testData/inspectionsLocal/restrictReturnStatementTargetMigration/simple2.kt"); + } + } + @TestMetadata("idea/testData/inspectionsLocal/safeCastWithReturn") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)