KT.MAP_GET_WITH_NOT_NULL_ASSERTION_OPERATOR

Call to 'map.get()' with a not-null assertion operator (!!)

Reports calls of the map access methods map[] or map.get() with a not-null assertion operator !!. This may result in a NullPointerException. Preferred access methods are map[] without !!, map.getValue(), map.getOrDefault() or map.getOrElse(). Based on an IntelliJ IDEA inspection MapGetWithNotNullAssertionOperatorInspection.

Noncompliant Code

Copy
val map = emptyMap<String, String>()
map["key"]!!

val map = emptyMap<String, String>()
map.get("key")!!

Compliant Code

Copy
val map = emptyMap<String, String>()
map["key"]

val map = emptyMap<String, String>()
map.getValue("key")

val map = emptyMap<String, String>()
map.getOrDefault("key", "")

val map = emptyMap<String, String>()
map.getOrElse("key", { "" })

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