Rules / Suspicious / recursive-getters

recursive-getters

lint/suspicious/recursive-getters
recommended
Same as recursive_getters · Dart lints

Flags a getter that unconditionally returns itself.

A body of => x (or => this.x, or return x;) inside get x calls the getter again with no base case, recursing until the stack overflows at runtime. It almost always means a backing field was intended — typically an underscore-prefixed _x — but was misspelled as the getter's own name. Return the backing field instead. Only a direct, unconditional self-reference is reported; a getter that merely mentions its own name inside a larger expression is left alone.

Invalid

example.dartdart
class A {
  int get x => x;

  int get y => this.y;

  String get label {
    return label;
  }

  double get z {
    return this.z;
  }

  num get w => this.w;
}

int get topLevel => topLevel;
Recursive getter — this getter unconditionally returns itself
1class A {
2 int get x => x;
Recursive getter — this getter unconditionally returns itself
3
4 int get y => this.y;
Recursive getter — this getter unconditionally returns itself
6 String get label {
7 return label;
Recursive getter — this getter unconditionally returns itself
10 double get z {
11 return this.z;
Recursive getter — this getter unconditionally returns itself
13
14 num get w => this.w;
Recursive getter — this getter unconditionally returns itself
16
17int get topLevel => topLevel;

Valid

example.dartdart
class A {
  int _x = 0;
  int _count = 0;

  int get x => _x;

  int get y {
    return _x + 1;
  }

  int get count => _count;

  int get doubled => _x * 2;

  String get label => 'label';

  num get w => this._x;
}

int _g = 0;

int get topLevel => _g;

How to configure

Set the severity of recursive-getters in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "recursive-getters": "error"
      }
    }
  }
}