Rules / Complexity / avoid-unnecessary-type-assertions
avoid-unnecessary-type-assertions
lint/complexity/avoid-unnecessary-type-assertionsrecommended
Same as avoid-unnecessary-type-assertions · Dart Code MetricsFlags an is check whose result is already guaranteed by the declared type.
Testing a variable against a type it is already known to have is always true,
so the check is dead code — drop it, or fix the declaration that makes it
redundant. Without full type resolution the rule tracks the declared types of
non-nullable local variables and class fields, and reports x is T when
x's declared type and T name the same type with matching type arguments.
It targets the affirmative is form; nullable declarations are skipped,
since x is T there still meaningfully narrows away null.
Invalid
example.dartdart
// Test cases for avoid-unnecessary-type-assertions rule
// Flags 'is T' checks where the variable is already known to be T
void testExplicitIntType() {
final int x = 5;
if (x is int) {
print("x is int");
}
}
void testExplicitStringType() {
final String name = "hello";
if (name is String) {
print("name is string");
}
}
void testExplicitListType() {
final List<String> items = [];
if (items is List) {
print("items is list");
}
}
void testMultipleUnnecessaryAssertions() {
final bool active = true;
final double value = 3.14;
if (active is bool) {
print("active is bool");
}
if (value is double) {
print("value is double");
}
}
class MyClass {
final int id = 42;
void checkId() {
if (id is int) {
print("id is int");
}
}
}
void testInlineAssertion() {
final String message = "test";
final result = message is String ? "yes" : "no";
}
Unnecessary type assertion — variable is already known to be this type
5 final int x = 5;
6 if (x is int) {
∙
Unnecessary type assertion — variable is already known to be this type
12 final String name = "hello";
13 if (name is String) {
∙
Unnecessary type assertion — variable is already known to be this type
19 final List<String> items = [];
20 if (items is List) {
∙
Unnecessary type assertion — variable is already known to be this type
28
29 if (active is bool) {
∙
Unnecessary type assertion — variable is already known to be this type
32
33 if (value is double) {
∙
Unnecessary type assertion — variable is already known to be this type
41 void checkId() {
42 if (id is int) {
∙
Unnecessary type assertion — variable is already known to be this type
49 final String message = "test";
50 final result = message is String ? "yes" : "no";
∙
Valid
example.dartdart
// Good examples for avoid-unnecessary-type-assertions rule
// Type checks where the type is not already known
void testDynamicTypeCheck() {
dynamic value = 42;
if (value is int) {
print("value is int");
}
}
void testObjectTypeCheck() {
Object obj = "hello";
if (obj is String) {
print("obj is string");
}
}
void testParameterTypeCheck() {
void checkType(dynamic param) {
if (param is bool) {
print("param is bool");
}
}
}
void testNullableTypeCheck() {
int? maybeInt = 5;
if (maybeInt is int) {
print("maybeInt is int");
}
}
void testBroadTypeCheck() {
List<dynamic> items = [];
if (items is List<String>) {
print("items is List<String>");
}
}
void testInheritanceTypeCheck() {
Object shape = Circle();
if (shape is Circle) {
print("shape is Circle");
}
}
class Circle {}
How to configure
Set the severity of avoid-unnecessary-type-assertions in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"complexity": {
"avoid-unnecessary-type-assertions": "error"
}
}
}
}