From 998adfb098526bd0dbbd9811132dae21df294625 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Tue, 24 Sep 2019 09:29:00 +0900 Subject: [PATCH] Move variable declaration into `when`: don't report when property has 'break' or 'continue' #KT-31999 --- .../MoveVariableDeclarationIntoWhenInspection.kt | 8 +++----- .../moveVariableDeclarationIntoWhen/hasBreak.kt | 12 ++++++++++++ .../moveVariableDeclarationIntoWhen/hasContinue.kt | 12 ++++++++++++ .../inspections/LocalInspectionTestGenerated.java | 10 ++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasBreak.kt create mode 100644 idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasContinue.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt index c308b3eaf05..8838e5afe74 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt @@ -32,7 +32,9 @@ class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), Cl val subjectExpression = expression.subjectExpression ?: return val property = expression.findDeclarationNear() ?: return if (!property.isOneLiner()) return - if (property.hasReturnOrThrowExpression()) return + if (property.initializer?.anyDescendantOfType { + it is KtThrowExpression || it is KtReturnExpression || it is KtBreakExpression || it is KtContinueExpression + } == true) return val action = property.action(expression) if (action == Action.NOTHING) return @@ -77,10 +79,6 @@ private fun KtWhenExpression.findDeclarationNear(): KtProperty? { return previousProperty.takeIf { !it.isVar && it.hasInitializer() && it.nameIdentifier?.text == subjectExpression?.text } } -private fun KtProperty.hasReturnOrThrowExpression(): Boolean { - return this.initializer?.anyDescendantOfType { it is KtReturnExpression || it is KtThrowExpression } == true -} - private tailrec fun KtExpression.previousPropertyFromParent(): KtProperty? { val parentExpression = parent as? KtExpression ?: return null if (this != when (parentExpression) { diff --git a/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasBreak.kt b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasBreak.kt new file mode 100644 index 00000000000..4546a0dd00b --- /dev/null +++ b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasBreak.kt @@ -0,0 +1,12 @@ +// PROBLEM: none +fun test() { + for (i in 1..10) { + val some = foo(i) ?: break + when (some) { + "some" -> some + else -> "" + } + } +} + +fun foo(i: Int): String? = "" \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasContinue.kt b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasContinue.kt new file mode 100644 index 00000000000..17e98905703 --- /dev/null +++ b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasContinue.kt @@ -0,0 +1,12 @@ +// PROBLEM: none +fun test() { + for (i in 1..10) { + val some = foo(i) ?: continue + when (some) { + "some" -> some + else -> "" + } + } +} + +fun foo(i: Int): String? = "" \ 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 21d8342dc3e..fd2aab99467 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -6093,6 +6093,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } + @TestMetadata("hasBreak.kt") + public void testHasBreak() throws Exception { + runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasBreak.kt"); + } + + @TestMetadata("hasContinue.kt") + public void testHasContinue() throws Exception { + runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasContinue.kt"); + } + @TestMetadata("hasReturn.kt") public void testHasReturn() throws Exception { runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasReturn.kt");