"Use withIndex()" intention&inspection

This commit is contained in:
Valentin Kipyatkov
2016-05-13 17:02:40 +03:00
parent 46055dfd2d
commit d658d3b9cf
23 changed files with 248 additions and 15 deletions
@@ -1,5 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'forEachIndexed{}'"
// IS_APPLICABLE: false
fun foo(list: List<String>) {
<caret>for ((index, s) in list.withIndex()) {
println(s.hashCode() * index)
@@ -1,5 +0,0 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'forEachIndexed{}'"
fun foo(list: List<String>) {
list.forEachIndexed { index, s -> println(s.hashCode() * index) }
}
+1
View File
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.loopToCallChain.UseWithIndexIntention
@@ -0,0 +1,11 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
fun foo(list: List<String>): Int? {
var index = 0
<caret>for ((i, s) in list.withIndex()) {
val x = s.length * index * i
index++
if (x > 0) return x
}
return null
}
@@ -0,0 +1,16 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
class X {
operator fun iterator(): Iterator<String>{
return emptyList<String>().iterator()
}
}
fun foo(x: X) {
var index = 0
<caret>for (s in x) {
if (s.length > index++) {
println(s)
}
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun foo(list: List<String>) {
var i = 0
<caret>for (s in list) {
println(i)
val x = s.length * i++
if (x > 1000) break
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun foo(list: List<String>) {
for ((i, s) in list.withIndex()) {
println(i)
val x = s.length * i
if (x > 1000) break
}
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun foo(list: List<String>) {
var i = 0
<caret>for (s in list) {
println(i)
val x = s.length * ++i
if (x > 1000) break
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun foo(list: List<String>) {
for ((i, s) in list.withIndex()) {
println(i)
val x = s.length * (i + 1)
if (x > 1000) break
}
}
+10
View File
@@ -0,0 +1,10 @@
// WITH_RUNTIME
fun foo(list: List<String>): Int? {
var index = 0
<caret>for (s in list) {
val x = s.length * index
index++
if (x > 0) return x
}
return null
}
+8
View File
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun foo(list: List<String>): Int? {
for ((index, s) in list.withIndex()) {
val x = s.length * index
if (x > 0) return x
}
return null
}