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
+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