Skip to main content

avoid-self-assignment

added in: 1.1.0
🛠
Pro+
preset: recommended

Warns when a variable or a class instance field / property is assigned to itself.

Example

❌ Bad:

class SomeClass {
String someString = '123';

void update(String newValue) {
// LINT: Avoid unnecessary self-assignment. Try checking this assignment for a potential mistake or removing it.
someString = someString;

if (someString == '1') {
// LINT: Outer if statement already ensures that the assignment target equals to the assigned value. Try checking this assignment for a potential mistake or removing it.
someString = '1';
}
}
}

✅ Good:

class SomeClass {
String? someString = '123';

void update(String newValue) {
someString = newValue; // Correct, another value

if (someString == '1') {
someString = null;
}
}
}