Docs
Linting rules

E031: unnecessary-not-null-assertion

Detects a not-null assertion when the expression's inferred type cannot be `null`.

Metadata

  • Code: E031
  • Rule: unnecessary-not-null-assertion
  • Status: Stable since v0.0.1
  • Quick 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;
E031: unnecessary not-null assertion
} return 0; }

Use instead:

fun addOne(value: int?): int {
    if (value != null) {
        return value + 1;
    }
    return 0;
}
Source code

Last updated on

On this page