no-self-assignments

Same as no_self_assignments in Dart lints

Path
lint/correctness/no-self-assignments
Default
On (recommended)
Origin
Dart lints
Rule
296 of 512
recommended

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

example.dartdart
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;
}
The variable or property is being assigned to itself.
5 C(int x, int y) {
6 x = x;
The variable or property is being assigned to itself.
6 x = x;
7 y = y;
The variable or property is being assigned to itself.
10 void update(int x) {
11 x = x;
The variable or property is being assigned to itself.
14 void field() {
15 x = x;
The variable or property is being assigned to itself.
23void prefixed(Holder holder) {
24 holder.value = holder.value;
The variable or property is being assigned to itself.
29void topLevel() {
30 top = top;
The variable or property is being assigned to itself.
33void inExpression(int a) {
34 print(a = a);
The variable or property is being assigned to itself.
39 void f() {
40 local = local;
The variable or property is being assigned to itself.
46void comments(int value) {
47 value /* left */ = /* right */ value;
The variable or property is being assigned to itself.
47 value /* left */ = /* right */ value;
48 value = // Comment before the RHS.

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "no-self-assignments": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/no-self-assignments: <reason>