tighten-type-of-initializing-formals
Same as tighten_type_of_initializing_formals in Dart lints
- Group
- Style
- Path
lint/style/tighten-type-of-initializing-formals- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 442 of 512
Flags a nullable this.x or super.x initializing formal whose constructor
asserts x != null.
When a constructor's initializer list asserts that an initializing formal is
not null, the parameter should declare a non-nullable type instead, so the
check happens at compile time. The assert must be assert(x != null) or
assert(null != x) with x naming a this.x or super.x formal of the same
constructor whose type is nullable: the explicit parameter type, the field's
declared type for an untyped this.x (an untyped field without initializer
is dynamic, which counts as nullable), or the super constructor's parameter
type for an untyped super.x. A trailing ? or dynamic proves
nullability even when the named type itself is unresolved; otherwise
unresolved types are never reported. The analyzer treats an undefined type
(a compile error) as nullable and reports it, but Falcon cannot tell an
undefined type from one declared in a library it does not load.
Parenthesized conditions or operands are not the analyzer's direct binary
identifier/null test. Repeated asserts on one formal produce one diagnostic.
class A {
String? p;
A(this.p) : assert(p != null); // flagged
A.typed(String this.p); // ok
}
Invalid
class A {
String? p;
A(this.p) : assert(p != null);
A.reversed(this.p) : assert(null != p);
A.typed(String? this.p) : assert(p != null);
A.named({this.p}) : assert(p != null);
}
class B {
int? x;
int? y;
B(
this.x,
this.y,
) : assert(x != null),
assert(y != null);
}
class Base {
final String? name;
Base(this.name);
}
class Derived extends Base {
Derived(super.name) : assert(name != null);
}
class Untyped {
var value;
Untyped(this.value) : assert(value != null);
}
class UnresolvedNullable {
Missing? value;
UnresolvedNullable(this.value) : assert(value != null);
} String? p; A(this.p) : assert(p != null); A(this.p) : assert(p != null); A.reversed(this.p) : assert(null != p); A.reversed(this.p) : assert(null != p); A.typed(String? this.p) : assert(p != null); A.typed(String? this.p) : assert(p != null); A.named({this.p}) : assert(p != null); B( this.x, this.x, this.y,class Derived extends Base { Derived(super.name) : assert(name != null); var value; Untyped(this.value) : assert(value != null); Missing? value; UnresolvedNullable(this.value) : assert(value != null);Valid
class A {
String? p;
A.typed(String this.p);
A.other(this.p);
A.unrelated(this.p, String? q) : assert(q != null);
A.nonNull(String this.p) : assert(p != null);
A.otherCheck(this.p) : assert(p == null);
}
class B {
final int x;
B(this.x) : assert(x != null);
}
class C {
final String? s;
C(String? value) : s = value, assert(value != null);
}
class Base {
final String name;
Base(this.name);
}
class Derived extends Base {
Derived(super.name) : assert(name != null);
}
class Inferred {
var value = 0;
Inferred(this.value) : assert(value != null);
}Configure tighten-type-of-initializing-formals
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"tighten-type-of-initializing-formals": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/tighten-type-of-initializing-formals: <reason>More style rules
- type-annotate-public-apis
type_annotate_public_apisrecommendedRequires type annotations on public declarations.
- type-init-formals
type_init_formalsrecommendedFlags a *redundant* type annotation on an initializing formal or super parameter (
type-init-formals, adopted from package:lints). - 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.