KT.USE_IF_EMPTY_OR_IF_BLANK

Use 'ifEmpty' or 'ifBlank'

This rule detects isEmpty or isBlank calls to assign a default value. They can be replaced with ifEmpty or ifBlank calls.

Noncompliant Code

Copy
fun test(list: List<Int>, s: String) {
    val a = if (list.isEmpty()) listOf(1) else list
    val b = if (list.isNotEmpty()) list else listOf(2)
    val c = if (s.isBlank()) "foo" else s
    val d = if (s.isNotBlank()) s else "bar"
}

Compliant Code

Copy
fun test(list: List<Int>, s: String) {
    val a = list.ifEmpty { listOf(1) }
    val b = list.ifEmpty { listOf(2) }
    val c = s.ifBlank { "foo" }
    val d = s.ifBlank { "bar" }
}

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