You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The operator is has a left operand whose static type is Type.
Description
Warn when an expression of the form t is T is encountered where the static type of t is Type.
Details
Emit a warning when an expression of the form t is T is encountered where the static type of t is Type.
The is operator will decide whether the left operand is an object whose run-time type is a subtype of the right operand, and it is essentially always wrong to pass a type literal as the left hand operand, or indeed any expression of type Type.
It seems likely that such an expression was intended to test whether the left operand as a type is a subtype of the right operand. Many terms denoting a type will also be accepted as the left operand of is because they are expressions as well as types, syntactically. This means that the construct is very likely to be wrong, but developers will not get any compile-time errors or warnings about these expressions, except for this lint.
There is no Dart operator (or any other mechanism) that directly tests the run-time subtype relationship between two terms derived from <type>, but the subtype query is a query which can be expressed indirectly (e.g., <S>[] is List<T> will evaluate to true if and only if S is a subtype of T).
Kind
This lint guards against errors by flagging expressions whose semantics is almost certainly very different from the author's intentions.
Bad Examples
voidf<X>(X x) {
// Wrong: `X is int` is simply another way to write `false`.if (Xisint) { ... }
}
Good Examples
voidg<X>(X x) {
// Right: `is` expects an object on the left, not a type.if (x isint) x.isEven;
}
Discussion
It is actually possible to create a situation where t is T may be a useful query even though t is or could be a type, but it is expected to be quite rare. In this case, T would probably always be Type, because t is Object is rather useless (t != null is the preferred way to test the same thing), and t is U where U is a top type is just another way to write true, and any other value for T would just yield false whenever t is indeed a type.
It is reasonable to expect that such rare cases can be marked using // ignore: operator_is_tests_a_type.
Discussion checklist
List any existing rules this proposal modifies, complements, overlaps or conflicts with. I could not find any such rules.
_If there's any prior art (e.g., in other linters), please add references here.
If this proposal corresponds to [Effective Dart] or [Flutter Style Guide] advice, please call it out. As far as I can see, the style guides do not currently cover this topic.
If this proposal is motivated by real-world examples, please provide as many details as you can.
The text was updated successfully, but these errors were encountered:
operator_is_tests_a_type
The operator
is
has a left operand whose static type isType
.Description
Warn when an expression of the form
t is T
is encountered where the static type oft
isType
.Details
Emit a warning when an expression of the form
t is T
is encountered where the static type oft
isType
.The
is
operator will decide whether the left operand is an object whose run-time type is a subtype of the right operand, and it is essentially always wrong to pass a type literal as the left hand operand, or indeed any expression of typeType
.It seems likely that such an expression was intended to test whether the left operand as a type is a subtype of the right operand. Many terms denoting a type will also be accepted as the left operand of
is
because they are expressions as well as types, syntactically. This means that the construct is very likely to be wrong, but developers will not get any compile-time errors or warnings about these expressions, except for this lint.There is no Dart operator (or any other mechanism) that directly tests the run-time subtype relationship between two terms derived from
<type>
, but the subtype query is a query which can be expressed indirectly (e.g.,<S>[] is List<T>
will evaluate to true if and only ifS
is a subtype ofT
).Kind
This lint guards against errors by flagging expressions whose semantics is almost certainly very different from the author's intentions.
Bad Examples
Good Examples
Discussion
It is actually possible to create a situation where
t is T
may be a useful query even thought
is or could be a type, but it is expected to be quite rare. In this case,T
would probably always beType
, becauset is Object
is rather useless (t != null
is the preferred way to test the same thing), andt is U
whereU
is a top type is just another way to writetrue
, and any other value forT
would just yieldfalse
whenevert
is indeed a type.It is reasonable to expect that such rare cases can be marked using
// ignore: operator_is_tests_a_type
.Discussion checklist
The text was updated successfully, but these errors were encountered: