avoid-unnecessary-local-variable
- Group
- Style
- Path
lint/style/avoid-unnecessary-local-variable- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 167 of 512
Reports a single-use local used only to initialize the next local. Both declarations must be eager and inferred: transferring the initializer then preserves evaluation order and its type-inference context.
Invalid
void a() {
var first = 1;
var second = first;
print(second);
}
void b() {
final first = Object();
final second = first;
print(second);
}
void c() {
final first = [1, 2];
final second = first;
print(second);
}
void d() {
final first = true;
final second = (first);
print(second);
}
void e() {
final first = DateTime.now();
var second = first;
print(second);
}void a() { var first = 1;void b() { final first = Object();void c() { final first = [1, 2];void d() { final first = true;void e() { final first = DateTime.now();Valid
Object immediateReturn() {
final value = Object();
return value;
}
void twice() {
final first = Object();
final second = first;
print(first);
print(second);
}
void typed() {
final num first = 1;
final second = first;
print(second);
}
void typedAlias() {
final first = [1];
final List<num> second = first;
print(second);
}
void deferred() {
late final first = Object();
final second = first;
print(second);
}
void separated() {
final first = Object();
print('intervening');
final second = first;
print(second);
}
void captured() {
var first = Object();
final second = first;
void change() { first = Object(); }
print(second);
}
void thrown() {
final error = Exception();
throw error;
}Configure avoid-unnecessary-local-variable
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-unnecessary-local-variable": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-unnecessary-local-variable: <reason>More style rules
- avoid-unnecessary-negations
Simplifies negated bool literals and equality comparisons with a logical negation on both sides.
- avoid-unnecessary-overrides-in-stateflutter
Redundant Flutter State overrides that drop inherited
@protected. - avoid-unnecessary-reassignment
Reports an eager local initializer immediately overwritten without a read.
- avoid-unnecessary-return
Flags bare returns at the end of a function or its final if/block branches.
- avoid-unnecessary-super
Omit implicit
super()initializers and provably redundant member prefixes. - avoid-unused-constructor-parameters
avoid_unused_constructor_parametersFlags constructor parameters that are never used.
- avoid-void-async
avoid_void_asyncrecommendedAvoid
asyncfunctions and methods declared to returnvoid. - avoid-wrapping-in-paddingflutter
Flags Padding directly wrapping a Flutter Container that can own the padding.