avoid-redundant-pattern-field-names

Same as avoid_redundant_pattern_field_names in Pyramid Lint

Group
Style
Path
lint/style/avoid-redundant-pattern-field-names
Default
On (recommended)
Origin
Pyramid Lint
Rule
123 of 512
recommended

Flags redundant field names in object and record patterns.

When a pattern binds a field to a variable of the same name — Point(x: x) or (name: name) — the explicit label merely repeats the getter name for no benefit. The shorthand Point(:x) / (:name) is shorter and equivalent. Only a field whose label matches its bound variable is flagged; a genuine rename such as Point(x: px) is left alone.

Invalid

example.dartdart
class Point {
  final int x;
  final int y;
  const Point(this.x, this.y);
}

void objectPatterns(Object o) {
  switch (o) {
    case Point(
          x: x,
          y: y,
        ):
      print('$x$y');
    default:
      break;
  }
}

void ifCase(Object o) {
  if (o case Point(x: x)) {
    print(x);
  }
}

int switchExpr(Object o) => switch (o) {
      Point(y: y) => y,
      _ => 0,
    };

void recordPattern(Object o) {
  switch (o) {
    case (
          first: first,
          second: second,
        ):
      print('$first$second');
    default:
      break;
  }
}
Remove the redundant field name from this pattern and use shorthand such as `:x`.
9 case Point(
10 x: x,
Remove the redundant field name from this pattern and use shorthand such as `:x`.
10 x: x,
11 y: y,
Remove the redundant field name from this pattern and use shorthand such as `:x`.
19void ifCase(Object o) {
20 if (o case Point(x: x)) {
Remove the redundant field name from this pattern and use shorthand such as `:x`.
25int switchExpr(Object o) => switch (o) {
26 Point(y: y) => y,
Remove the redundant field name from this pattern and use shorthand such as `:x`.
32 case (
33 first: first,
Remove the redundant field name from this pattern and use shorthand such as `:x`.
33 first: first,
34 second: second,

Valid

example.dartdart
class Point {
  final int x;
  final int y;
  const Point(this.x, this.y);
}

void shorthand(Object o) {
  switch (o) {
    case Point(:final x, :final y):
      print('$x$y');
    default:
      break;
  }
}

void renamed(Object o) {
  switch (o) {
    case Point(x: final a, y: final b):
      print('$a$b');
    default:
      break;
  }
}

void differentNames(Object o) {
  if (o case Point(x: y)) {
    print(y);
  }
}

int switchExpr(Object o) => switch (o) {
      Point(:final x) => x,
      _ => 0,
    };

void records(Object o) {
  switch (o) {
    case (first: final a, second: final b):
      print('$a$b');
    default:
      break;
  }
}

Configure avoid-redundant-pattern-field-names

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-redundant-pattern-field-names": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/avoid-redundant-pattern-field-names: <reason>