Linting rules
E003: mutable-variable-can-be-immutable
Checks for variables that are declared as mutable (`var`) but are never mutated.
Metadata
Code:E003Rule:mutable-variable-can-be-immutableStatus: Stable sincev0.0.1Quick 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; println(x);
}Use instead:
fun main() {
val x = 1;
println(x);
}Behavior notes
- Variables with zero usages are handled by
unused-variableand 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.
Last updated on