Rules / Style / prefer-iterable-where-type
prefer-iterable-where-type
Flags .where((e) => e is T), which .whereType<T>() expresses directly.
Filtering an iterable with an is test is exactly what Iterable.whereType
does, but the getter also narrows the element type, yielding an
Iterable<T> instead of leaving the original element type in place. Using
whereType<T>() is shorter, avoids the throwaway closure, and removes the
casts or checks that a bare where filter would still require downstream.
The rule matches a single-argument .where call whose sole positional
argument is a one-parameter arrow closure of the form param is T — the
test must be non-negated and applied to the closure's own parameter.
Invalid
example.dartdart
void bad() {
var a = items.where((e) => e is String);
var b = list.where((x) => x is int);
var c = things.where((t) => t is Widget);
var d = values.where((v) => v is double).toList();
var e = data.where((item) => item is Map);
}
Use 'whereType<T>()' instead of 'where' with an 'is' check.
1void bad() {
2 var a = items.where((e) => e is String);
∙
Use 'whereType<T>()' instead of 'where' with an 'is' check.
2 var a = items.where((e) => e is String);
3 var b = list.where((x) => x is int);
∙
Use 'whereType<T>()' instead of 'where' with an 'is' check.
3 var b = list.where((x) => x is int);
4 var c = things.where((t) => t is Widget);
∙
Use 'whereType<T>()' instead of 'where' with an 'is' check.
4 var c = things.where((t) => t is Widget);
5 var d = values.where((v) => v is double).toList();
∙
Use 'whereType<T>()' instead of 'where' with an 'is' check.
5 var d = values.where((v) => v is double).toList();
6 var e = data.where((item) => item is Map);
∙
Valid
example.dartdart
void good() {
var a = items.whereType<String>();
var b = list.where((e) => e.isValid);
var c = things.where((e) => e is! String);
var d = values.where((e) => e is int && e > 0);
var e = data.where((a, b) => a is int);
var f = items.map((e) => e is String);
}
How to configure
Set the severity of prefer-iterable-where-type in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"prefer-iterable-where-type": "error"
}
}
}
}