Skip to content

Latest commit

 

History

History
35 lines (24 loc) · 613 Bytes

NullableToStringCall.md

File metadata and controls

35 lines (24 loc) · 613 Bytes

Pattern: Nullable toString call

Issue: -

Description

Turn on this rule to flag toString calls with a nullable receiver that may return the string null.

Example of incorrect code:

fun foo(a: Any?): String {
    return a.toString()
}

fun bar(a: Any?): String {
    return "$a"
}

Example of correct code:

fun foo(a: Any?): String {
    return a?.toString() ?: "-"
}

fun bar(a: Any?): String {
    return "${a ?: "-"}"
}

Further Reading