From 4569b85a163a13f43319f1cd77a2814aab5b818d Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 17 Jul 2020 11:48:14 +0900 Subject: [PATCH] MoveVariableDeclarationIntoWhenInspection: report it if initializer is single line even if property is multi line #KT-39467 Fixed --- .../MoveVariableDeclarationIntoWhenInspection.kt | 12 +++++++----- .../multiLinePropertywithSingleLineInitializer.kt | 10 ++++++++++ ...ltiLinePropertywithSingleLineInitializer.kt.after | 8 ++++++++ .../multiLinePropertywithSingleLineInitializer2.kt | 10 ++++++++++ ...tiLinePropertywithSingleLineInitializer2.kt.after | 9 +++++++++ .../inspections/LocalInspectionTestGenerated.java | 10 ++++++++++ 6 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer.kt create mode 100644 idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer.kt.after create mode 100644 idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer2.kt create mode 100644 idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt index d41bfd2d260..0c5fecd9faf 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/MoveVariableDeclarationIntoWhenInspection.kt @@ -32,19 +32,21 @@ class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), Cl whenExpressionVisitor(fun(expression: KtWhenExpression) { val subjectExpression = expression.subjectExpression ?: return val property = expression.findDeclarationNear() ?: return - if (!property.isOneLiner()) return - if (property.initializer?.anyDescendantOfType { + val identifier = property.nameIdentifier ?: return + val initializer = property.initializer ?: return + if (!initializer.isOneLiner()) return + if (initializer.anyDescendantOfType { it is KtThrowExpression || it is KtReturnExpression || it is KtBreakExpression || it is KtContinueExpression - } == true) return + }) return val action = property.action(expression) if (action == Action.NOTHING) return - val identifier = property.nameIdentifier ?: return holder.registerProblem( property, TextRange.from(identifier.startOffsetInParent, identifier.textLength), - action.description, action.createFix(subjectExpression.createSmartPointer()) + action.description, + action.createFix(subjectExpression.createSmartPointer()) ) }) } diff --git a/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer.kt b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer.kt new file mode 100644 index 00000000000..11a85eee04a --- /dev/null +++ b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer.kt @@ -0,0 +1,10 @@ +fun test() = 42 + +fun foo() { + val a = + test() + when (a) { + 1 -> 0 + else -> 24 + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer.kt.after b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer.kt.after new file mode 100644 index 00000000000..ceb0742a830 --- /dev/null +++ b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer.kt.after @@ -0,0 +1,8 @@ +fun test() = 42 + +fun foo() { + when (test()) { + 1 -> 0 + else -> 24 + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer2.kt b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer2.kt new file mode 100644 index 00000000000..52b75572ab5 --- /dev/null +++ b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer2.kt @@ -0,0 +1,10 @@ +fun test() = 42 + +fun foo() { + val a = + test() + when (a) { + 1 -> a + else -> 24 + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer2.kt.after b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer2.kt.after new file mode 100644 index 00000000000..cee0e15f453 --- /dev/null +++ b/idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer2.kt.after @@ -0,0 +1,9 @@ +fun test() = 42 + +fun foo() { + when (val a = + test()) { + 1 -> a + else -> 24 + } +} \ 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 dea52d2fb57..7852bf9f11c 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -6885,6 +6885,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inUnaryAndProperty.kt"); } + @TestMetadata("multiLinePropertywithSingleLineInitializer.kt") + public void testMultiLinePropertywithSingleLineInitializer() throws Exception { + runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer.kt"); + } + + @TestMetadata("multiLinePropertywithSingleLineInitializer2.kt") + public void testMultiLinePropertywithSingleLineInitializer2() throws Exception { + runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/multiLinePropertywithSingleLineInitializer2.kt"); + } + @TestMetadata("notApplicableInBinaryExpression.kt") public void testNotApplicableInBinaryExpression() throws Exception { runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableInBinaryExpression.kt");