Unnecessary Safe Call

ID

kotlin.unnecessary_safe_call

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Kotlin

Tags

null-safety, redundant

Description

Reports ?. safe-call accesses where the receiver is already non-nullable. The ?. operator short-circuits a member access when the receiver is null; when the type system guarantees the receiver is non-null, the short-circuit can never trigger and the syntax is dead.

Rationale

fun process(name: String) {
    val len = name?.length    // FLAW — name is non-nullable
    name?.trim()              // FLAW — name is non-nullable
}

fun process(name: String?) {
    val len = name?.length    // OK — name is nullable
}

A redundant ?. also pollutes the inferred result type: name?.length on a non-nullable String still returns Int?, forcing every downstream consumer to handle a null branch that can never happen.

Remediation

Replace ?. with .. If the call site fails to compile after the change, the receiver was in fact nullable and the rule should not have fired.