Split or glue together checks for not null and is instance + more intelligent use of filter vs filterTo

#KT-14284 Fixed
 #KT-14286 Fixed
 #KT-14287 Fixed
 #KT-14303 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-10-17 16:27:01 +03:00
parent 53220b980e
commit 89099b93b1
41 changed files with 548 additions and 126 deletions
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexedTo(){}'"
// IS_APPLICABLE_2: false
fun foo(list: List<Any>, out: MutableList<Any>){
<caret>for ((i, any) in list.withIndex()) {
if (any is String && i % 2 == 0)
out.add(any)
}
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexedTo(){}'"
// IS_APPLICABLE_2: false
fun foo(list: List<Any>, out: MutableList<Any>){
list.filterIndexedTo(out) { i, any -> any is String && i % 2 == 0 }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'"
fun foo(list: List<String?>){
<caret>for (l in list) {
if (l != null && l.startsWith("IMG:"))
println(l)
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'"
fun foo(list: List<String?>){
list
.filter { it != null && it.startsWith("IMG:") }
.forEach { println(it) }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'"
fun foo(list: List<String?>){
list
.asSequence()
.filter { it != null && it.startsWith("IMG:") }
.forEach { println(it) }
}
@@ -1,6 +1,6 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '...flatMap{}.filterNot{}.mapTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence()...flatMap{}.filterNot{}.mapTo(){}'"
// INTENTION_TEXT: "Replace with '...flatMap{}.filter{}.mapTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence()...flatMap{}.filter{}.mapTo(){}'"
fun foo(list: List<String>, target: MutableCollection<String>) {
var i = 0
<caret>for (s in list) {
@@ -1,10 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '...flatMap{}.filterNot{}.mapTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence()...flatMap{}.filterNot{}.mapTo(){}'"
// INTENTION_TEXT: "Replace with '...flatMap{}.filter{}.mapTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence()...flatMap{}.filter{}.mapTo(){}'"
fun foo(list: List<String>, target: MutableCollection<String>) {
<caret>list
.filterIndexed { i, s -> i % 10 != 0 }
.flatMap { it.indices }
.filterNot { it == 10 }
.filter { it != 10 }
.mapTo(target) { it.toString() }
}
@@ -1,11 +1,11 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '...flatMap{}.filterNot{}.mapTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence()...flatMap{}.filterNot{}.mapTo(){}'"
// INTENTION_TEXT: "Replace with '...flatMap{}.filter{}.mapTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence()...flatMap{}.filter{}.mapTo(){}'"
fun foo(list: List<String>, target: MutableCollection<String>) {
<caret>list
.asSequence()
.filterIndexed { i, s -> i % 10 != 0 }
.flatMap { it.indices.asSequence() }
.filterNot { it == 10 }
.filter { it != 10 }
.mapTo(target) { it.toString() }
}
@@ -3,7 +3,9 @@
// IS_APPLICABLE_2: false
fun foo(list: List<String>, target: MutableList<String>) {
<caret>for (s in list) {
if (s.length == 0) continue
if (bar(s)) continue
target.add(s)
}
}
}
fun bar(string: String): Boolean = TODO()
@@ -2,5 +2,7 @@
// INTENTION_TEXT: "Replace with 'filterNotTo(){}'"
// IS_APPLICABLE_2: false
fun foo(list: List<String>, target: MutableList<String>) {
<caret>list.filterNotTo(target) { it.length == 0 }
}
list.filterNotTo(target) { bar(it) }
}
fun bar(string: String): Boolean = TODO()
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'"
fun foo(list: List<String?>){
<caret>for (l in list) {
if (l == null) continue
if (l.startsWith("IMG:"))
println(l)
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'"
fun foo(list: List<String?>){
list
.filter { it != null && it.startsWith("IMG:") }
.forEach { println(it) }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filter{}.forEach{}'"
fun foo(list: List<String?>){
list
.asSequence()
.filter { it != null && it.startsWith("IMG:") }
.forEach { println(it) }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIsInstance<>().filterTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterIsInstance<>().filterTo(){}'"
fun foo(list: List<Any>, out: MutableList<String>){
<caret>for (any in list) {
if (any is String && any.length > 0)
out.add(any)
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIsInstance<>().filterTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterIsInstance<>().filterTo(){}'"
fun foo(list: List<Any>, out: MutableList<String>){
list
.filterIsInstance<String>()
.filterTo(out) { it.length > 0 }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIsInstance<>().filterTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterIsInstance<>().filterTo(){}'"
fun foo(list: List<Any>, out: MutableList<String>){
list
.asSequence()
.filterIsInstance<String>()
.filterTo(out) { it.length > 0 }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterNotNull().filter{}.forEach{}'"
fun foo(list: List<String?>){
<caret>for (l in list) {
if (l != null && l.startsWith("IMG:"))
println(l.hashCode())
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterNotNull().filter{}.forEach{}'"
fun foo(list: List<String?>){
list
.filterNotNull()
.filter { it.startsWith("IMG:") }
.forEach { println(it.hashCode()) }
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterNotNull().filter{}.forEach{}'"
fun foo(list: List<String?>){
list
.asSequence()
.filterNotNull()
.filter { it.startsWith("IMG:") }
.forEach { println(it.hashCode()) }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterNotNull().filter{}.forEach{}'"
fun foo(list: List<String?>){
<caret>for (l in list) {
if (l == null || !l.startsWith("IMG:")) continue
println(l.hashCode())
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterNotNull().filter{}.forEach{}'"
fun foo(list: List<String?>){
list
.filterNotNull()
.filter { it.startsWith("IMG:") }
.forEach { println(it.hashCode()) }
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().filter{}.forEach{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterNotNull().filter{}.forEach{}'"
fun foo(list: List<String?>){
list
.asSequence()
.filterNotNull()
.filter { it.startsWith("IMG:") }
.forEach { println(it.hashCode()) }
}
@@ -1,6 +1,6 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().firstOrNull{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterNotNull().firstOrNull{}'"
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
// IS_APPLICABLE_2: false
fun getFirstValue() = "value"
@@ -1,13 +1,11 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().firstOrNull{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterNotNull().firstOrNull{}'"
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
// IS_APPLICABLE_2: false
fun getFirstValue() = "value"
fun foo(list: List<String?>): String? {
val value = getFirstValue()
val <caret>found: String? = list
.filterNotNull()
.firstOrNull { !it.startsWith("IMG:") && it.contains(value) }
val found: String? = list.firstOrNull { it != null && !it.startsWith("IMG:") && it.contains(value) }
return found
}
@@ -1,14 +0,0 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().firstOrNull{}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().filterNotNull().firstOrNull{}'"
fun getFirstValue() = "value"
fun foo(list: List<String?>): String? {
val value = getFirstValue()
val <caret>found: String? = list
.asSequence()
.filterNotNull()
.firstOrNull { !it.startsWith("IMG:") && it.contains(value) }
return found
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'indexOfFirst{}'"
// IS_APPLICABLE_2: false
fun f8_indexOfFirst_complex(value: String, list: List<Any?>):Int{
<caret>for ((i, any) in list.withIndex())
if (any != null)
if (any is String)
if (any == value)
return i
return -1
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'indexOfFirst{}'"
// IS_APPLICABLE_2: false
fun f8_indexOfFirst_complex(value: String, list: List<Any?>):Int{
return list.indexOfFirst { it != null && it is String && it == value }
}
@@ -1,6 +1,6 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMap{}.filterNot{}.mapIndexedTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().flatMap{}.filterNot{}.mapIndexedTo(){}'"
// INTENTION_TEXT: "Replace with 'flatMap{}.filter{}.mapIndexedTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().flatMap{}.filter{}.mapIndexedTo(){}'"
fun foo(list: List<String>, target: MutableCollection<Int>) {
var i = 0
<caret>for (s in list) {
@@ -1,9 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMap{}.filterNot{}.mapIndexedTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().flatMap{}.filterNot{}.mapIndexedTo(){}'"
// INTENTION_TEXT: "Replace with 'flatMap{}.filter{}.mapIndexedTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().flatMap{}.filter{}.mapIndexedTo(){}'"
fun foo(list: List<String>, target: MutableCollection<Int>) {
<caret>list
.flatMap { it.indices }
.filterNot { it == 10 }
.filter { it != 10 }
.mapIndexedTo(target) { i, j -> i + j }
}
@@ -1,10 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMap{}.filterNot{}.mapIndexedTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().flatMap{}.filterNot{}.mapIndexedTo(){}'"
// INTENTION_TEXT: "Replace with 'flatMap{}.filter{}.mapIndexedTo(){}'"
// INTENTION_TEXT_2: "Replace with 'asSequence().flatMap{}.filter{}.mapIndexedTo(){}'"
fun foo(list: List<String>, target: MutableCollection<Int>) {
<caret>list
.asSequence()
.flatMap { it.indices.asSequence() }
.filterNot { it == 10 }
.filter { it != 10 }
.mapIndexedTo(target) { i, j -> i + j }
}
@@ -1,6 +1,6 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// IS_APPLICABLE_2: false
// INTENTION_TEXT: "Replace with '...filter{}.map{}.firstOrNull()'"
// INTENTION_TEXT_2: "Replace with 'asSequence()...filter{}.map{}.firstOrNull()'"
fun foo(list: List<Any>, o: Any): Int? {
<caret>for (s in list) {
if (s is String && s.length > 0) {
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '...filter{}.map{}.firstOrNull()'"
// INTENTION_TEXT_2: "Replace with 'asSequence()...filter{}.map{}.firstOrNull()'"
fun foo(list: List<Any>, o: Any): Int? {
return list
.filterIsInstance<String>()
.filter { it.length > 0 }
.map { it.length * 2 }
.firstOrNull()
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '...filter{}.map{}.firstOrNull()'"
// INTENTION_TEXT_2: "Replace with 'asSequence()...filter{}.map{}.firstOrNull()'"
fun foo(list: List<Any>, o: Any): Int? {
return list
.asSequence()
.filterIsInstance<String>()
.filter { it.length > 0 }
.map { it.length * 2 }
.firstOrNull()
}