Skip to content

Files

Latest commit

 

History

History
33 lines (24 loc) · 814 Bytes

UseIfEmptyOrIfBlank.md

File metadata and controls

33 lines (24 loc) · 814 Bytes

Pattern: Use of isEmpty/isBlank

Issue: -

Description

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

Example of incorrect code:

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"
}

Example of correct code:

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" }
}

Further Reading