@@ -28,26 +28,30 @@ class TypeCast {
2828 this .isReversed = false ,
2929 });
3030
31- /// Returns the first type from source's supertypes
32- /// which is corresponding to target or null
33- DartType ? castTypeInHierarchy () {
31+ /// Checks that type checking is unnecessary
32+ bool get isUnnecessaryTypeCheck {
33+ final source = this .source;
34+
35+ if (_isNullableCompatibility) {
36+ return false ;
37+ }
38+
3439 if (source.element == target.element) {
35- return source ;
40+ return _areGenericsWithSameTypeArgs ;
3641 }
3742
38- final objectType = source;
39- if (objectType is InterfaceType ) {
40- return objectType.allSupertypes.firstWhereOrNull (
41- (value) => value.element == target.element,
43+ if (source is InterfaceType ) {
44+ return source.allSupertypes.any (
45+ (e) => e.element == target.element,
4246 );
4347 }
4448
45- return null ;
49+ return false ;
4650 }
4751
4852 /// Checks for nullable type casts
4953 /// Only one case `Type? is Type` always valid assertion case.
50- bool get isNullableCompatibility {
54+ bool get _isNullableCompatibility {
5155 final isObjectTypeNullable =
5256 source.nullabilitySuffix != NullabilitySuffix .none;
5357 final isCastedTypeNullable =
@@ -56,33 +60,8 @@ class TypeCast {
5660 return isObjectTypeNullable && ! isCastedTypeNullable;
5761 }
5862
59- /// Checks that type checking is unnecessary
60- /// [source] is the source expression type
61- /// [target] is the type against which the expression type is compared
62- /// and false for positive comparison, i.e. 'is', 'as' or 'whereType'
63- bool get isUnnecessaryTypeCheck {
64- if (isNullableCompatibility) {
65- return false ;
66- }
67-
68- final objectCastedType = castTypeInHierarchy ();
69- if (objectCastedType == null ) {
70- return isReversed;
71- }
72-
73- final objectTypeCast = TypeCast (
74- source: objectCastedType,
75- target: target,
76- );
77- if (! objectTypeCast.areGenericsWithSameTypeArgs) {
78- return false ;
79- }
80-
81- return ! isReversed;
82- }
83-
8463 /// Checks for type arguments and compares them
85- bool get areGenericsWithSameTypeArgs {
64+ bool get _areGenericsWithSameTypeArgs {
8665 if (source is DynamicType || target is DynamicType ) {
8766 return false ;
8867 }
0 commit comments