Skip to content

match_getter_setter_field_names

v1.0.0WarningCode Quality

This rule flags a getter and setter pair that does not read and write the same backing field.

This is the copy-paste bug that type checking cannot catch: both members compile, both have the right signature, and the mismatch only shows up as a value that will not stick. It is most common where a class has several similar pairs, which is exactly where it is hardest to spot by reading.

Only a pair whose bodies are a single field reference is compared. A getter that computes, or a setter that validates before assigning, has no single field to compare and is skipped rather than guessed at. A compound assignment (+=, ??=) reads the field too, so its asymmetry can be deliberate and is also skipped.

This rule is in the recommended preset.

class Box {
int _width = 0;
int _height = 0;
int get width => _width;
set width(int value) => _height = value; // writes the wrong field
}
class Box {
int _width = 0;
int _height = 0;
int get width => _width;
set width(int value) => _width = value;
}

This rule is on with preset: recommended or preset: opinionated.

To disable this rule:

many_lints.yaml
rules:
match_getter_setter_field_names: false

To keep the rule on but skip certain paths, use per-rule exclude.