Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

fix: add check for supertypes #775

Merged
merged 2 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* fix: add check for supertypes for [`avoid-non-null-assertions`](https://dartcodemetrics.dev/docs/rules/common/avoid-non-null-assertion) rule.
* fix: cover more cases in [`prefer-immediate-return`](https://dartcodemetrics.dev/docs/rules/common/prefer-immediate-return) rule
* fix: support index expressions for [`no-magic-number`](https://dartcodemetrics.dev/docs/rules/common/no-magic-number) rule.
* chore: restrict `analyzer` version to `>=2.4.0 <3.5.0`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/type.dart';

import '../../../../../utils/node_utils.dart';
import '../../../lint_utils.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ class _Visitor extends RecursiveAstVisitor<void> {
if (operand is IndexExpression) {
final type = operand.target?.staticType;

return type != null && type.isDartCoreMap;
return type is InterfaceType &&
(_isMapOrSubclassOfMap(type) ||
type.allSupertypes.any(_isMapOrSubclassOfMap));
}

return false;
}

bool _isMapOrSubclassOfMap(DartType? type) =>
type != null && type.isDartCoreMap;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,19 @@ class Test {
object!.method(); // LINT

object?.method();

final myMap = MyMap<String, String>();
myMap['key']!.contains('other');

final myOtherMap = MyAnotherMap<String, String>();
myOtherMap['key']!.contains('other');
}
}

class MyMap<K, V> extends Map<K, V> {
void doNothing() {}
}

class MyAnotherMap<K, V> implements Map<K, V> {
void doNothing() {}
}