Skip to content

Files

Latest commit

 

History

History
27 lines (18 loc) · 571 Bytes

CastToNullableType.md

File metadata and controls

27 lines (18 loc) · 571 Bytes

Pattern: Cast to nullable type

Issue: -

Description

There are cases where as String? is misused as the safe cast (as? String), so if you want to prevent those cases, turn on this rule.

Example of incorrect code:

fun foo(a: Any?) {
    val x: String? = a as String? // If 'a' is not String, ClassCastException will be thrown.
}

Example of correct code:

fun foo(a: Any?) {
    val x: String? = a as? String
}

Further Reading