Variable or field assigned to itself
ID |
dart.correctness.self_assignment |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Logic |
Language |
Dart |
Description
Reports an assignment whose left and right sides are the same variable or the same field access, such as timeout = timeout; or this.name = this.name;. The assignment has no effect at run time.
Rationale
A self-assignment is always a mistake, and usually a revealing one. The common case is a constructor or setter where a parameter shadows a field of the same name: the author meant this.timeout = timeout and wrote timeout = timeout, so the field is silently left at its default and the value the caller passed is thrown away. The compiler cannot help, because the statement is perfectly legal, and the resulting bug shows up far from the line that caused it.
The following code illustrates the pattern detected by this rule:
class Settings {
int timeout = 0;
String name = '';
int retries = 0;
void applyTimeout(int timeout) {
// FLAGGED: Variable or field assigned to itself
timeout = timeout;
}
}