Skip to content

Commit 42495c2

Browse files
authored
Enable avoid_dynamic_calls lint (#2410)
Add argument types to `typedMatches` arguments which were unnecessarily widening it back to `dynamic`. Add some missing `as dynamic` and `as Function` which is the pattern the lint uses to allow intentional dynamic calls. Ignore some dynamic calls in a test for a legacy API. Ignore a couple false positives for calling methods (as opposed to getters) on a cast expression. The false positive was fixed in https://dart-review.googlesource.com/c/sdk/+/390640 but not yet published. Remove the deprecated lint `package_api_docs`. Replaces dart-archive/matcher#205
1 parent 9a267de commit 42495c2

14 files changed

+34
-28
lines changed

pkgs/matcher/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.12.18-wip
2+
3+
* Remove some dynamic invocations.
4+
15
## 0.12.17
26

37
* Require Dart 3.4

pkgs/matcher/analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ include: package:lints/recommended.yaml
33
linter:
44
rules:
55
- always_declare_return_types
6+
- avoid_dynamic_calls
67
- avoid_private_typedef_functions
78
- avoid_unused_constructor_parameters
89
- cancel_subscriptions

pkgs/matcher/lib/src/core_matchers.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ class _ReturnsNormally extends FeatureMatcher<Function> {
150150
@override
151151
bool typedMatches(Function f, Map matchState) {
152152
try {
153-
f();
153+
// ignore: unnecessary_cast
154+
(f as Function)();
154155
return true;
155156
} catch (e, s) {
156157
addStateInfo(matchState, {'exception': e, 'stack': s});

pkgs/matcher/lib/src/expect/throws_matcher.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ class Throws extends AsyncMatcher {
8282
}
8383

8484
try {
85-
item as Function;
86-
var value = item();
85+
var value = (item as Function)();
8786
if (value is Future) {
8887
return _matchFuture(value, 'returned a Future that emitted ');
8988
}

pkgs/matcher/lib/src/iterable_matchers.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class _UnorderedMatches extends _IterableMatcher {
204204
.add(' unordered');
205205

206206
@override
207-
Description describeTypedMismatch(dynamic item,
207+
Description describeTypedMismatch(Iterable item,
208208
Description mismatchDescription, Map matchState, bool verbose) =>
209209
mismatchDescription.add(_test(item.toList())!);
210210

pkgs/matcher/lib/src/map_matchers.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'feature_matcher.dart';
56
import 'interfaces.dart';
67
import 'util.dart';
78

89
/// Returns a matcher which matches maps containing the given [value].
910
Matcher containsValue(Object? value) => _ContainsValue(value);
1011

11-
class _ContainsValue extends Matcher {
12+
class _ContainsValue extends FeatureMatcher<Map> {
1213
final Object? _value;
1314

1415
const _ContainsValue(this._value);
1516

1617
@override
17-
bool matches(Object? item, Map matchState) =>
18-
(item as dynamic).containsValue(_value);
18+
bool typedMatches(Map item, Map matchState) => item.containsValue(_value);
1919
@override
2020
Description describe(Description description) =>
2121
description.add('contains value ').addDescriptionOf(_value);
@@ -26,16 +26,15 @@ class _ContainsValue extends Matcher {
2626
Matcher containsPair(Object? key, Object? valueOrMatcher) =>
2727
_ContainsMapping(key, wrapMatcher(valueOrMatcher));
2828

29-
class _ContainsMapping extends Matcher {
29+
class _ContainsMapping extends FeatureMatcher<Map> {
3030
final Object? _key;
3131
final Matcher _valueMatcher;
3232

3333
const _ContainsMapping(this._key, this._valueMatcher);
3434

3535
@override
36-
bool matches(Object? item, Map matchState) =>
37-
(item as dynamic).containsKey(_key) &&
38-
_valueMatcher.matches(item[_key], matchState);
36+
bool typedMatches(Map item, Map matchState) =>
37+
item.containsKey(_key) && _valueMatcher.matches(item[_key], matchState);
3938

4039
@override
4140
Description describe(Description description) {
@@ -47,9 +46,9 @@ class _ContainsMapping extends Matcher {
4746
}
4847

4948
@override
50-
Description describeMismatch(Object? item, Description mismatchDescription,
51-
Map matchState, bool verbose) {
52-
if (!(item as dynamic).containsKey(_key)) {
49+
Description describeTypedMismatch(
50+
Map item, Description mismatchDescription, Map matchState, bool verbose) {
51+
if (!item.containsKey(_key)) {
5352
return mismatchDescription
5453
.add(" doesn't contain key ")
5554
.addDescriptionOf(_key);

pkgs/matcher/lib/src/numeric_matchers.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class _IsCloseTo extends FeatureMatcher<num> {
1818
const _IsCloseTo(this._value, this._delta);
1919

2020
@override
21-
bool typedMatches(dynamic item, Map matchState) {
21+
bool typedMatches(num item, Map matchState) {
2222
var diff = item - _value;
2323
if (diff < 0) diff = -diff;
2424
return diff <= _delta;
@@ -32,8 +32,8 @@ class _IsCloseTo extends FeatureMatcher<num> {
3232
.addDescriptionOf(_value);
3333

3434
@override
35-
Description describeTypedMismatch(dynamic item,
36-
Description mismatchDescription, Map matchState, bool verbose) {
35+
Description describeTypedMismatch(
36+
num item, Description mismatchDescription, Map matchState, bool verbose) {
3737
var diff = item - _value;
3838
if (diff < 0) diff = -diff;
3939
return mismatchDescription.add(' differs by ').addDescriptionOf(diff);
@@ -67,7 +67,7 @@ class _InRange extends FeatureMatcher<num> {
6767
this._low, this._high, this._lowMatchValue, this._highMatchValue);
6868

6969
@override
70-
bool typedMatches(dynamic value, Map matchState) {
70+
bool typedMatches(num value, Map matchState) {
7171
if (value < _low || value > _high) {
7272
return false;
7373
}

pkgs/matcher/lib/src/operator_matchers.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class _AllOf extends Matcher {
5757
@override
5858
Description describeMismatch(dynamic item, Description mismatchDescription,
5959
Map matchState, bool verbose) {
60-
var matcher = matchState['matcher'];
60+
var matcher = matchState['matcher'] as Matcher;
6161
matcher.describeMismatch(
6262
item, mismatchDescription, matchState['state'], verbose);
6363
return mismatchDescription;

pkgs/matcher/lib/src/order_matchers.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class _OrderingMatcher extends Matcher {
8080
return _equalValue;
8181
} else if ((item as dynamic) < _value) {
8282
return _lessThanValue;
83-
} else if (item > _value) {
83+
} else if ((item as dynamic) > _value) {
8484
return _greaterThanValue;
8585
} else {
8686
return false;

pkgs/matcher/lib/src/string_matchers.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class _StringStartsWith extends FeatureMatcher<String> {
8080
const _StringStartsWith(this._prefix);
8181

8282
@override
83-
bool typedMatches(dynamic item, Map matchState) => item.startsWith(_prefix);
83+
bool typedMatches(String item, Map matchState) => item.startsWith(_prefix);
8484

8585
@override
8686
Description describe(Description description) =>
@@ -97,7 +97,7 @@ class _StringEndsWith extends FeatureMatcher<String> {
9797
const _StringEndsWith(this._suffix);
9898

9999
@override
100-
bool typedMatches(dynamic item, Map matchState) => item.endsWith(_suffix);
100+
bool typedMatches(String item, Map matchState) => item.endsWith(_suffix);
101101

102102
@override
103103
Description describe(Description description) =>
@@ -119,7 +119,7 @@ class _StringContainsInOrder extends FeatureMatcher<String> {
119119
const _StringContainsInOrder(this._substrings);
120120

121121
@override
122-
bool typedMatches(dynamic item, Map matchState) {
122+
bool typedMatches(String item, Map matchState) {
123123
var fromIndex = 0;
124124
for (var s in _substrings) {
125125
var index = item.indexOf(s, fromIndex);

0 commit comments

Comments
 (0)