Back to Intellij Community

LoopToCallChain

plugins/kotlin/code-insight/descriptions/resources-en/inspectionDescriptions/LoopToCallChain.html

2025.3-rc-2486 B
Original Source

Reports for loops that can be replaced with a sequence of stdlib operations (like map, filter, and so on).

Example:

fun foo(list: List<String>): List<Int> {
  val result = ArrayList<Int>()
  for (s in list) {
     if (s.length > 0)
       result.add(s.hashCode())
     }
  return result
}

After the quick-fix is applied:

fun foo(list: List<String>): List<Int> {
  val result = list
    .filter { it.length > 0 }
    .map { it.hashCode() }
  return result
}