Skip to content

Commit 6719bc6

Browse files
authored
Use dart_flutter_team_lints for matcher (#2426)
Change the dependency and analysis options include to the stricter team configuration. Fix newly surfaced diagnostics. Most code changes are related to the strict modes that are enabled by the team config. Fix most implicit casts with explicit casts, but fix some arguments in `typedMatches` implementation so they don't unnecessarily widen the type back to `dynamic` before using the value. Bump to Dart 3.5 The min SDK for the test runner is already at 3.5.0 and matcher is only usable with the latest version of the test runner.
1 parent dc0f8ea commit 6719bc6

35 files changed

+188
-237
lines changed

.github/workflows/dart.yml

+86-148
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/matcher/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 0.12.18-wip
22

33
* Remove some dynamic invocations.
4+
* Add explicit casts from `dynamic` values.
5+
* Require Dart 3.5
46

57
## 0.12.17
68

pkgs/matcher/analysis_options.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include: package:lints/recommended.yaml
1+
include: package:dart_flutter_team_lints/analysis_options.yaml
22

33
linter:
44
rules:

pkgs/matcher/lib/expect.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ export 'src/expect/prints_matcher.dart' show prints;
3838
export 'src/expect/stream_matcher.dart' show StreamMatcher;
3939
export 'src/expect/stream_matchers.dart'
4040
show
41-
emitsDone,
4241
emits,
43-
emitsError,
44-
mayEmit,
4542
emitsAnyOf,
46-
emitsInOrder,
43+
emitsDone,
44+
emitsError,
4745
emitsInAnyOrder,
46+
emitsInOrder,
4847
emitsThrough,
48+
mayEmit,
4949
mayEmitMultiple,
5050
neverEmits;
5151
export 'src/expect/throws_matcher.dart' show Throws, throws, throwsA;

pkgs/matcher/lib/mirror_matchers.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class _HasProperty extends Matcher {
7373
.add('has property "$_name" with value ')
7474
.addDescriptionOf(matchState['value']);
7575
var innerDescription = StringDescription();
76-
matchState['state'] ??= {};
76+
matchState['state'] ??= <Object?, Object?>{};
7777
_matcher?.describeMismatch(matchState['value'], innerDescription,
7878
matchState['state'] as Map, verbose);
7979
if (innerDescription.length > 0) {

pkgs/matcher/lib/src/core_matchers.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class _Empty extends Matcher {
1414
const _Empty();
1515

1616
@override
17-
bool matches(Object? item, Map matchState) => (item as dynamic).isEmpty;
17+
bool matches(Object? item, Map matchState) =>
18+
(item as dynamic).isEmpty as bool;
1819

1920
@override
2021
Description describe(Description description) => description.add('empty');
@@ -27,7 +28,8 @@ class _NotEmpty extends Matcher {
2728
const _NotEmpty();
2829

2930
@override
30-
bool matches(Object? item, Map matchState) => (item as dynamic).isNotEmpty;
31+
bool matches(Object? item, Map matchState) =>
32+
(item as dynamic).isNotEmpty as bool;
3133

3234
@override
3335
Description describe(Description description) => description.add('non-empty');

pkgs/matcher/lib/src/equals_matcher.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class _DeepMatcher extends Matcher {
167167
Object? expected, Object? actual, String location, int depth) {
168168
// If the expected value is a matcher, try to match it.
169169
if (expected is Matcher) {
170-
var matchState = {};
170+
var matchState = <Object?, Object?>{};
171171
if (expected.matches(actual, matchState)) return null;
172172
return _Mismatch(location, actual, (description, verbose) {
173173
var oldLength = description.length;

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ Future expectLater(dynamic actual, dynamic matcher,
7474

7575
/// The implementation of [expect] and [expectLater].
7676
Future _expect(Object? actual, Object? matcher,
77-
{String? reason, skip, bool verbose = false, ErrorFormatter? formatter}) {
77+
{String? reason,
78+
Object? skip,
79+
bool verbose = false,
80+
ErrorFormatter? formatter}) {
7881
final test = TestHandle.current;
7982
formatter ??= (actual, matcher, reason, matchState, verbose) {
8083
var mismatchDescription = StringDescription();
@@ -133,7 +136,7 @@ Future _expect(Object? actual, Object? matcher,
133136
return Future.sync(() {});
134137
}
135138

136-
var matchState = {};
139+
var matchState = <Object?, Object?>{};
137140
try {
138141
if ((matcher as Matcher).matches(actual, matchState)) {
139142
return Future.sync(() {});

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,17 @@ class _ExpectedFunction<T> {
124124
/// Returns a function that has the same number of positional arguments as the
125125
/// wrapped function (up to a total of 6).
126126
Function get func {
127-
if (_callback is Function(Never, Never, Never, Never, Never, Never)) {
127+
if (_callback is void Function(Never, Never, Never, Never, Never, Never)) {
128128
return max6;
129129
}
130-
if (_callback is Function(Never, Never, Never, Never, Never)) return max5;
131-
if (_callback is Function(Never, Never, Never, Never)) return max4;
132-
if (_callback is Function(Never, Never, Never)) return max3;
133-
if (_callback is Function(Never, Never)) return max2;
134-
if (_callback is Function(Never)) return max1;
135-
if (_callback is Function()) return max0;
130+
if (_callback is void Function(Never, Never, Never, Never, Never)) {
131+
return max5;
132+
}
133+
if (_callback is void Function(Never, Never, Never, Never)) return max4;
134+
if (_callback is void Function(Never, Never, Never)) return max3;
135+
if (_callback is void Function(Never, Never)) return max2;
136+
if (_callback is void Function(Never)) return max1;
137+
if (_callback is void Function()) return max0;
136138

137139
_outstandingWork?.complete();
138140
throw ArgumentError(
@@ -219,7 +221,8 @@ class _ExpectedFunction<T> {
219221
@Deprecated('Will be removed in 0.13.0')
220222
Function expectAsync(Function callback,
221223
{int count = 1, int max = 0, String? id, String? reason}) =>
222-
_ExpectedFunction(callback, count, max, id: id, reason: reason).func;
224+
_ExpectedFunction<Object?>(callback, count, max, id: id, reason: reason)
225+
.func;
223226

224227
/// Informs the framework that the given [callback] of arity 0 is expected to be
225228
/// called [count] number of times (by default 1).
@@ -415,7 +418,8 @@ Func6<T, A, B, C, D, E, F> expectAsync6<T, A, B, C, D, E, F>(
415418
@Deprecated('Will be removed in 0.13.0')
416419
Function expectAsyncUntil(Function callback, bool Function() isDone,
417420
{String? id, String? reason}) =>
418-
_ExpectedFunction(callback, 0, -1, id: id, reason: reason, isDone: isDone)
421+
_ExpectedFunction<Object?>(callback, 0, -1,
422+
id: id, reason: reason, isDone: isDone)
419423
.func;
420424

421425
/// Informs the framework that the given [callback] of arity 0 is expected to be

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class _Completes extends AsyncMatcher {
5858
result = await _matcher.matchAsync(value) as String?;
5959
if (result == null) return null;
6060
} else {
61-
var matchState = {};
61+
var matchState = <Object?, Object?>{};
6262
if (_matcher.matches(value, matchState)) return null;
6363
result = _matcher
6464
.describeMismatch(value, StringDescription(), matchState, false)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class _Prints extends AsyncMatcher {
3333
// synchronous.
3434
@override
3535
dynamic /*FutureOr<String>*/ matchAsync(Object? item) {
36-
if (item is! Function()) return 'was not a unary Function';
36+
if (item is! Object? Function()) return 'was not a unary Function';
3737

3838
var buffer = StringBuffer();
3939
var result = runZoned(item,
@@ -53,7 +53,7 @@ class _Prints extends AsyncMatcher {
5353
/// Verifies that [actual] matches [_matcher] and returns a [String]
5454
/// description of the failure if it doesn't.
5555
String? _check(String actual) {
56-
var matchState = {};
56+
var matchState = <Object?, Object?>{};
5757
if (_matcher.matches(actual, matchState)) return null;
5858

5959
var result = _matcher

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class _StreamMatcher extends AsyncMatcher implements StreamMatcher {
155155
.listen(events.add, onDone: () => events.add(null));
156156

157157
// Wait on a timer tick so all buffered events are emitted.
158-
await Future.delayed(Duration.zero);
158+
await Future<void>.delayed(Duration.zero);
159159
_unawaited(subscription.cancel());
160160

161161
var eventsString = events.map((event) {

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ StreamMatcher emits(Object? matcher) {
3434
return StreamMatcher((queue) async {
3535
if (!await queue.hasNext) return '';
3636

37-
var matchState = {};
37+
var matchState = <Object?, Object?>{};
3838
var actual = await queue.next;
3939
if (wrapped.matches(actual, matchState)) return null;
4040

@@ -138,7 +138,7 @@ StreamMatcher emitsAnyOf(Iterable matchers) {
138138
if (consumedMost == null) {
139139
transaction.reject();
140140
if (firstError != null) {
141-
await Future.error(firstError!, firstStackTrace);
141+
await Future<Never>.error(firstError!, firstStackTrace);
142142
}
143143

144144
var failureMessages = <String>[];
@@ -368,7 +368,9 @@ Future<bool> _tryInAnyOrder(
368368

369369
if (consumedMost == null) {
370370
transaction.reject();
371-
if (firstError != null) await Future.error(firstError!, firstStackTrace);
371+
if (firstError != null) {
372+
await Future<Never>.error(firstError!, firstStackTrace);
373+
}
372374
return false;
373375
} else {
374376
transaction.commit(consumedMost!);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ class Throws extends AsyncMatcher {
116116

117117
/// Verifies that [error] matches [_matcher] and returns a [String]
118118
/// description of the failure if it doesn't.
119-
String? _check(error, StackTrace? trace) {
119+
String? _check(Object error, StackTrace? trace) {
120120
if (_matcher == null) return null;
121121

122-
var matchState = {};
122+
var matchState = <Object?, Object?>{};
123123
if (_matcher.matches(error, matchState)) return null;
124124

125125
var result = _matcher

pkgs/matcher/lib/src/map_matchers.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class _ContainsValue extends Matcher {
1616
@override
1717
bool matches(Object? item, Map matchState) =>
1818
// ignore: avoid_dynamic_calls
19-
(item as dynamic).containsValue(_value);
19+
(item as dynamic).containsValue(_value) as bool;
2020
@override
2121
Description describe(Description description) =>
2222
description.add('contains value ').addDescriptionOf(_value);
@@ -36,7 +36,7 @@ class _ContainsMapping extends Matcher {
3636
@override
3737
bool matches(Object? item, Map matchState) =>
3838
// ignore: avoid_dynamic_calls
39-
(item as dynamic).containsKey(_key) &&
39+
((item as dynamic).containsKey(_key) as bool) &&
4040
_valueMatcher.matches((item as dynamic)[_key], matchState);
4141

4242
@override

pkgs/matcher/lib/src/operator_matchers.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class _AllOf extends Matcher {
5959
Map matchState, bool verbose) {
6060
var matcher = matchState['matcher'] as Matcher;
6161
matcher.describeMismatch(
62-
item, mismatchDescription, matchState['state'], verbose);
62+
item, mismatchDescription, matchState['state'] as Map, verbose);
6363
return mismatchDescription;
6464
}
6565

pkgs/matcher/lib/src/order_matchers.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ class _OrderingMatcher extends Matcher {
7878
bool matches(Object? item, Map matchState) {
7979
if (item == _value) {
8080
return _equalValue;
81-
} else if ((item as dynamic) < _value) {
81+
} else if ((item as dynamic) < _value as bool) {
8282
return _lessThanValue;
83-
} else if ((item as dynamic) > _value) {
83+
} else if ((item as dynamic) > _value as bool) {
8484
return _greaterThanValue;
8585
} else {
8686
return false;

pkgs/matcher/lib/src/string_matchers.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class _IsEqualIgnoringWhitespace extends FeatureMatcher<String> {
6161
description.addDescriptionOf(_matchValue).add(' ignoring whitespace');
6262

6363
@override
64-
Description describeTypedMismatch(dynamic item,
64+
Description describeTypedMismatch(String item,
6565
Description mismatchDescription, Map matchState, bool verbose) {
6666
return mismatchDescription
6767
.add('is ')
@@ -152,7 +152,7 @@ class _MatchesRegExp extends FeatureMatcher<String> {
152152
: throw ArgumentError('matches requires a regexp or string');
153153

154154
@override
155-
bool typedMatches(dynamic item, Map matchState) => _regexp.hasMatch(item);
155+
bool typedMatches(String item, Map matchState) => _regexp.hasMatch(item);
156156

157157
@override
158158
Description describe(Description description) =>

pkgs/matcher/lib/src/util.dart

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ Matcher wrapMatcher(Object? valueOrMatcher) {
4343
} else if (valueOrMatcher is bool Function(Never)) {
4444
// unary predicate, but expects a specific type
4545
// so wrap it.
46-
// ignore: unnecessary_lambdas
47-
return predicate((a) => (valueOrMatcher as dynamic)(a));
46+
return predicate((a) => (valueOrMatcher as dynamic)(a) as bool);
4847
} else {
4948
return equals(valueOrMatcher);
5049
}

pkgs/matcher/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: >-
66
repository: https://github.com/dart-lang/test/tree/master/pkgs/matcher
77

88
environment:
9-
sdk: ^3.4.0
9+
sdk: ^3.5.0
1010

1111
dependencies:
1212
async: ^2.10.0
@@ -16,8 +16,8 @@ dependencies:
1616
test_api: ">=0.5.0 <0.8.0"
1717

1818
dev_dependencies:
19+
dart_flutter_team_lints: ^3.2.0
1920
fake_async: ^1.3.0
20-
lints: ^3.0.0
2121
test: ^1.23.0
2222

2323
dependency_overrides:

pkgs/matcher/test/core_matchers_test.dart

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:matcher/matcher.dart';
6-
import 'package:test/test.dart' show test, group;
6+
import 'package:test/test.dart' show group, test;
77

88
import 'test_utils.dart';
99

@@ -42,22 +42,22 @@ void main() {
4242
});
4343

4444
test('same', () {
45-
var a = {};
46-
var b = {};
45+
var a = <Object?, Object?>{};
46+
var b = <Object?, Object?>{};
4747
shouldPass(a, same(a));
4848
shouldFail(b, same(a), 'Expected: same instance as {} Actual: {}');
4949
});
5050

5151
test('equals', () {
52-
var a = {};
53-
var b = {};
52+
var a = <Object?, Object?>{};
53+
var b = <Object?, Object?>{};
5454
shouldPass(a, equals(a));
5555
shouldPass(a, equals(b));
5656
});
5757

5858
test('equals with null', () {
5959
Object? a; // null
60-
var b = {};
60+
var b = <Object?, Object?>{};
6161
shouldPass(a, equals(a));
6262
shouldFail(
6363
a, equals(b), 'Expected: {} Actual: <null> Which: expected a map');
@@ -87,7 +87,7 @@ void main() {
8787
});
8888

8989
test('anything', () {
90-
var a = {};
90+
var a = <Object?, Object?>{};
9191
shouldPass(0, anything);
9292
shouldPass(null, anything);
9393
shouldPass(a, anything);
@@ -107,8 +107,8 @@ void main() {
107107
});
108108

109109
test('hasLength', () {
110-
var a = {};
111-
var b = [];
110+
var a = <Object?, Object?>{};
111+
var b = <Object?>[];
112112
shouldPass(a, hasLength(0));
113113
shouldPass(b, hasLength(0));
114114
shouldPass('a', hasLength(1));
@@ -173,13 +173,13 @@ void main() {
173173
['foo', 'bar'],
174174
['foo'],
175175
3,
176-
[]
176+
<Object?>[]
177177
];
178178
var expected1 = [
179179
['foo', 'bar'],
180180
['foo'],
181181
4,
182-
[]
182+
<Object?>[]
183183
];
184184
var reason1 = "Expected: [['foo', 'bar'], ['foo'], 4, []] "
185185
"Actual: [['foo', 'bar'], ['foo'], 3, []] "
@@ -189,13 +189,13 @@ void main() {
189189
['foo', 'barry'],
190190
['foo'],
191191
4,
192-
[]
192+
<Object?>[]
193193
];
194194
var expected2 = [
195195
['foo', 'bar'],
196196
['foo'],
197197
4,
198-
[]
198+
<Object?>[]
199199
];
200200
var reason2 = "Expected: [['foo', 'bar'], ['foo'], 4, []] "
201201
"Actual: [['foo', 'barry'], ['foo'], 4, []] "

pkgs/matcher/test/expect_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void main() {
1818
});
1919

2020
test('contains an async error', () {
21-
expect(expectLater(Future.error('oh no'), completion(isFalse)),
21+
expect(expectLater(Future<Never>.error('oh no'), completion(isFalse)),
2222
throwsA('oh no'));
2323
});
2424
});

0 commit comments

Comments
 (0)