Replace 'if' with 'when': don't remove branch brances
#KT-32650 Fixed
This commit is contained in:
committed by
Dmitry Gridin
parent
f3b42507d9
commit
80b09b9a08
+4
-4
@@ -115,9 +115,9 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun BuilderByPattern<*>.appendElseBlock(block: KtExpression?) {
|
private fun BuilderByPattern<*>.appendElseBlock(block: KtExpression?, unwrapBlockOrParenthesis: Boolean = false) {
|
||||||
appendFixedText("else->")
|
appendFixedText("else->")
|
||||||
appendExpression(block?.unwrapBlockOrParenthesis())
|
appendExpression(if (unwrapBlockOrParenthesis) block?.unwrapBlockOrParenthesis() else block)
|
||||||
appendFixedText("\n")
|
appendFixedText("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,7 +160,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
|||||||
appendFixedText("->")
|
appendFixedText("->")
|
||||||
|
|
||||||
val currentThenBranch = currentIfExpression.then
|
val currentThenBranch = currentIfExpression.then
|
||||||
appendExpression(currentThenBranch?.unwrapBlockOrParenthesis())
|
appendExpression(currentThenBranch)
|
||||||
appendFixedText("\n")
|
appendFixedText("\n")
|
||||||
|
|
||||||
canPassThrough = canPassThrough || canPassThrough(currentThenBranch)
|
canPassThrough = canPassThrough || canPassThrough(currentThenBranch)
|
||||||
@@ -175,7 +175,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
|||||||
currentIfExpression = syntheticElseBranch
|
currentIfExpression = syntheticElseBranch
|
||||||
toDelete.add(syntheticElseBranch)
|
toDelete.add(syntheticElseBranch)
|
||||||
} else {
|
} else {
|
||||||
appendElseBlock(syntheticElseBranch)
|
appendElseBlock(syntheticElseBranch, unwrapBlockOrParenthesis = true)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else if (currentElseBranch is KtIfExpression) {
|
} else if (currentElseBranch is KtIfExpression) {
|
||||||
|
|||||||
@@ -3,9 +3,15 @@ fun println(s: String) {}
|
|||||||
fun test(x: Double, a: Boolean, b: Boolean) {
|
fun test(x: Double, a: Boolean, b: Boolean) {
|
||||||
if (x > 0.0) {
|
if (x > 0.0) {
|
||||||
when {
|
when {
|
||||||
a -> println("a")
|
a -> {
|
||||||
b -> println("b")
|
println("a")
|
||||||
else -> println("none")
|
}
|
||||||
|
b -> {
|
||||||
|
println("b")
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
println("none")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+4
-6
@@ -2,12 +2,10 @@ fun println(s: String) {}
|
|||||||
|
|
||||||
fun test(a: Boolean, b: Boolean) {
|
fun test(a: Boolean, b: Boolean) {
|
||||||
<caret>if (a) {
|
<caret>if (a) {
|
||||||
|
// comment
|
||||||
|
// comment
|
||||||
println("a")
|
println("a")
|
||||||
}
|
}
|
||||||
else if (b) {
|
else if (b) println("b")
|
||||||
println("b")
|
else println("none")
|
||||||
}
|
|
||||||
else {
|
|
||||||
println("none")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,11 @@ fun println(s: String) {}
|
|||||||
|
|
||||||
fun test(a: Boolean, b: Boolean) {
|
fun test(a: Boolean, b: Boolean) {
|
||||||
when {
|
when {
|
||||||
a -> println("a")
|
a -> {
|
||||||
|
// comment
|
||||||
|
// comment
|
||||||
|
println("a")
|
||||||
|
}
|
||||||
b -> println("b")
|
b -> println("b")
|
||||||
else -> println("none")
|
else -> println("none")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
fun foo(a: Any) {
|
fun foo(a: Any) {
|
||||||
when (a) {
|
when (a) {
|
||||||
"" -> println(a)
|
"" -> {
|
||||||
is String -> println(a)
|
println(a)
|
||||||
is List<*> -> @Suppress("UNCHECKED_CAST")
|
}
|
||||||
println(a as List<String>)
|
is String -> {
|
||||||
|
println(a)
|
||||||
|
}
|
||||||
|
is List<*> -> {
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
println(a as List<String>)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+6
-2
@@ -1,7 +1,11 @@
|
|||||||
fun toInt(s: Number): Int {
|
fun toInt(s: Number): Int {
|
||||||
when (s) {
|
when (s) {
|
||||||
is Int -> foo()
|
is Int -> {
|
||||||
else -> return -1
|
foo()
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// code below will be lost!
|
// code below will be lost!
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
fun bar(arg: Int): Int {
|
fun bar(arg: Int): Int {
|
||||||
when {
|
when {
|
||||||
arg < 0 -> if (arg == -3) return 0
|
arg < 0 -> {
|
||||||
|
if (arg == -3) return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (arg > 0) {
|
if (arg > 0) {
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
+9
-3
@@ -1,12 +1,18 @@
|
|||||||
fun foo(arg: Any?): Int {
|
fun foo(arg: Any?): Int {
|
||||||
// 1
|
// 1
|
||||||
when (arg) {
|
when (arg) {
|
||||||
is Int -> return arg // 2
|
is Int -> {
|
||||||
|
return arg // 2
|
||||||
|
}
|
||||||
// 3
|
// 3
|
||||||
is String -> return 42 // 4
|
is String -> {
|
||||||
|
return 42 // 4
|
||||||
|
}
|
||||||
// 5
|
// 5
|
||||||
null -> // 6
|
null -> {
|
||||||
|
// 6
|
||||||
return 0
|
return 0
|
||||||
|
}
|
||||||
// 7
|
// 7
|
||||||
// 8
|
// 8
|
||||||
else -> return -1
|
else -> return -1
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
fun test(n: Int): String {
|
fun test(n: Int): String {
|
||||||
return <caret>when (n) {
|
return <caret>when (n) {
|
||||||
0 -> "zero"
|
0 -> {
|
||||||
1 -> "one"
|
"zero"
|
||||||
2 -> "two"
|
}
|
||||||
else -> "unknown"
|
1 -> {
|
||||||
|
"one"
|
||||||
|
}
|
||||||
|
2 -> {
|
||||||
|
"two"
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
"unknown"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,11 @@ fun println(s: String) {}
|
|||||||
|
|
||||||
fun foo() {
|
fun foo() {
|
||||||
when {
|
when {
|
||||||
@Ann b -> println("!")
|
@Ann b -> {
|
||||||
else -> println("")
|
println("!")
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
println("")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+10
-4
@@ -3,11 +3,17 @@
|
|||||||
fun testIf(xs: List<Any>) {
|
fun testIf(xs: List<Any>) {
|
||||||
loop@ for (x in xs) {
|
loop@ for (x in xs) {
|
||||||
when (x) {
|
when (x) {
|
||||||
is String -> for (c in x) {
|
is String -> {
|
||||||
continue // do not change
|
for (c in x) {
|
||||||
|
continue // do not change
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is Int -> {
|
||||||
|
break@loop
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
println(x)
|
||||||
}
|
}
|
||||||
is Int -> break@loop
|
|
||||||
else -> println(x)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-1
@@ -9,6 +9,8 @@ fun testIf(x: Any) {
|
|||||||
break // do not change
|
break // do not change
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> println(x)
|
else -> {
|
||||||
|
println(x)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,18 +2,22 @@ fun test() {
|
|||||||
loop@ while (true) {
|
loop@ while (true) {
|
||||||
loop1@ for (i in -10..10) {
|
loop1@ for (i in -10..10) {
|
||||||
when {
|
when {
|
||||||
i < 0 -> if (i < -5) {
|
i < 0 -> {
|
||||||
break@loop1
|
if (i < -5) {
|
||||||
} else {
|
break@loop1
|
||||||
continue@loop
|
} else {
|
||||||
|
continue@loop
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else -> if (i == 0) {
|
else -> {
|
||||||
i.hashCode()
|
if (i == 0) {
|
||||||
break@loop1
|
i.hashCode()
|
||||||
} else if (i > 5) {
|
break@loop1
|
||||||
i.hashCode()
|
} else if (i > 5) {
|
||||||
} else {
|
i.hashCode()
|
||||||
continue@loop1
|
} else {
|
||||||
|
continue@loop1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-2
@@ -20,10 +20,14 @@ fun test() {
|
|||||||
// Comment 7
|
// Comment 7
|
||||||
break@loop1
|
break@loop1
|
||||||
}
|
}
|
||||||
i > 5 -> // Comment 8
|
i > 5 -> {
|
||||||
|
// Comment 8
|
||||||
i.hashCode()
|
i.hashCode()
|
||||||
else -> // Comment 9
|
}
|
||||||
|
else -> {
|
||||||
|
// Comment 9
|
||||||
continue@loop1
|
continue@loop1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,14 @@ fun test() {
|
|||||||
loop@ for (i in -2..2) {
|
loop@ for (i in -2..2) {
|
||||||
// Some comment
|
// Some comment
|
||||||
when {
|
when {
|
||||||
i < 0 -> // Very important comment
|
i < 0 -> {
|
||||||
|
// Very important comment
|
||||||
break@loop
|
break@loop
|
||||||
else -> // More comments
|
}
|
||||||
|
else -> {
|
||||||
|
// More comments
|
||||||
i.hashCode()
|
i.hashCode()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user