recursive-getters

Same as recursive_getters in Dart lints

Path
lint/suspicious/recursive-getters
Default
On (recommended)
Origin
Dart lints
Rule
429 of 512
recommended

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;
Avoid this recursive getter because it unconditionally returns itself.
1class A {
2 int get x => x;
Avoid this recursive getter because it unconditionally returns itself.
3
4 int get y => this.y;
Avoid this recursive getter because it unconditionally returns itself.
6 String get label {
7 return label;
Avoid this recursive getter because it unconditionally returns itself.
10 double get z {
11 return this.z;
Avoid this recursive getter because it unconditionally returns itself.
13
14 num get w => this.w;
Avoid this recursive getter because it 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;

Configure recursive-getters

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

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

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

example.dartdart
// falcon-ignore lint/suspicious/recursive-getters: <reason>