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

Commit 24f3636

Browse files
committed
fix: fix regression in is! checks for avoid-unnecessary-type-assertions
1 parent e869ce4 commit 24f3636

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 4.18.1
4+
5+
* fix: fix regression in is! checks for [`avoid-unnecessary-type-assertions`](https://dartcodemetrics.dev/docs/rules/common/avoid-unnecessary-type-assertions).
6+
* chore: restrict `analyzer` version to `>=4.4.0 <4.8.0`.
7+
38
## 4.18.0
49

510
* feat: support passing file paths to all commands.

lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_unnecessary_type_assertions/visitor.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ class _Visitor extends RecursiveAstVisitor<void> {
4444
final castedType = node.type.type;
4545

4646
if (node.notOperator != null) {
47-
if (_isUselessTypeCheck(castedType, objectType, true)) {
47+
if (objectType != null &&
48+
!objectType.isDynamic &&
49+
!objectType.isDartCoreObject &&
50+
_isUselessTypeCheck(castedType, objectType, true)) {
4851
_expressions[node] =
4952
'${node.isOperator.keyword?.lexeme ?? ''}${node.notOperator ?? ''}';
5053
}

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ environment:
1010
sdk: ">=2.17.0 <3.0.0"
1111

1212
dependencies:
13-
analyzer: ">=4.1.0 <4.8.0"
14-
analyzer_plugin: ">=0.11.0 <0.12.0"
13+
analyzer: ">=4.3.0 <4.8.0"
14+
analyzer_plugin: ">=0.11.1 <0.12.0"
1515
ansicolor: ^2.0.1
1616
args: ^2.0.0
1717
collection: ^1.15.0

test/src/analyzers/lint_analyzer/rules/rules_list/avoid_unnecessary_type_assertions/examples/example_with_not_is.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ class Cat extends HomeAnimal {}
1111
void checkSameOrInheritor() {
1212
final cat = Cat();
1313

14-
final check = cat is! Cat; // Lint
14+
final check = cat is! Cat; // LINT
1515
final check = cat is Dog;
1616
final check = cat is! Dog; // LINT
17-
final check = cat is! NotAnimal; // Lint
17+
final check = cat is! NotAnimal; // LINT
1818

1919
final Cat? nullableCat = null;
2020

2121
final check = nullableCat is! Cat;
22+
23+
final dynamic a;
24+
final check = a is! int;
25+
26+
final Object b;
27+
final check = b is! int;
2228
}

0 commit comments

Comments
 (0)