Rules / Style / prefer-is-not-operator
prefer-is-not-operator
Flags a negated type test !(x is T) that should use the is! operator.
Dart provides the is! operator precisely so negated type checks read
directly as x is! T instead of wrapping the test in parentheses and negating
it, which is easier to misread and to accidentally mis-parenthesize. The rule
fires on a logical-not applied to a non-negated is expression; a test that
already uses is! produces no negation to flag.
Invalid
example.dartdart
void f(Object x) {
if (!(x is int)) {}
var a = !(x is String);
final b = !(x is List<int>);
final c = !(x is double);
print(!(x is bool));
print('$a $b $c');
}
Prefer using the 'is!' operator.
1void f(Object x) {
2 if (!(x is int)) {}
∙
Prefer using the 'is!' operator.
2 if (!(x is int)) {}
3 var a = !(x is String);
∙
Prefer using the 'is!' operator.
3 var a = !(x is String);
4 final b = !(x is List<int>);
∙
Prefer using the 'is!' operator.
4 final b = !(x is List<int>);
5 final c = !(x is double);
∙
Prefer using the 'is!' operator.
5 final c = !(x is double);
6 print(!(x is bool));
∙
Valid
example.dartdart
void f(Object x) {
if (x is! int) {}
var a = x is String;
final b = !(x is! double);
var c = !someBool(x);
print(x is bool);
print('$a $b $c');
}
bool someBool(Object x) => true;
How to configure
Set the severity of prefer-is-not-operator in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"prefer-is-not-operator": "error"
}
}
}
}