Prohibiting incorrect transformations of loops with expression-embedded break or continue + allowed "?: continue" pattern for mapNotNull
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(list: List<String?>, target: MutableList<String>) {
|
||||
<caret>for (s in list) {
|
||||
val length = s?.length ?: break
|
||||
target.add(length.toString())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(list: List<String>, target: MutableList<String>) {
|
||||
<caret>for (s in list) {
|
||||
val length = if (s.isNotEmpty()) s.length else break
|
||||
target.add(length.toString())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(list: List<String>, target: MutableList<String>) {
|
||||
<caret>for (s in list) {
|
||||
val length = if (s.isNotEmpty()) s.length else continue
|
||||
target.add(length.toString())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'mapIndexedNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String?>, target: MutableList<String>) {
|
||||
<caret>for ((index, s) in list.withIndex()) {
|
||||
val length = s?.substring(index)?.length ?: continue
|
||||
target.add(length.toString())
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'mapIndexedNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String?>, target: MutableList<String>) {
|
||||
<caret>list
|
||||
.mapIndexedNotNull { index, s -> s?.substring(index)?.length }
|
||||
.mapTo(target) { it.toString() }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'mapNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String?>, target: MutableList<String>) {
|
||||
<caret>for (s in list) {
|
||||
val length = s?.length ?: continue
|
||||
target.add(length.toString())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'mapNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String?>, target: MutableList<String>) {
|
||||
<caret>list
|
||||
.mapNotNull { it?.length }
|
||||
.mapTo(target) { it.toString() }
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'flatMap{}.mapNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String>, target: MutableList<String>) {
|
||||
<caret>for (s in list) {
|
||||
for (i in s.indices) {
|
||||
val v = bar(i) ?: continue
|
||||
target.add(v.substring(1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun bar(p: Int): String? = null
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'flatMap{}.mapNotNull{}.mapTo(){}'"
|
||||
fun foo(list: List<String>, target: MutableList<String>) {
|
||||
<caret>list
|
||||
.flatMap { it.indices }
|
||||
.mapNotNull { bar(it) }
|
||||
.mapTo(target) { it.substring(1) }
|
||||
}
|
||||
|
||||
fun bar(p: Int): String? = null
|
||||
@@ -0,0 +1,13 @@
|
||||
// WITH_RUNTIME
|
||||
// IS_APPLICABLE: false
|
||||
fun foo(list: List<String>, target: MutableList<String>) {
|
||||
Loop@
|
||||
<caret>for (s in list) {
|
||||
for (i in s.indices) {
|
||||
val v = bar(i) ?: continue@Loop
|
||||
target.add(v.substring(1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun bar(p: Int): String? = null
|
||||
Reference in New Issue
Block a user