KT.UNSAFE_CALL_ON_NULLABLE_TYPE

Unsafe call on nullable types

Reports unsafe calls on nullable types. These calls will throw a NullPointerException in case the nullable value is null. Kotlin provides many ways to work with nullable types to increase null safety. Guard the code appropriately to prevent NullPointerExceptions.

Noncompliant Code

Copy
fun foo(str: String?) {
    println(str!!.length)
}

Compliant Code

Copy
fun foo(str: String?) {
    println(str?.length)
}

Options

  • excludes (default: ['**/test/**', '**/androidtest/**', '**/commontest/**', '**/jvmtest/**', '**/jstest/**', '**/iostest/**'])

    path filters

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