From 80b09b9a08a0d5b6b6bd349ad6b64e4a1db6734c Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Tue, 23 Jul 2019 18:57:36 +0900 Subject: [PATCH] Replace 'if' with 'when': don't remove branch brances #KT-32650 Fixed --- .../intentions/IfToWhenIntention.kt | 8 +++--- .../cascadeIf/insideOtherIf.kt.after | 12 ++++++--- .../inspectionsLocal/cascadeIf/normal.kt | 10 +++---- .../cascadeIf/normal.kt.after | 6 ++++- .../cascadeIf/withAnnotation.kt.after | 14 +++++++--- .../ifToWhen/ifElseSwallowTail.kt.after | 8 ++++-- .../ifWhen/ifToWhen/multipleIfFake.kt.after | 4 ++- .../ifToWhen/multipleIfWithReturns.kt.after | 12 ++++++--- .../ifWhen/ifToWhen/onElseIf2.kt.after | 16 +++++++++--- .../ifWhen/ifToWhen/withAnnotation.kt.after | 8 ++++-- .../ifWhen/ifToWhen/withInternalLoop.kt.after | 14 +++++++--- .../ifToWhen/withInternalLoopOnly.kt.after | 4 ++- .../ifWhen/ifToWhen/withLoopDeep.kt.after | 26 +++++++++++-------- .../ifToWhen/withLoopDeepAndComments.kt.after | 8 ++++-- .../ifWhen/ifToWhen/withLoopThen.kt.after | 8 ++++-- 15 files changed, 108 insertions(+), 50 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt index e478d150208..1934a2f1d6a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt @@ -115,9 +115,9 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres } } - private fun BuilderByPattern<*>.appendElseBlock(block: KtExpression?) { + private fun BuilderByPattern<*>.appendElseBlock(block: KtExpression?, unwrapBlockOrParenthesis: Boolean = false) { appendFixedText("else->") - appendExpression(block?.unwrapBlockOrParenthesis()) + appendExpression(if (unwrapBlockOrParenthesis) block?.unwrapBlockOrParenthesis() else block) appendFixedText("\n") } @@ -160,7 +160,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres appendFixedText("->") val currentThenBranch = currentIfExpression.then - appendExpression(currentThenBranch?.unwrapBlockOrParenthesis()) + appendExpression(currentThenBranch) appendFixedText("\n") canPassThrough = canPassThrough || canPassThrough(currentThenBranch) @@ -175,7 +175,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres currentIfExpression = syntheticElseBranch toDelete.add(syntheticElseBranch) } else { - appendElseBlock(syntheticElseBranch) + appendElseBlock(syntheticElseBranch, unwrapBlockOrParenthesis = true) break } } else if (currentElseBranch is KtIfExpression) { diff --git a/idea/testData/inspectionsLocal/cascadeIf/insideOtherIf.kt.after b/idea/testData/inspectionsLocal/cascadeIf/insideOtherIf.kt.after index 3db0953e3e8..edf8352dc2c 100644 --- a/idea/testData/inspectionsLocal/cascadeIf/insideOtherIf.kt.after +++ b/idea/testData/inspectionsLocal/cascadeIf/insideOtherIf.kt.after @@ -3,9 +3,15 @@ fun println(s: String) {} fun test(x: Double, a: Boolean, b: Boolean) { if (x > 0.0) { when { - a -> println("a") - b -> println("b") - else -> println("none") + a -> { + println("a") + } + b -> { + println("b") + } + else -> { + println("none") + } } } } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/cascadeIf/normal.kt b/idea/testData/inspectionsLocal/cascadeIf/normal.kt index 9c25978881b..335cd50aaaf 100644 --- a/idea/testData/inspectionsLocal/cascadeIf/normal.kt +++ b/idea/testData/inspectionsLocal/cascadeIf/normal.kt @@ -2,12 +2,10 @@ fun println(s: String) {} fun test(a: Boolean, b: Boolean) { if (a) { + // comment + // comment println("a") } - else if (b) { - println("b") - } - else { - println("none") - } + else if (b) println("b") + else println("none") } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/cascadeIf/normal.kt.after b/idea/testData/inspectionsLocal/cascadeIf/normal.kt.after index 0bf6de068aa..4116d692ded 100644 --- a/idea/testData/inspectionsLocal/cascadeIf/normal.kt.after +++ b/idea/testData/inspectionsLocal/cascadeIf/normal.kt.after @@ -2,7 +2,11 @@ fun println(s: String) {} fun test(a: Boolean, b: Boolean) { when { - a -> println("a") + a -> { + // comment + // comment + println("a") + } b -> println("b") else -> println("none") } diff --git a/idea/testData/inspectionsLocal/cascadeIf/withAnnotation.kt.after b/idea/testData/inspectionsLocal/cascadeIf/withAnnotation.kt.after index f74ecfacd6d..e7579b38469 100644 --- a/idea/testData/inspectionsLocal/cascadeIf/withAnnotation.kt.after +++ b/idea/testData/inspectionsLocal/cascadeIf/withAnnotation.kt.after @@ -1,9 +1,15 @@ // WITH_RUNTIME fun foo(a: Any) { when (a) { - "" -> println(a) - is String -> println(a) - is List<*> -> @Suppress("UNCHECKED_CAST") - println(a as List) + "" -> { + println(a) + } + is String -> { + println(a) + } + is List<*> -> { + @Suppress("UNCHECKED_CAST") + println(a as List) + } } } \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/ifElseSwallowTail.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/ifElseSwallowTail.kt.after index 93af0381c26..b7527a5dd28 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/ifElseSwallowTail.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/ifElseSwallowTail.kt.after @@ -1,7 +1,11 @@ fun toInt(s: Number): Int { when (s) { - is Int -> foo() - else -> return -1 + is Int -> { + foo() + } + else -> { + return -1 + } } // code below will be lost! diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/multipleIfFake.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/multipleIfFake.kt.after index fa0f8b5d671..23428c0213b 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/multipleIfFake.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/multipleIfFake.kt.after @@ -1,6 +1,8 @@ fun bar(arg: Int): Int { when { - arg < 0 -> if (arg == -3) return 0 + arg < 0 -> { + if (arg == -3) return 0 + } } if (arg > 0) { return 1 diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/multipleIfWithReturns.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/multipleIfWithReturns.kt.after index cc10f914c1f..29c2545dd02 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/multipleIfWithReturns.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/multipleIfWithReturns.kt.after @@ -1,12 +1,18 @@ fun foo(arg: Any?): Int { // 1 when (arg) { - is Int -> return arg // 2 + is Int -> { + return arg // 2 + } // 3 - is String -> return 42 // 4 + is String -> { + return 42 // 4 + } // 5 - null -> // 6 + null -> { + // 6 return 0 + } // 7 // 8 else -> return -1 diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/onElseIf2.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/onElseIf2.kt.after index dba5a8e64d6..e7ecc919b1b 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/onElseIf2.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/onElseIf2.kt.after @@ -1,8 +1,16 @@ fun test(n: Int): String { return when (n) { - 0 -> "zero" - 1 -> "one" - 2 -> "two" - else -> "unknown" + 0 -> { + "zero" + } + 1 -> { + "one" + } + 2 -> { + "two" + } + else -> { + "unknown" + } } } \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/withAnnotation.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/withAnnotation.kt.after index cbbbc9bfe1c..2da9db57648 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/withAnnotation.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/withAnnotation.kt.after @@ -8,7 +8,11 @@ fun println(s: String) {} fun foo() { when { - @Ann b -> println("!") - else -> println("") + @Ann b -> { + println("!") + } + else -> { + println("") + } } } \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/withInternalLoop.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/withInternalLoop.kt.after index 495e2e8539e..9304485cb13 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/withInternalLoop.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/withInternalLoop.kt.after @@ -3,11 +3,17 @@ fun testIf(xs: List) { loop@ for (x in xs) { when (x) { - is String -> for (c in x) { - continue // do not change + is String -> { + for (c in x) { + continue // do not change + } + } + is Int -> { + break@loop + } + else -> { + println(x) } - is Int -> break@loop - else -> println(x) } } } \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/withInternalLoopOnly.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/withInternalLoopOnly.kt.after index 487da061a4b..dbe61e802a9 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/withInternalLoopOnly.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/withInternalLoopOnly.kt.after @@ -9,6 +9,8 @@ fun testIf(x: Any) { break // do not change } } - else -> println(x) + else -> { + println(x) + } } } \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopDeep.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopDeep.kt.after index 7c4a7ef8e0c..ff29ef1c165 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopDeep.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopDeep.kt.after @@ -2,18 +2,22 @@ fun test() { loop@ while (true) { loop1@ for (i in -10..10) { when { - i < 0 -> if (i < -5) { - break@loop1 - } else { - continue@loop + i < 0 -> { + if (i < -5) { + break@loop1 + } else { + continue@loop + } } - else -> if (i == 0) { - i.hashCode() - break@loop1 - } else if (i > 5) { - i.hashCode() - } else { - continue@loop1 + else -> { + if (i == 0) { + i.hashCode() + break@loop1 + } else if (i > 5) { + i.hashCode() + } else { + continue@loop1 + } } } } diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopDeepAndComments.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopDeepAndComments.kt.after index 8e6b9a4cb06..5b0f25163eb 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopDeepAndComments.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopDeepAndComments.kt.after @@ -20,10 +20,14 @@ fun test() { // Comment 7 break@loop1 } - i > 5 -> // Comment 8 + i > 5 -> { + // Comment 8 i.hashCode() - else -> // Comment 9 + } + else -> { + // Comment 9 continue@loop1 + } } } } diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopThen.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopThen.kt.after index 35434fcbf53..a5fc5b42ab9 100644 --- a/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopThen.kt.after +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/withLoopThen.kt.after @@ -3,10 +3,14 @@ fun test() { loop@ for (i in -2..2) { // Some comment when { - i < 0 -> // Very important comment + i < 0 -> { + // Very important comment break@loop - else -> // More comments + } + else -> { + // More comments i.hashCode() + } } } } \ No newline at end of file