Manually incremented index variable can be replaced with use of 'withIndex()'
Reports for
loops with a manually incremented index variable.
for
loops with a manually incremented index variable can be simplified with the withIndex()
function.
Use withIndex() instead of manual index increment quick-fix can be used to amend the code automatically.
Example:
fun foo(list: List<String>): Int? {
var index = 0
for (s in list) { <== can be simplified
val x = s.length * index
index++
if (x > 0) return x
}
return null
}
After the quick-fix is applied:
fun foo(list: List<String>): Int? {
for ((index, s) in list.withIndex()) {
val x = s.length * index
if (x > 0) return x
}
return null
}
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
UseWithIndex- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Kotlin, 243.23126-IJ |
Last modified: 03 December 2024