Type test negated with ! instead of the is! operator
ID |
dart.correctness.prefer_is_not |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Idiom |
Language |
Dart |
Description
Reports a type test written as !(value is Type) where Dart provides the dedicated is! operator.
Rationale
The two forms mean the same thing, but the negation reads in the wrong order: the ! sits at the front, several tokens away from the is it applies to, so the reader has to reach the end of the parenthesised expression before knowing the test was inverted. In a compound condition that distance grows, and a misplaced or dropped parenthesis silently changes which operand is negated while still compiling. is! puts the negation on the operator itself, where it cannot be detached and cannot be misread.
The following code illustrates the pattern detected by this rule:
class Auditor {
bool reject(Object value) {
// FLAGGED: Type test negated with ! instead of the is! operator
return !(value is String);
}
}