KT.DONT_DOWNCAST_COLLECTION_TYPES

Down-casting immutable collection types

Down-casting immutable types from kotlin.collections should be discouraged. The result of the downcast is platform specific and can lead to unexpected crashes. Prefer to use instead the toMutable<Type>() functions.

Noncompliant Code

Copy
val list: List<Int> = getAList()
if (list is MutableList) {
  list.add(42)
}
(list as MutableList).add(42)

Compliant Code

Copy
val list: List<Int> = getAList()
list.toMutableList().add(42)

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