type-init-formals
Same as type_init_formals in Dart lints
- Group
- Style
- Path
lint/style/type-init-formals- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 444 of 512
Flags a redundant type annotation on an initializing formal or super
parameter (type-init-formals, adopted from package:lints).
In C(int this.x) or C(int super.x) the annotation restates a type the
field (or super-parameter) already fixes, so it adds nothing and risks
drifting out of sync with that declaration. It is redundant only when it
names the same type: annotating a narrower one — num x; C(int this.x); —
is legal Dart that meaningfully restricts the constructor's parameter, and
upstream (nodeType.type == field.type) does not report it.
Falcon limitation: this rule compares written types after whitespace normalization, not semantic type equality. Target types come from explicitly declared file-local fields and non-primary superclass constructors, including forwarded super parameters. Primary-constructor fields and extension-type representation fields are not visited. Missing or untyped targets are skipped. Superclass lookup uses only the final name segment, so a prefixed superclass can be confused with a local declaration. Likewise, identically spelled type parameters from different scopes can compare equal without representing the same type. A reported annotation is therefore not always safe to remove.
Invalid
// 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);
}
// Named super parameter whose super constructor states the type outright.
class NamedBase {
final String label;
NamedBase({required this.label});
}
class NamedDerived extends NamedBase {
NamedDerived({required String super.label});
} double y; A(int this.x, double this.y); double y; A(int this.x, double this.y);class Derived extends Base { Derived(int super.a); num size; C(String this.name); C(String this.name); C.sized(num this.size); bool flag; D(bool this.flag);class NamedDerived extends NamedBase { NamedDerived({required String super.label});Valid
// 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);
}
// A *narrowing* type annotation on an initializing formal is not redundant: it
// restricts the constructor's parameter below the field's declared type.
class Narrowing {
num x;
Object label;
Narrowing(int this.x);
Narrowing.labelled(String this.label);
}
// The same narrowing applies to super parameters.
class WideBase {
final num a;
final Object b;
WideBase(this.a, {required this.b});
}
class NarrowDerived extends WideBase {
NarrowDerived(int super.a, {required String super.b});
}
// A pattern guard puts `this.` right after a type-like token without any
// initializing formal being involved.
class Guarded {
bool flag;
Guarded(this.flag);
String describe(Object value) {
switch (value) {
case String s when this.flag:
return s;
case int i when this.flag:
return '$i';
default:
return '';
}
}
}Configure type-init-formals
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"type-init-formals": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/type-init-formals: <reason>More style rules
- unawaited-futures
unawaited_futuresrecommendedFutureresults inasyncbodies must be awaited or markedunawaited. - unintended-html-in-doc-comment
unintended_html_in_doc_commentrecommendedFlags angle-bracket text in doc comments that Markdown would render as an HTML tag.
- unnecessary-await-in-return
unnecessary_await_in_returnAvoid
awaiton a returnedFuturethe function's ownFuturealready covers. - unnecessary-brace-in-string-interps
unnecessary_brace_in_string_interpsrecommendedFlags
${x}string interpolations whose braces can be dropped. - unnecessary-breaks
unnecessary_breaksrecommendedFlags a trailing
breakin a switch case that already ends implicitly. - unnecessary-constructor-name
unnecessary_constructor_namerecommendedFlags a redundant
.newon constructor declarations and invocations. - unnecessary-final
unnecessary_finalDon't use
finalfor local variables and parameters. - unnecessary-late
unnecessary_laterecommendedFlags a redundant
latemodifier on an initialized static or top-level variable.