KT.SAFE_CAST

Casts should be replaced with safe casts

This rule inspects casts and reports casts which could be replaced with safe casts instead.

Noncompliant Code

Copy
fun numberMagic(number: Number) {
    val i = if (number is Int) number else null
    // ...
}

Compliant Code

Copy
fun numberMagic(number: Number) {
    val i = number as? Int
    // ...
}

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