Unnecessary Not Null Assertion

ID

kotlin.unnecessary_not_null

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Kotlin

Tags

null-safety, redundant

Description

Reports redundant !! not-null assertions applied to a receiver whose type is already non-nullable. The !! operator converts T? into T and throws NullPointerException if the receiver is null; when the receiver is already declared as T, the assertion can never throw and serves no purpose.

Rationale

fun greet(name: String) {
    println(name!!)            // FLAW — name is already non-nullable
}

fun greetMaybe(name: String?) {
    println(name!!)            // OK — name is nullable, !! is meaningful
}

Beyond being dead code, the !! signals a misunderstanding of Kotlin’s type system: the writer was either unsure of the receiver’s nullability or copied the operator from a defensive Java-style habit. Either way, removing it makes the call site shorter and the intent clearer.

Remediation

Delete the !!. If the call site still does not compile, the receiver was in fact nullable and the rule should not have fired — file a false-positive report.