Introduce "Call chain on collection should be converted into 'Sequence'"

So #KT-15476 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-06-29 18:52:14 +03:00
committed by Mikhail Glukhikh
parent 76a98c8e67
commit 108dea5b46
42 changed files with 668 additions and 0 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.collections.ConvertCallChainIntoSequenceInspection
@@ -0,0 +1,35 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Pair<List<Int>, List<Int>>> {
return list
.<caret>chunked(1)
.distinct()
.distinctBy { it }
.drop(1)
.dropWhile { true }
.filter { true }
.filterIndexed { index, list -> true }
.filterIsInstance<Int>()
.filterNot { true }
.filterNotNull()
.map { it }
.mapIndexed { index, i -> i }
.mapIndexedNotNull { index, i -> i }
.mapNotNull { it }
.minus(1)
.minusElement(1)
.onEach {}
.plus(1)
.plusElement(1)
.requireNoNulls()
.sorted()
.sortedBy { it }
.sortedByDescending { it }
.sortedDescending()
.sortedWith(Comparator { o1, o2 -> 0 })
.take(1)
.takeWhile { true }
.windowed(1, 1, true)
.zipWithNext()
.zipWithNext { a, b -> a }
}
@@ -0,0 +1,37 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Pair<List<Int>, List<Int>>> {
return list
.asSequence()
.chunked(1)
.distinct()
.distinctBy { it }
.drop(1)
.dropWhile { true }
.filter { true }
.filterIndexed { index, list -> true }
.filterIsInstance<Int>()
.filterNot { true }
.filterNotNull()
.map { it }
.mapIndexed { index, i -> i }
.mapIndexedNotNull { index, i -> i }
.mapNotNull { it }
.minus(1)
.minusElement(1)
.onEach {}
.plus(1)
.plusElement(1)
.requireNoNulls()
.sorted()
.sortedBy { it }
.sortedByDescending { it }
.sortedDescending()
.sortedWith(Comparator { o1, o2 -> 0 })
.take(1)
.takeWhile { true }
.windowed(1, 1, true)
.zipWithNext()
.zipWithNext { a, b -> a }
.toList()
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun test(list: List<Int>): Int {
return list // comment1
.<caret>filter { it > 1 } // comment2
.map { it * 2 } // comment3
.sum() // comment4
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test(list: List<Int>): Int {
return list // comment1
.asSequence()
.filter { it > 1 } // comment2
.map { it * 2 } // comment3
.sum() // comment4
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list // comment1
.<caret>filter { it > 1 } // comment2
.map { it * 2 } // comment3
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list // comment1
.asSequence()
.filter { it > 1 } // comment2
.map { it * 2 }
.toList() // comment3
}
@@ -0,0 +1,6 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list.<caret>filter { it > 1 }
}
@@ -0,0 +1,6 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list.filter { it > 2 }.dropLast(1).<caret>filter { it > 2 }
}
@@ -0,0 +1,11 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list
.reversed()
.map { it + 1 }
.<caret>map { it + 1 }
.dropLast(1)
.takeLast(2)
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test(list: List<Int>?): List<Int>? {
return list
?.<caret>filter { it > 1 }
?.map { it * 2 }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test(list: List<Int>?): List<Int>? {
return list
?.asSequence()
?.filter { it > 1 }
?.map { it * 2 }
?.toList()
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun test(list: List<Int>?): List<Int>? {
return list?.filter { it > 1 }!!.<caret>filter { it > 2 }.filter { it > 3 }
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun test(list: List<Int>?): List<Int>? {
return list?.filter { it > 1 }!!.asSequence().filter { it > 2 }.filter { it > 3 }.toList()
}
@@ -0,0 +1,11 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list
.<caret>filter { it > 1 }
.mapNotNull {
if (it == 2) return emptyList()
it * 2
}
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list
.<caret>filter { it > 1 }
.mapNotNull {
if (it == 2) return@mapNotNull null
it * 2
}
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list
.asSequence()
.filter { it > 1 }
.mapNotNull {
if (it == 2) return@mapNotNull null
it * 2
}
.toList()
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun test(): List<Int> {
return listOf(1, 2, 3).<caret>filter { it > 1 }.map { it * 2 }
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun test(): List<Int> {
return listOf(1, 2, 3).asSequence().filter { it > 1 }.map { it * 2 }.toList()
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list
.filter<caret> { it > 1 }
.map { it * 2 }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list
.asSequence()
.filter { it > 1 }
.map { it * 2 }
.toList()
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list
.reversed()
.<caret>map { it + 1 }
.map { it + 1 }
.dropLast(1)
.takeLast(2)
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun test(list: List<Int>): List<Int> {
return list
.reversed()
.asSequence()
.map { it + 1 }
.map { it + 1 }
.toList()
.dropLast(1)
.takeLast(2)
}
@@ -0,0 +1,11 @@
// WITH_RUNTIME
fun test(foo: Foo): List<Int> {
return foo.getList()
.<caret>filter { it > 1 }
.map { it * 2 }
}
class Foo {
fun getList(): List<Int> = listOf(1, 2, 3)
}
@@ -0,0 +1,13 @@
// WITH_RUNTIME
fun test(foo: Foo): List<Int> {
return foo.getList()
.asSequence()
.filter { it > 1 }
.map { it * 2 }
.toList()
}
class Foo {
fun getList(): List<Int> = listOf(1, 2, 3)
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun test(): Int {
return listOf(1, 2, 3)
.<caret>filter { it > 1 }
.map { it * 2 }
.sum()
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test(): Int {
return listOf(1, 2, 3)
.asSequence()
.filter { it > 1 }
.map { it * 2 }
.sum()
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun test(): Int {
return listOf(1, 2, 3)
.<caret>filter { it > 1 }
.map { it * 2 }
.binarySearch(4)
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun test(): Int {
return listOf(1, 2, 3)
.asSequence()
.filter { it > 1 }
.map { it * 2 }
.toList()
.binarySearch(4)
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun test(): Int {
return listOf(1, 2, 3)
.<caret>filter { it > 1 }
.map { it * 2 }
.let {
it.binarySearch(1)
}
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
fun test(): Int {
return listOf(1, 2, 3)
.asSequence()
.filter { it > 1 }
.map { it * 2 }
.toList()
.let {
it.binarySearch(1)
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun test(list: List<List<Int>>): List<Int> {
return list
.<caret>filter { it.count() > 2 }
.map { it + it }
.flatMap { it + it }
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun test(list: List<List<Int>>): List<Int> {
return list
.asSequence()
.filter { it.count() > 2 }
.map { it + it }
.toList()
.flatMap { it + it }
}