KT-14336 for to stdlib to takeWhile: propose conversion into takeWhile if break is located in else branch

#KT-14336 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-10-17 19:14:19 +03:00
parent 5c8db294df
commit 18f10860df
9 changed files with 125 additions and 8 deletions
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
// IS_APPLICABLE_2: false
fun foo(list: List<String>): String? {
<caret>for (s in list) {
if (s.isEmpty()) continue
else return s
}
return null
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
// IS_APPLICABLE_2: false
fun foo(list: List<String>): String? {
return list.firstOrNull { !it.isEmpty() }
}
@@ -0,0 +1,13 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'takeWhile{}'"
// IS_APPLICABLE_2: false
fun foo(n: Int, list: List<Int>): List<Int>{
val result = mutableListOf<Int>()
<caret>for (i in list) {
if (i >= n)
result.add(i)
else
break
}
return result
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'takeWhile{}'"
// IS_APPLICABLE_2: false
fun foo(n: Int, list: List<Int>): List<Int>{
val result = list.takeWhile { it >= n }
return result
}
@@ -0,0 +1,13 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'takeWhile{}'"
// IS_APPLICABLE_2: false
fun foo(n: Int, list: List<Int>): List<Int>{
val result = mutableListOf<Int>()
<caret>for (i in list) {
if (i < n)
break
else
result.add(i)
}
return result
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'takeWhile{}'"
// IS_APPLICABLE_2: false
fun foo(n: Int, list: List<Int>): List<Int>{
val result = list.takeWhile { it >= n }
return result
}