avoid-assigning-to-static-field

Path
lint/correctness/avoid-assigning-to-static-field
Default
Off, opt in
Origin
Falcon
Rule
15 of 512

Flags instance methods writing resolved static fields, including compound assignments and increments. Static methods, top-level functions, declarations, instance fields, setters, and shadowed names are excluded.

Invalid

example.dartdart
class Counter {
  static int count = 0;
  static int? cached;
  void update() {
    count = 1;
    count += 2;
    count++;
    --count;
    cached ??= 1;
    Counter.count = 2;
    Counter.count *= 2;
    Counter.count--;
  }
}
Avoid assigning to static fields from instance methods; move the state to an instance or make the method static.
4 void update() {
5 count = 1;
Avoid assigning to static fields from instance methods; move the state to an instance or make the method static.
5 count = 1;
6 count += 2;
Avoid assigning to static fields from instance methods; move the state to an instance or make the method static.
6 count += 2;
7 count++;
Avoid assigning to static fields from instance methods; move the state to an instance or make the method static.
7 count++;
8 --count;
Avoid assigning to static fields from instance methods; move the state to an instance or make the method static.
8 --count;
9 cached ??= 1;
Avoid assigning to static fields from instance methods; move the state to an instance or make the method static.
9 cached ??= 1;
10 Counter.count = 2;
Avoid assigning to static fields from instance methods; move the state to an instance or make the method static.
10 Counter.count = 2;
11 Counter.count *= 2;
Avoid assigning to static fields from instance methods; move the state to an instance or make the method static.
11 Counter.count *= 2;
12 Counter.count--;

Valid

example.dartdart
class Counter {
  static int count = 0;
  int value = 0;
  static set setting(int value) {}
  void change(int count) {
    count = 1;
    value = 2;
    value++;
    this.value += 2;
  }
  void local() {
    int count = 0;
    count++;
  }
  static void reset() {
    count = 0;
    Counter.count += 1;
    count++;
  }
}
void change(Counter counter) {
  counter.value = 3;
  Counter.setting = 1;
  final read = Counter.count;
  Counter.count = 2;
  Counter.count--;
}

Configure avoid-assigning-to-static-field

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-assigning-to-static-field": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/avoid-assigning-to-static-field: <reason>