cast-nullable-to-non-nullable

Same as cast_nullable_to_non_nullable in Dart lints

Group
Style
Path
lint/style/cast-nullable-to-non-nullable
Default
Off, opt in
Origin
Dart lints
Rule
200 of 512

Flags e as T casts of a nullable value to a non-nullable type.

Casting a nullable expression straight to a non-nullable type hides the null check inside the cast; the null assertion operator ! states the intent directly. The cast is reported only when the operand's static type is proven nullable (never dynamic or unresolved) and the target type is proven non-nullable (not a potentially nullable type parameter, dynamic, void, Null, or FutureOr of a nullable type). Operand types include the promotions proven on the current control-flow path.

String? s;
var a = s as String; // flagged
var b = s!;          // ok

Invalid

example.dartdart
class A {}

class B extends A {}

String? maybeString() => null;

int? maybeInt() => null;

A? maybeA() => null;

void f(String? s, int? count, List<int>? list) {
  var a = s as String;
  var b = count as int;
  var c = list as List<int>;
  var d = maybeString() as String;
  var e = maybeA() as B;
  var g = maybeInt() as num;
  print([a, b, c, d, e, g]);
}

Object g(A? input) => input as Object;

// Promoting sites only guard uses within the same declaration.
void checksElsewhere(Object? val) {
  if (val == null) return;
  print(val);
}

void castHere(Object? val) {
  print(val as String);
}

// Comparing with a non-null value does not promote.
void comparedToValue(Object? s) {
  if (s == 'none') return;
  print(s as String);
}

void untypedInitializer(int? p) {
  var copy = p;
  print(copy as int);
}

class Holder {
  final int? _value;
  Holder(this._value);

  int read() => _value as int;

  int checked() {
    if (_value == null) return 0;
    return _value;
  }
}
Don't cast a nullable value to a non-nullable type.
11void f(String? s, int? count, List<int>? list) {
12 var a = s as String;
Don't cast a nullable value to a non-nullable type.
12 var a = s as String;
13 var b = count as int;
Don't cast a nullable value to a non-nullable type.
13 var b = count as int;
14 var c = list as List<int>;
Don't cast a nullable value to a non-nullable type.
14 var c = list as List<int>;
15 var d = maybeString() as String;
Don't cast a nullable value to a non-nullable type.
15 var d = maybeString() as String;
16 var e = maybeA() as B;
Don't cast a nullable value to a non-nullable type.
16 var e = maybeA() as B;
17 var g = maybeInt() as num;
Don't cast a nullable value to a non-nullable type.
20
21Object g(A? input) => input as Object;
Don't cast a nullable value to a non-nullable type.
29void castHere(Object? val) {
30 print(val as String);
Don't cast a nullable value to a non-nullable type.
35 if (s == 'none') return;
36 print(s as String);
Don't cast a nullable value to a non-nullable type.
40 var copy = p;
41 print(copy as int);
Don't cast a nullable value to a non-nullable type.
47
48 int read() => _value as int;

Valid

example.dartdart
class A {}

class B extends A {}

String? maybeString() => null;

A? maybeA() => null;

void f(String s, String? n, dynamic d, Object o) {
  var a = n!;
  var b = s as String;
  var c = n as String?;
  var e = d as String;
  var g = o as String;
  var h = maybeA() as B?;
  var i = maybeString() as dynamic;
  var j = maybeString() as Object?;
  print([a, b, c, e, g, h, i, j]);
}

void promoted(String? s) {
  if (s != null) {
    print(s as String);
  }
}

T generic<T>(Object? value) => value as T;

void initialized() {
  int? maybeInt = 5;
  final int nonNull = maybeInt as int;
  print(nonNull);
}

Configure cast-nullable-to-non-nullable

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "cast-nullable-to-non-nullable": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/cast-nullable-to-non-nullable: <reason>