Can Be Non-nullable
ID |
kotlin.can_be_nonnullable |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Kotlin |
Tags |
code-style, null-safety |
Description
Reports val properties declared with a nullable type T? whose initialiser
is a compile-time literal known to be non-null (string, numeric, boolean, or
character constant). Because a val cannot be reassigned, the property is
already guaranteed never to be null, and the type can be tightened from
T? to T.
Rationale
val tag: String? = "alpha" // FLAW — never null, drop the `?`
val explicit: String? = null // OK — initialiser is null
val derived: String? = compute() // OK — call may return null
val plain: String = "alpha" // OK — already non-nullable
A nullable type forces every consumer to handle the null branch — usually
with ?., !!, or ?:. When the value can never be null, that ceremony
adds reading cost without buying any safety.
Remediation
Drop the ? from the type so the compiler enforces the property’s real
contract:
val tag: String = "alpha"