no-self-assignments
Same as no_self_assignments in Dart lints
- Group
- Correctness
- Path
lint/correctness/no-self-assignments- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 296 of 512
Flags assignments of a variable to itself, such as x = x;.
A self-assignment has no effect and usually hides a typo — most often a
constructor or setter that meant to write this.x = x;. Only plain =
assignments whose two sides are the same simple identifier (x = x) or the
same prefixed identifier (a.b = a.b) are reported; this.x = x and
compound assignments are left alone.
class C {
int x;
C(this.x);
void set(int x) {
x = x; // flagged: assigns the parameter to itself
this.x = x; // fine
}
}
Invalid
class C {
int x = 0;
int y = 0;
C(int x, int y) {
x = x;
y = y;
}
void update(int x) {
x = x;
}
void field() {
x = x;
}
}
class Holder {
int value = 0;
}
void prefixed(Holder holder) {
holder.value = holder.value;
}
int top = 0;
void topLevel() {
top = top;
}
void inExpression(int a) {
print(a = a);
}
void inClosure() {
var local = 1;
void f() {
local = local;
}
f();
}
void comments(int value) {
value /* left */ = /* right */ value;
value = // Comment before the RHS.
value;
} C(int x, int y) { x = x; x = x; y = y; void update(int x) { x = x; void field() { x = x;void prefixed(Holder holder) { holder.value = holder.value;void topLevel() { top = top;void inExpression(int a) { print(a = a); void f() { local = local;void comments(int value) { value /* left */ = /* right */ value; value /* left */ = /* right */ value; value = // Comment before the RHS.Valid
class C {
int x = 0;
int y = 0;
C(int x, int y) {
this.x = x;
this.y = y;
}
// `this.x` is a property access, not an identifier.
void reassign() {
this.x = this.x;
}
void compound(int z) {
z += z;
z ??= z;
}
void different(int a, int b) {
a = b;
}
}
class Holder {
int value = 0;
Holder? next;
}
void nullAware(Holder holder) {
holder.next?.value = holder.next?.value ?? 0;
}
void differentPrefix(Holder a, Holder b) {
a.value = b.value;
}
void parenthesized(int a) {
a = (a);
}
void cascade(Holder holder) {
final value = holder.value;
holder..value = value;
}
void deeper(Holder holder) {
holder.next!.value = holder.next!.value;
}Configure no-self-assignments
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"no-self-assignments": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/no-self-assignments: <reason>More correctness rules
- null-check-on-nullable-type-parameter
null_check_on_nullable_type_parameterrecommendedFlags a null assertion
x!on a nullable type parameterT?whose expected type isTorT?for a potentially nullableT, and null-assert patterns whose matched value is a nullable type parameter. - null-closures
null_closuresrecommendedReject literal
nullin the closure slots of curated SDK APIs. - prefer-correct-json-casts
Decode JSON containers using their actual runtime generic types.
- prefer-define-hero-tagflutter
Require an explicit heroTag on Flutter
FloatingActionButtonconstructors. - prefer-expect-latertest
Requires expectLater when a canonical expect call receives an unawaited Future.
- prefer-immutable-bloc-eventsrecommendedbloc
Flags Bloc event classes without an explicit @immutable annotation.
- prefer-immutable-bloc-staterecommendedbloc
Flags Bloc state classes without an explicit @immutable annotation.
- prefer-immutable-provider-argumentsriverpod
Requires stable value equality for freshly created Riverpod family keys.