match-getter-setter-field-names

Path
lint/correctness/match-getter-setter-field-names
Default
Off, opt in
Origin
Falcon
Rule
259 of 512

Detect likely copy-paste errors in trivial accessors when a different field matching the accessor name is declared. Inherited contracts and computed accessors are conservatively excluded.

Invalid

example.dartdart
class State {
  int _count = 0;
  String _name = '';
  int _total = 0;
  int _size = 0;
  int _amount = 0;
  int _quantity = 0;
  String _title = '';
  String _label = '';
  int get total => _count;
  int get size { return this._count; }
  set amount(int value) => _count = value;
  set quantity(int value) { this._count = value; }
  String get title => _name;
  set label(String value) { _name = value; }
}
Accessor `total` uses `_count` instead of the field matching its name.
9 String _label = '';
10 int get total => _count;
Accessor `size` uses `_count` instead of the field matching its name.
10 int get total => _count;
11 int get size { return this._count; }
Accessor `amount` uses `_count` instead of the field matching its name.
11 int get size { return this._count; }
12 set amount(int value) => _count = value;
Accessor `quantity` uses `_count` instead of the field matching its name.
12 set amount(int value) => _count = value;
13 set quantity(int value) { this._count = value; }
Accessor `title` uses `_name` instead of the field matching its name.
13 set quantity(int value) { this._count = value; }
14 String get title => _name;
Accessor `label` uses `_name` instead of the field matching its name.
14 String get title => _name;
15 set label(String value) { _name = value; }

Valid

example.dartdart
class State {
  int _count = 0;
  int get count => _count;
  set count(int value) => _count = value;
  int get doubled => _count * 2;
  set positive(int value) { if (value > 0) _count = value; }
  int get constant => 42;
  int get externalValue => unknown;
  set checked(int value) { print(value); _count = value; }
  set parameter(int _count) => _count = _count;
}
class Other {
  int _size = 0;
  int get size { return this._size; }
}
int unknown = 0;
class Aliases {
  bool _enabled = false;
  bool get isEnabled => _enabled;
  int _count = 0;
  int get size => _count;
  int get length => _count;
  int __value = 0;
  int get value => __value;
}
abstract class Contract {
  int get value;
}
class Inherited implements Contract {
  int _count = 0;
  int _value = 1;
  int get value => _count;
}

Configure match-getter-setter-field-names

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "match-getter-setter-field-names": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/match-getter-setter-field-names: <reason>
  • missing-test-assertionrecommendedtest

    Reports inline test callbacks that contain no recognized assertion calls.

  • no-adjacent-strings-in-listno_adjacent_strings_in_listrecommended

    Flags adjacent string literals used as an element of a list or set literal.

  • no-import-cyclescross-file

    Report mutually dependent components of imported or exported library files.

  • no-logic-in-create-stateno_logic_in_create_staterecommendedflutter

    Require StatefulWidget.createState to return only a fresh zero-argument Flutter State construction.

  • no-self-assignmentsno_self_assignmentsrecommended

    Flags assignments of a variable to itself, such as x = x;.

  • null-check-on-nullable-type-parameternull_check_on_nullable_type_parameterrecommended

    Flags a null assertion x! on a nullable type parameter T? whose expected type is T or T? for a potentially nullable T, and null-assert patterns whose matched value is a nullable type parameter.

  • null-closuresnull_closuresrecommended

    Reject literal null in the closure slots of curated SDK APIs.

  • prefer-correct-json-casts

    Decode JSON containers using their actual runtime generic types.