Skip to main content

avoid-unnecessary-reassignment

Warns when a value is reassigned to a variable without using the initial value.

Example

❌ Bad:

String alpha = '';
if (something) {
// LINT: Avoid unnecessary reassignment.
// Try moving this assignment to the initial variable declaration.
alpha = 'true value';
} else {
// LINT: Avoid unnecessary reassignment.
// Try moving this assignment to the initial variable declaration.
alpha = 'false value';
}

String beta = '';
// LINT: Avoid unnecessary reassignment.
// Try moving this assignment to the initial variable declaration.
beta = something ? 'true value' : 'false value';

SomeObject object = SomeObject();
// LINT: Avoid unnecessary reassignment.
// Try moving this assignment to the initial variable declaration.
object = something ? SomeObject() : SomeObject();

✅ Good:

final alpha = something ? 'true value' : 'false value';
final beta = something ? 'true value' : 'false value';
final object = SomeObject();

Additional Resources