KT.COULD_BE_SEQUENCE

Possible performance penalty due to a new list being created for each call

Long chains of collection operations will have a performance penalty due to a new list being created for each call. Consider using sequences instead. Read more about this in the https://kotlinlang.org/docs/sequences.html

Noncompliant Code

Copy
listOf(1, 2, 3, 4).map { it*2 }.filter { it < 4 }.map { it*it }

Compliant Code

Copy
listOf(1, 2, 3, 4).asSequence().map { it*2 }.filter { it < 4 }.map { it*it }.toList()
listOf(1, 2, 3, 4).map { it*2 }

Options

  • threshold (default: 3)

    the number of chained collection operations required to trigger rule

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html