This repository was archived by the owner on Jul 16, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +22
-1
lines changed
lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_non_null_assertion
test/src/analyzers/lint_analyzer/rules/rules_list/avoid_non_null_assertion/examples Expand file tree Collapse file tree 4 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 22
33## Unreleased
44
5+ * fix: add check for supertypes for [ ` avoid-non-null-assertions ` ] ( https://dartcodemetrics.dev/docs/rules/common/avoid-non-null-assertion ) rule.
56* fix: cover more cases in [ ` prefer-immediate-return ` ] ( https://dartcodemetrics.dev/docs/rules/common/prefer-immediate-return ) rule
67* fix: support index expressions for [ ` no-magic-number ` ] ( https://dartcodemetrics.dev/docs/rules/common/no-magic-number ) rule.
78* chore: restrict ` analyzer ` version to ` >=2.4.0 <3.5.0 ` .
Original file line number Diff line number Diff line change 33import 'package:analyzer/dart/ast/ast.dart' ;
44import 'package:analyzer/dart/ast/token.dart' ;
55import 'package:analyzer/dart/ast/visitor.dart' ;
6+ import 'package:analyzer/dart/element/type.dart' ;
67
78import '../../../../../utils/node_utils.dart' ;
89import '../../../lint_utils.dart' ;
Original file line number Diff line number Diff line change @@ -19,9 +19,14 @@ class _Visitor extends RecursiveAstVisitor<void> {
1919 if (operand is IndexExpression ) {
2020 final type = operand.target? .staticType;
2121
22- return type != null && type.isDartCoreMap;
22+ return type is InterfaceType &&
23+ (_isMapOrSubclassOfMap (type) ||
24+ type.allSupertypes.any (_isMapOrSubclassOfMap));
2325 }
2426
2527 return false ;
2628 }
29+
30+ bool _isMapOrSubclassOfMap (DartType ? type) =>
31+ type != null && type.isDartCoreMap;
2732}
Original file line number Diff line number Diff line change @@ -27,5 +27,19 @@ class Test {
2727 object! .method (); // LINT
2828
2929 object? .method ();
30+
31+ final myMap = MyMap <String , String >();
32+ myMap['key' ]! .contains ('other' );
33+
34+ final myOtherMap = MyAnotherMap <String , String >();
35+ myOtherMap['key' ]! .contains ('other' );
3036 }
3137}
38+
39+ class MyMap <K , V > extends Map <K , V > {
40+ void doNothing () {}
41+ }
42+
43+ class MyAnotherMap <K , V > implements Map <K , V > {
44+ void doNothing () {}
45+ }
You can’t perform that action at this time.
0 commit comments