Extract Function: Merge jumps with default exits if all exit points are equivalent

#KT-6598 Fixed
This commit is contained in:
Alexey Sedunov
2015-01-12 17:31:39 +03:00
parent 9278dee1a4
commit 8d40ca1a74
24 changed files with 324 additions and 13 deletions
@@ -0,0 +1,14 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
for (n in 1..b) {
<selection>if (a > 0) throw Exception("")
if (a + b > 0) break
println(a - b)
return</selection>
}
}
@@ -0,0 +1,19 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
for (n in 1..b) {
__dummyTestFun__(a, b)
break
}
}
private fun __dummyTestFun__(a: Int, b: Int) {
if (a > 0) throw Exception("")
if (a + b > 0) return
println(a - b)
return
}
@@ -0,0 +1,15 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
@loop1 for (p in 1..b) {
@loop2 for (n in 1..b) {
<selection>if (a > 0) throw Exception("")
if (a + b > 0) break@loop2
if (a - b > 0) continue@loop1</selection>
}
}
}
@@ -0,0 +1,20 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
@loop1 for (p in 1..b) {
@loop2 for (n in 1..b) {
if (b(a, b)) break@loop2
}
}
}
private fun b(a: Int, b: Int): Boolean {
if (a > 0) throw Exception("")
if (a + b > 0) return true
if (a - b > 0) return true
return false
}
@@ -0,0 +1,10 @@
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
for (n in 1..b) {
<selection>if (a > 0) throw Exception("")
if (a + b > 0) continue
println(a - b)
return</selection>
}
}
@@ -0,0 +1 @@
Selected code fragment has multiple exit points
@@ -0,0 +1,13 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
for (n in 1..b) {
<selection>if (a > 0) throw Exception("")
if (a + b > 0) break
println(a - b)</selection>
}
}
@@ -0,0 +1,18 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
for (n in 1..b) {
if (b(a, b)) break
}
}
private fun b(a: Int, b: Int): Boolean {
if (a > 0) throw Exception("")
if (a + b > 0) return true
println(a - b)
return false
}
@@ -0,0 +1,13 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
for (n in 1..b) {
<selection>if (a > 0) throw Exception("")
if (a + b > 0) continue
println(a - b)</selection>
}
}
@@ -0,0 +1,17 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
for (n in 1..b) {
__dummyTestFun__(a, b)
}
}
private fun __dummyTestFun__(a: Int, b: Int) {
if (a > 0) throw Exception("")
if (a + b > 0) return
println(a - b)
}
@@ -0,0 +1,12 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
<selection>if (a > 0) throw Exception("")
if (b + a > 0) return
println(a - b)</selection>
}
@@ -0,0 +1,16 @@
// PARAM_TYPES: kotlin.Int
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: value-parameter val a: kotlin.Int defined in foo
// PARAM_DESCRIPTOR: val b: kotlin.Int defined in foo
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
__dummyTestFun__(a, b)
}
private fun __dummyTestFun__(a: Int, b: Int) {
if (a > 0) throw Exception("")
if (b + a > 0) return
println(a - b)
}
@@ -0,0 +1,17 @@
// WITH_RUNTIME
// PARAM_TYPES: kotlin.Int, Comparable<Int>
// PARAM_DESCRIPTOR: value-parameter val i: kotlin.Int defined in example
fun example(i: Int) {
when (i) {
1 -> {
<selection>if (i > 5) {
println("true")
}
else {
println("false")
return
}
println("!!")</selection>
}
}
}
@@ -0,0 +1,20 @@
// WITH_RUNTIME
// PARAM_TYPES: kotlin.Int, Comparable<Int>
// PARAM_DESCRIPTOR: value-parameter val i: kotlin.Int defined in example
fun example(i: Int) {
when (i) {
1 -> {
__dummyTestFun__(i)
}
}
}
private fun __dummyTestFun__(i: Int) {
if (i > 5) {
println("true")
} else {
println("false")
return
}
println("!!")
}
@@ -0,0 +1,12 @@
// SIBLING:
fun foo(a: Int) {
val b: Int = 1
@loop1 for (p in 1..b) {
@loop2 for (n in 1..b) {
<selection>if (a > 0) throw Exception("")
if (a + b > 0) break@loop1
println(a - b)
break@loop2</selection>
}
}
}
@@ -0,0 +1 @@
Selected code fragment has multiple exit points
@@ -9,5 +9,6 @@ fun foo(a: Int): Int {
<selection>if (a > 0) throw Exception("")
if (a + b > 0) continue
println(a - b)</selection>
println(a + b)
}
}
@@ -7,6 +7,7 @@ fun foo(a: Int): Int {
val b: Int = 1
for (n in 1..b) {
if (b(a, b)) continue
println(a + b)
}
}
@@ -9,4 +9,5 @@ fun foo(a: Int) {
<selection>if (a > 0) throw Exception("")
if (b + a > 0) return
println(a - b)</selection>
println(a + b)
}
@@ -7,6 +7,7 @@ fun foo(a: Int) {
val b: Int = 1
if (b(a, b)) return
println(a + b)
}
private fun b(a: Int, b: Int): Boolean {