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
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;
}
}void f(String? s, int? count, List<int>? list) { var a = s as String; var a = s as String; var b = count as int; var b = count as int; var c = list as List<int>; var c = list as List<int>; var d = maybeString() as String; var d = maybeString() as String; var e = maybeA() as B; var e = maybeA() as B; var g = maybeInt() as num; Object g(A? input) => input as Object;void castHere(Object? val) { print(val as String); if (s == 'none') return; print(s as String); var copy = p; print(copy as int); int read() => _value as int;Valid
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.
{
"linter": {
"rules": {
"style": {
"cast-nullable-to-non-nullable": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/cast-nullable-to-non-nullable: <reason>More style rules
- class-members-ordering
class_members_orderingFlags class members declared out of a canonical order.
- combinators-ordering
combinators_orderingFlags
show/hidecombinators whose names are not sorted alphabetically. - conditional-uri-does-not-exist
conditional_uri_does_not_existrecommendedRequire the targets of conditional import and export URIs to exist.
- constant-identifier-names
constant_identifier_namesrecommendedFlags constant identifiers that are not lowerCamelCase.
- curly-braces-in-flow-control-structures
curly_braces_in_flow_control_structuresrecommendedRequires curly braces around the bodies of flow-control statements.
- dangling-library-doc-comments
dangling_library_doc_commentsrecommendedAttach library documentation to an explicit
librarydirective. - deprecated-consistency
deprecated_consistencyrecommendedFlags deprecations that are not carried through to related declarations.
- directives-ordering
directives_orderingFlags import and export directives that break the Effective Dart ordering.