null-check-on-nullable-type-parameter
Same as null_check_on_nullable_type_parameter in Dart lints
- Group
- Correctness
- Path
lint/correctness/null-check-on-nullable-type-parameter- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 301 of 512
Flags a null assertion x! on a nullable type parameter T? whose
expected type is T or T? for a potentially nullable T, and
null-assert patterns whose matched value is a nullable type parameter.
Diagnostics underline only the terminal ! of either syntax-tree assertion node.
As in the analyzer's expected-type helper, only these positions have an expected type:
- a returned or yielded value (also awaited) of a function declaration
with a declared return type (an
asyncfunction'sFutureargument), or of a closure or untyped local function whose block body's returns inferTorT?; methods have none; - a variable's initializer: its declared type, and for an untyped local or
forvariable the type inferred fromx!itself; - the value (also awaited) of a plain
=assignment, exceptx = x!anda.b = a.b!; - the right operand of an operator method, and an argument, including
constructor-initializer
super(...)/this(...)arguments; an awaited right operand or positional argument has none; - an element of a list or set literal, a key or value of a map literal
(through the map's collection
ifandforelements); an awaited or parenthesized map key expects the map's value type.
Conditional branches, ?? operands, cascade targets, records, and
patterns have none. Inferred types never keep the T & Object
intersection of x!. Flow analysis promotes a local after its x!.
Invalid
T force<T>(T? value) {
T result = value!;
T assigned;
assigned = value!;
return result;
}
T returnForce<T>(T? value) {
return value!;
}
void pattern<T>(T? value) {
if (value case var item!) {
print(item);
}
}
class Holder<T> {
T field;
T force(T? value) => value!;
void assign(Holder<T> self, T? value) {
self.field = value!;
}
}
T closureContexts<T>(T? value) {
final T Function(T?) callback = (T? inner) => inner!;
final T Function<T>(T?) generic = <T>(T? inner) => inner!;
callback(value);
generic<T>(value);
return value!;
}
class PatternBox<T> {
PatternBox(this.value);
final T? value;
}
void nestedPatterns<T>(
(T?,) record,
List<T?> list,
Map<String, T?> map,
PatternBox<T> box,
) {
if (record case (var item!,)) {
print(item);
}
if (list case [var item!]) {
print(item);
}
if (map case {'x': var item!}) {
print(item);
}
if (box case PatternBox(value: var item!)) {
print(item);
}
}
void acceptGeneric<T>(T Function() callback) {}
void explicitCallType<T>(T? value) {
acceptGeneric<T>(() => value!);
}
T? keepNullable<T>(T? value) => value!;
void nullableAssignment<T>(T? value) {
T? result;
result = value!;
print(result);
}
class Entry<T> {
T? key;
}
void compare<T>(int Function(T, T) comparator, T key, Entry<T> entry) {
comparator(key, entry.key!);
}
T assignment<T>(T? value) {
T result;
result = value!;
return result;
}T force<T>(T? value) { T result = value!;T returnForce<T>(T? value) { return value!;void pattern<T>(T? value) { if (value case var item!) { void assign(Holder<T> self, T? value) { self.field = value!; generic<T>(value); return value!;) { if (record case (var item!,)) { } if (list case [var item!]) { } if (map case {'x': var item!}) { } if (box case PatternBox(value: var item!)) { T? keepNullable<T>(T? value) => value!; T? result; result = value!;void compare<T>(int Function(T, T) comparator, T key, Entry<T> entry) { comparator(key, entry.key!); T result; result = value!;Valid
String forceString(String? value) => value!;
void concretePattern(String? value) {
if (value case var item!) {
print(item);
}
}
class T {}
T classNamedT(T? value) => value!;
T outerReturnDoesNotLeak<T>(T? value) {
final int Function() callback = () => value!;
callback();
return value;
}
T namedLocalDoesNotInheritContext<T>(T? value) {
int local() => value!;
local();
return value;
}
T bounded<T extends Object>(T? value) => value!;
class Generic<T> {
T force(T? value) => value!;
}
void inferredClosure<T>(T? value) {
final T Function(T?) callback = (T? inner) => inner!;
final T Function<T>(T?) generic = <T>(T? inner) => inner!;
callback(value);
generic<T>(value);
}
T checkedAssignment<T>(T? value) {
value!;
T result;
result = value!;
return result;
}
void selfAssignment<T>(T? value) {
value = value!;
}Configure null-check-on-nullable-type-parameter
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"null-check-on-nullable-type-parameter": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/null-check-on-nullable-type-parameter: <reason>More correctness rules
- null-closures
null_closuresrecommendedReject literal
nullin the closure slots of curated SDK APIs. - prefer-correct-json-casts
Decode JSON containers using their actual runtime generic types.
- prefer-define-hero-tagflutter
Require an explicit heroTag on Flutter
FloatingActionButtonconstructors. - prefer-expect-latertest
Requires expectLater when a canonical expect call receives an unawaited Future.
- prefer-immutable-bloc-eventsrecommendedbloc
Flags Bloc event classes without an explicit @immutable annotation.
- prefer-immutable-bloc-staterecommendedbloc
Flags Bloc state classes without an explicit @immutable annotation.
- prefer-immutable-provider-argumentsriverpod
Requires stable value equality for freshly created Riverpod family keys.
- prefer-read-for-methodsrecommendedprovider
Flags void method actions on a listening Provider lookup during build.