use-existing-variable

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

Reuse a local for stable SDK arithmetic and String.length/int parity getters. Calls, user getters, allocations and overloaded operators are excluded. Whole-scope mutation facts deliberately reject even later/closure writes.

Invalid

example.dartdart
int a() {
  final x = 2;
  final cached = x + 1;
  return x + 1;
}
int b() {
  final x = 3;
  final cached = x * 2;
  return (x * 2);
}
int c() {
  final x = 4;
  final cached = x - 1;
  final next = x - 1;
  return next;
}
int d() {
  final x = 5;
  final cached = (x + 1) * 2;
  return (x + 1) * 2;
}
int e() {
  final cached = 2 + 3;
  return 2 + 3;
}
void sdkGetter(String value) {
  final cached = value.length.isOdd;
  print(value.length.isOdd);
}
Reuse the existing local instead of repeating this computation.
3 final cached = x + 1;
4 return x + 1;
Reuse the existing local instead of repeating this computation.
8 final cached = x * 2;
9 return (x * 2);
Reuse the existing local instead of repeating this computation.
13 final cached = x - 1;
14 final next = x - 1;
Reuse the existing local instead of repeating this computation.
19 final cached = (x + 1) * 2;
20 return (x + 1) * 2;
Reuse the existing local instead of repeating this computation.
23 final cached = 2 + 3;
24 return 2 + 3;
Reuse the existing local instead of repeating this computation.
27 final cached = value.length.isOdd;
28 print(value.length.isOdd);

Valid

example.dartdart
int mutation() {
  var x = 1;
  final cached = x + 2;
  x = 2;
  return x + 2;
}
int captured() {
  var x = 1;
  final cached = x + 2;
  void change() { x++; }
  return x + 2;
}
Object allocate() {
  final cached = Object();
  return Object();
}
int calls(int Function() next) {
  final cached = next() + 1;
  return next() + 1;
}
int shadow() {
  final x = 1;
  final cached = x + 1;
  { final x = 2; return x + 1; }
}
int branch(bool flag) {
  final x = 1;
  if (flag) { final cached = x + 1; }
  return x + 1;
}
int division() {
  final x = 1;
  final cached = x ~/ 2;
  return x ~/ 2;
}
class Counter {
  Counter operator +(int other) => Counter();
}
Counter overloaded() {
  final x = Counter();
  final cached = x + 1;
  return x + 1;
}
int mutatedCache() {
  final x = 1;
  var cached = x + 1;
  cached = 3;
  return x + 1;
}
int constantContext() {
  final cached = 2 + 3;
  const next = 2 + 3;
  return next;
}
int lateDependency() {
  late final x = 1;
  final cached = x + 1;
  return x + 1;
}
class Unstable {
  int get length => DateTime.now().second;
}
bool customGetter(Unstable value) {
  final cached = value.length.isOdd;
  return value.length.isOdd;
}

Configure use-existing-variable

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

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

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

example.dartdart
// falcon-ignore lint/style/use-existing-variable: <reason>
  • 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.

  • use-spacer-as-expanded-childuse_spacer_as_expanded_childrecommendedflutter

    Flags an Expanded wrapping an empty SizedBox or Container that should be a Spacer.