Acton
Linting rules

E003: mutable-variable-can-be-immutable

Checks for variables that are declared as mutable (`var`) but are never mutated.

Metadata

  • Code: E003
  • Rule: mutable-variable-can-be-immutable
  • Status: Stable since v0.0.1
  • Quick fix: sometimes available

What it does

Checks for variables that are declared as mutable (var) but are never mutated.

Why is this bad?

Using val instead of var makes the code clearer by signaling that the variable's value will not change.

Example

fun main() {
    var x = 1;
E003: variable can be immutable
println(x); }

Use instead:

fun main() {
    val x = 1;
    println(x);
}

Behavior notes

  • Variables with zero usages are handled by unused-variable and are not reported here.
  • A variable is considered mutated when write usage is detected (for example =, +=, mutate arg, field/index writes, mutable method calls).
  • Autofix is available for simple var x = ... declarations. Destructuring declarations still warn but may not have a fix.
Source code

Last updated on

On this page