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
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);
} final cached = x + 1; return x + 1; final cached = x * 2; return (x * 2); final cached = x - 1; final next = x - 1; final cached = (x + 1) * 2; return (x + 1) * 2; final cached = 2 + 3; return 2 + 3; final cached = value.length.isOdd; print(value.length.isOdd);Valid
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.
{
"linter": {
"rules": {
"style": {
"use-existing-variable": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/use-existing-variable: <reason>More style rules
- use-full-hex-values-for-flutter-colors
use_full_hex_values_for_flutter_colorsrecommendedflutterRequire an eight-digit hexadecimal integer for the canonical Flutter Color default constructor.
- use-function-type-syntax-for-parameters
use_function_type_syntax_for_parametersrecommendedFlags an old-style function-typed parameter written with inline parameter syntax.
- use-if-null-to-convert-nulls-to-bools
use_if_null_to_convert_nulls_to_boolsrecommendedPrefers
??to comparing a nullableboolagainst a boolean literal. - use-late-for-private-fields-and-variables
use_late_for_private_fields_and_variablesFlags a private nullable field or top-level variable that is only ever assigned non-null values and read through
!, so it could belatewith a non-nullable type. - use-named-constants
use_named_constantsrecommendedPrefers a class's predefined static constant to constructing an equal constant.
- use-raw-strings
use_raw_stringsFlags string literals whose only escapes are
\\and\$, which a raw string would express without any backslash. - use-setters-to-change-properties
use_setters_to_change_propertiesFlags one-parameter
voidmethods that only assign that parameter to a field. - use-spacer-as-expanded-child
use_spacer_as_expanded_childrecommendedflutterFlags an
Expandedwrapping an emptySizedBoxorContainerthat should be aSpacer.