use-existing-destructuring

Group
Style
Path
lint/style/use-existing-destructuring
Default
Off, opt in
Origin
Falcon
Rule
488 of 512

Reuse or extend an existing record destructuring instead of rereading a field. Records have immutable built-in fields. Object getters and list/map indexing can execute user code or change value and are intentionally not candidates.

Invalid

example.dartdart
int a((int, int) pair) {
  final (first, second) = pair;
  return pair.$1;
}
int b((int, int) pair) {
  final (first, second) = pair;
  return pair.$2;
}
String c(({String name}) person) {
  final (:name) = person;
  return person.name;
}
int d(({int count}) record) {
  final (count: total) = record;
  return record.count;
}
int e() {
  final pair = (1, 2);
  var (first, second) = pair;
  print(pair.$1);
  return first;
}
int extendPositional((int, int) pair) {
  final (first, _) = pair;
  return pair.$2;
}
int extendNamed(({int first, int second}) pair) {
  final (:first) = pair;
  return pair.second;
}
Reuse or extend the existing record destructuring instead of reading this field again.
2 final (first, second) = pair;
3 return pair.$1;
Reuse or extend the existing record destructuring instead of reading this field again.
6 final (first, second) = pair;
7 return pair.$2;
Reuse or extend the existing record destructuring instead of reading this field again.
10 final (:name) = person;
11 return person.name;
Reuse or extend the existing record destructuring instead of reading this field again.
14 final (count: total) = record;
15 return record.count;
Reuse or extend the existing record destructuring instead of reading this field again.
19 var (first, second) = pair;
20 print(pair.$1);
Reuse or extend the existing record destructuring instead of reading this field again.
24 final (first, _) = pair;
25 return pair.$2;
Reuse or extend the existing record destructuring instead of reading this field again.
28 final (:first) = pair;
29 return pair.second;

Valid

example.dartdart
int mutation((int, int) pair) {
  var (first, second) = pair;
  first = 3;
  return pair.$1;
}
int sourceMutation((int, int) pair) {
  final (first, second) = pair;
  pair = (3, 4);
  return pair.$1;
}
int otherSource((int, int) pair, (int, int) other) {
  final (first, second) = pair;
  return other.$1;
}
int scoped((int, int) pair) {
  { final (first, second) = pair; }
  return pair.$1;
}
int list(List<int> values) {
  final [first, ...rest] = values;
  return values[0];
}
class C { int get value => DateTime.now().second; }
int getter(C object) {
  final C(:value) = object;
  return object.value;
}
int captured((int, int) pair) {
  var (first, second) = pair;
  void change() { first++; }
  return pair.$1;
}

Configure use-existing-destructuring

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "use-existing-destructuring": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/use-existing-destructuring: <reason>
  • use-existing-variable

    Reuse a local for stable SDK arithmetic and String.length/int parity getters.

  • use-full-hex-values-for-flutter-colorsuse_full_hex_values_for_flutter_colorsrecommendedflutter

    Require an eight-digit hexadecimal integer for the canonical Flutter Color default constructor.

  • use-function-type-syntax-for-parametersuse_function_type_syntax_for_parametersrecommended

    Flags an old-style function-typed parameter written with inline parameter syntax.

  • use-if-null-to-convert-nulls-to-boolsuse_if_null_to_convert_nulls_to_boolsrecommended

    Prefers ?? to comparing a nullable bool against a boolean literal.

  • use-late-for-private-fields-and-variablesuse_late_for_private_fields_and_variables

    Flags a private nullable field or top-level variable that is only ever assigned non-null values and read through !, so it could be late with a non-nullable type.

  • use-named-constantsuse_named_constantsrecommended

    Prefers a class's predefined static constant to constructing an equal constant.

  • use-raw-stringsuse_raw_strings

    Flags string literals whose only escapes are \\ and \$, which a raw string would express without any backslash.

  • use-setters-to-change-propertiesuse_setters_to_change_properties

    Flags one-parameter void methods that only assign that parameter to a field.