avoid-unnecessary-reassignment
- Group
- Style
- Path
lint/style/avoid-unnecessary-reassignment- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 170 of 512
Reports an eager local initializer immediately overwritten without a read. An explicit declared type preserves inference when the assignment moves. Only effect-free primitive/string initializers are discardable. Branching if/else overwrites are deliberately excluded without control-flow proof.
Invalid
void a(int next) {
int x = 1;
x = next;
print(x);
}
void b(bool next) {
bool x = false;
x = next;
print(x);
}
void c(double next) {
double x = 1.5;
x = next;
print(x);
}
void d(Object next) {
Object? x = null;
x = next;
print(x);
}
void e(bool flag) {
int x = 0;
x = flag ? 1 : 2;
print(x);
}
void stringInitializer(String next) {
String alpha = '';
alpha = next;
print(alpha);
} int x = 1; x = next; bool x = false; x = next; double x = 1.5; x = next; Object? x = null; x = next; int x = 0; x = flag ? 1 : 2; String alpha = ''; alpha = next;Valid
void read() { int x = 1; print(x); x = 2; }
void call() { Object x = Object(); x = Object(); }
void lateLocal() { late int x = 1; x = 2; }
void compound() { int x = 1; x += 2; }
void dependsOnOld() { int x = 1; x = x + 2; }
void selfAssignment() { int x = 1; x = x; }
void capture() {
Object? x = null;
x = () => x;
print(x);
}
class C {
int get value => 1;
set value(int x) {}
void field() { value = 2; }
}
void shadow() {
var x = 1;
void nested(int x) { x = 2; }
nested(x);
}
void inferredDynamic() {
var x = null;
x = 1;
x = 'a';
print(x);
}
void inferredFromDynamic(dynamic next) {
var x = 0;
x = next;
print(x);
}
void interpolatedInitializer(Object next) {
String x = '$next';
x = 'replacement';
print(x);
}
void branchOverwrite(bool flag) {
int x = 0;
if (flag) { x = 1; } else { x = 2; }
print(x);
}Configure avoid-unnecessary-reassignment
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"avoid-unnecessary-reassignment": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/avoid-unnecessary-reassignment: <reason>More style rules
- 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.
- binary-expression-operand-order
binary-expression-operand-orderFlags comparison expressions with a literal on the left-hand side.
- boolean-prefixes
boolean_prefixesFlags boolean identifiers that lack a conventional interrogative prefix.
- camel-case-extensions
camel_case_extensionsrecommendedFlags named extensions whose name is not
UpperCamelCase.