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->")
|
||||
appendExpression(block?.unwrapBlockOrParenthesis())
|
||||
appendExpression(if (unwrapBlockOrParenthesis) block?.unwrapBlockOrParenthesis() else block)
|
||||
appendFixedText("\n")
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
||||
appendFixedText("->")
|
||||
|
||||
val currentThenBranch = currentIfExpression.then
|
||||
appendExpression(currentThenBranch?.unwrapBlockOrParenthesis())
|
||||
appendExpression(currentThenBranch)
|
||||
appendFixedText("\n")
|
||||
|
||||
canPassThrough = canPassThrough || canPassThrough(currentThenBranch)
|
||||
@@ -175,7 +175,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
||||
currentIfExpression = syntheticElseBranch
|
||||
toDelete.add(syntheticElseBranch)
|
||||
} else {
|
||||
appendElseBlock(syntheticElseBranch)
|
||||
appendElseBlock(syntheticElseBranch, unwrapBlockOrParenthesis = true)
|
||||
break
|
||||
}
|
||||
} else if (currentElseBranch is KtIfExpression) {
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-6
@@ -2,12 +2,10 @@ fun println(s: String) {}
|
||||
|
||||
fun test(a: Boolean, b: Boolean) {
|
||||
<caret>if (a) {
|
||||
// comment
|
||||
// comment
|
||||
println("a")
|
||||
}
|
||||
else if (b) {
|
||||
println("b")
|
||||
}
|
||||
else {
|
||||
println("none")
|
||||
}
|
||||
else if (b) println("b")
|
||||
else println("none")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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<String>)
|
||||
"" -> {
|
||||
println(a)
|
||||
}
|
||||
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 {
|
||||
when (s) {
|
||||
is Int -> foo()
|
||||
else -> return -1
|
||||
is Int -> {
|
||||
foo()
|
||||
}
|
||||
else -> {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
// code below will be lost!
|
||||
|
||||
@@ -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
|
||||
|
||||
+9
-3
@@ -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
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
fun test(n: Int): String {
|
||||
return <caret>when (n) {
|
||||
0 -> "zero"
|
||||
1 -> "one"
|
||||
2 -> "two"
|
||||
else -> "unknown"
|
||||
0 -> {
|
||||
"zero"
|
||||
}
|
||||
1 -> {
|
||||
"one"
|
||||
}
|
||||
2 -> {
|
||||
"two"
|
||||
}
|
||||
else -> {
|
||||
"unknown"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,11 @@ fun println(s: String) {}
|
||||
|
||||
fun foo() {
|
||||
when {
|
||||
@Ann b -> println("!")
|
||||
else -> println("")
|
||||
@Ann b -> {
|
||||
println("!")
|
||||
}
|
||||
else -> {
|
||||
println("")
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-4
@@ -3,11 +3,17 @@
|
||||
fun testIf(xs: List<Any>) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -9,6 +9,8 @@ fun testIf(x: Any) {
|
||||
break // do not change
|
||||
}
|
||||
}
|
||||
else -> println(x)
|
||||
else -> {
|
||||
println(x)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-2
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user