Inspection: Simplify Nested forEach/onEach in also/apply to onEach

^KT-40283 Fixed
This commit is contained in:
Enteerman
2020-07-21 20:50:07 +08:00
committed by Vladimir Dolzhenko
parent 85d99612a2
commit c3aeaa9052
25 changed files with 423 additions and 0 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.SimplifyNestedEachInScopeFunctionInspection
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(){
listOf(1,2,3).also<caret> { it.forEach{ it + 4 } }.forEach{ it + 5 }
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(){
listOf(1,2,3).onEach { it + 4 }.forEach{ it + 5 }
}
@@ -0,0 +1,6 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(){
val a = listOf(1,2,3)
"".also<caret> { a.forEach { it + 2 } }
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(){
listOf(1,2,3).also<caret> { s -> s.forEach{ it + 4 } }.forEach{ it + 5 }
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(){
listOf(1,2,3).onEach { it + 4 }.forEach{ it + 5 }
}
@@ -0,0 +1,5 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(){
listOf(1,2,3).also<caret> { listOf(1,2,3).forEach { } }
}
@@ -0,0 +1,5 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(){
mutableListOf(1,2,3).also<caret> { list -> list.forEach { list.add(it) } }
}
@@ -0,0 +1,7 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(){
listOf(1, 2, 3).also<caret> a@ { it.forEach { i ->
if (i % 2 == 0) return@a
}}
}
@@ -0,0 +1,7 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(){
listOf(1, 2, 3).also<caret> { it.forEach { i ->
if (i % 2 == 0) return@also
}}
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun test(){
listOf(1, 2, 3).also<caret> { it.forEach { i ->
if (i % 2 == 0) return@forEach
}}
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun test(){
listOf(1, 2, 3).onEach { i ->
if (i % 2 == 0) return@onEach
}
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(){
listOf(1,2,3).apply<caret> { forEach{ it + 4 } }.forEach{ it + 5 }
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(){
listOf(1,2,3).onEach { it + 4 }.forEach{ it + 5 }
}
@@ -0,0 +1,5 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(){
listOf(1,2,3).apply<caret> { 1.apply<caret> { forEach{ it + 1 } } }
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(){
listOf(1,2,3).apply<caret> { this@apply.forEach { it + 5 } }
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(){
listOf(1,2,3).onEach { it + 5 }
}
@@ -0,0 +1,5 @@
// PROBLEM: none
// WITH_RUNTIME
fun test(){
listOf(1,2,3).apply<caret> { forEach { this@apply.contains(it) } }
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun test(){
1.apply {
listOf(1, 2, 3).apply<caret> { forEach { this.plus(it) } }
}
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun test(){
1.apply {
listOf(1, 2, 3).onEach { this.plus(it) }
}
}