Rules / Style / type-init-formals
type-init-formals
Flags a redundant type annotation on an initializing formal or super parameter.
In C(int this.x) or C(int super.x) the parameter's type is already fixed by
the field (or super-parameter) it initializes, so restating it is redundant and
risks drifting out of sync with the field's declared type. Detection runs over the
raw source because the parser only records a field/super formal when the token is
literally this/super with no preceding type. To stay conservative the rule
flags only a this./super. immediately preceded by a bare type identifier — a
shape that in valid Dart occurs solely in a constructor parameter list — and skips
non-type words such as final, covariant, return, and await.
Invalid
example.dartdart
// Type annotations on initializing formals and super parameters.
class A {
int x;
double y;
A(int this.x, double this.y);
}
class Base {
final int a;
Base(this.a);
}
class Derived extends Base {
Derived(int super.a);
}
class C {
String name;
num size;
C(String this.name);
C.sized(num this.size);
}
class D {
bool flag;
D(bool this.flag);
}
Don't type annotate initializing formals.
4 double y;
5 A(int this.x, double this.y);
∙
Don't type annotate initializing formals.
4 double y;
5 A(int this.x, double this.y);
∙
Don't type annotate initializing formals.
13class Derived extends Base {
14 Derived(int super.a);
∙
Don't type annotate initializing formals.
19 num size;
20 C(String this.name);
∙
Don't type annotate initializing formals.
20 C(String this.name);
21 C.sized(num this.size);
∙
Don't type annotate initializing formals.
25 bool flag;
26 D(bool this.flag);
∙
Valid
example.dartdart
// Initializing formals and super parameters without a redundant type.
class A {
int x;
double y;
A(this.x, this.y);
}
class Base {
final int a;
Base(this.a);
}
class Derived extends Base {
Derived(super.a);
}
class C {
String name;
num size;
C(this.name);
C.sized(this.size);
void use() {
print(this.name);
return this.size.isNaN ? null : null;
}
}
class D {
bool flag;
D(this.flag);
}
How to configure
Set the severity of type-init-formals in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"type-init-formals": "error"
}
}
}
}