Linting rules
E031: unnecessary-not-null-assertion
Detects a not-null assertion when the expression's inferred type cannot be `null`.
Metadata
Code:E031Rule:unnecessary-not-null-assertionStatus: Stable sincev0.0.1Quick fix: sometimes available
What it does
Detects a not-null assertion when the expression's inferred type cannot be null.
Why is this bad?
The ! operator only removes null from a nullable type. Applying it to an expression
that is already non-null has no effect and can mislead readers into expecting a nullable value.
Behavior notes
For a direct chain such as value!!, E031 reports only the outermost unnecessary !.
When E014 is also enabled, E014 owns the chain's auto-fix to avoid overlapping edits.
Example
fun addOne(value: int?): int {
if (value != null) {
return value! + 1; }
return 0;
}Use instead:
fun addOne(value: int?): int {
if (value != null) {
return value + 1;
}
return 0;
}Last updated on