Skip to content

Commit 0b41ef1

Browse files
srawlinsCommit Queue
authored andcommitted
lint rules: Migrate tests for two rules:
* unawaited_futures * void_checks Change-Id: I7746c3f66d178a84fc8996dac09d53efc664d14a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394560 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Auto-Submit: Samuel Rawlins <[email protected]>
1 parent d18d32d commit 0b41ef1

File tree

4 files changed

+283
-258
lines changed

4 files changed

+283
-258
lines changed

pkg/linter/test/rules/unawaited_futures_test.dart

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,128 @@ class UnawaitedFuturesTest extends LintRuleTest {
1717
@override
1818
String get lintRule => LintNames.unawaited_futures;
1919

20+
test_classImplementsFuture() async {
21+
// https://github.com/dart-lang/linter/issues/2211
22+
await assertDiagnostics(r'''
23+
void f(Future2 p) async {
24+
g(p);
25+
}
26+
Future2 g(Future2 p) => p;
27+
abstract class Future2 implements Future {}
28+
''', [
29+
lint(28, 5),
30+
]);
31+
}
32+
33+
test_functionCall_assigned() async {
34+
await assertNoDiagnostics(r'''
35+
Future<int> f() async {
36+
var x = g();
37+
return x;
38+
}
39+
Future<int> g() => Future.value(0);
40+
''');
41+
}
42+
43+
test_functionCall_awaited() async {
44+
await assertNoDiagnostics(r'''
45+
void f() async {
46+
await g();
47+
}
48+
Future<int> g() => Future.value(0);
49+
''');
50+
}
51+
52+
test_functionCall_inListContext() async {
53+
await assertNoDiagnostics(r'''
54+
void f() async {
55+
var x = [g()];
56+
x..[0] = g();
57+
}
58+
Future<int> g() => Future.value(0);
59+
''');
60+
}
61+
62+
test_functionCall_interpolated_unawaited() async {
63+
await assertDiagnostics(r'''
64+
void f() async {
65+
'${g()}';
66+
}
67+
Future<int> g() => Future.value(0);
68+
''', [
69+
lint(22, 3),
70+
]);
71+
}
72+
73+
test_functionCall_returnedWithFutureType() async {
74+
await assertNoDiagnostics(r'''
75+
void f() async {
76+
<String, Future>{}.putIfAbsent('foo', () => g());
77+
}
78+
Future<int> g() => Future.value(0);
79+
''');
80+
}
81+
82+
test_functionCall_unawaited() async {
83+
await assertDiagnostics(r'''
84+
void f() async {
85+
g();
86+
}
87+
Future<int> g() => Future.value(0);
88+
''', [
89+
lint(19, 4),
90+
]);
91+
}
92+
93+
test_functionCallInCascade_assignment() async {
94+
await assertNoDiagnostics(r'''
95+
void f() async {
96+
C()..futureField = g();
97+
}
98+
Future<int> g() => Future.value(0);
99+
class C {
100+
Future<int>? futureField;
101+
}
102+
''');
103+
}
104+
105+
test_functionCallInCascade_inAsync() async {
106+
await assertDiagnostics(r'''
107+
void f() async {
108+
C()..doAsync();
109+
}
110+
class C {
111+
Future<void> doAsync() async {}
112+
}
113+
''', [
114+
lint(22, 11),
115+
]);
116+
}
117+
118+
test_functionCallInCascade_indexAssignment() async {
119+
await assertNoDiagnostics(r'''
120+
void f() async {
121+
C()
122+
..x?[0] = g();
123+
}
124+
Future<int> g() => Future.value(0);
125+
class C {
126+
List<Future<void>>? x = [];
127+
}
128+
''');
129+
}
130+
131+
test_functionCallInCascade_inSync() async {
132+
await assertNoDiagnostics(r'''
133+
void foo() {
134+
C()..doAsync();
135+
}
136+
class C {
137+
Future<void> doAsync() async {}
138+
}
139+
''');
140+
}
141+
20142
test_undefinedIdentifier() async {
21143
await assertDiagnostics(r'''
22144
f() async {

pkg/linter/test/rules/void_checks_test.dart

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ class VoidChecksTest extends LintRuleTest {
1717
@override
1818
String get lintRule => LintNames.void_checks;
1919

20+
test_assert_blockBody_returnStatement() async {
21+
await assertNoDiagnostics(r'''
22+
void f() {
23+
assert(() {
24+
return true;
25+
}());
26+
}
27+
''');
28+
}
29+
2030
test_constructorArgument_genericParameter() async {
2131
await assertDiagnostics(r'''
2232
void f(dynamic p) {
@@ -126,6 +136,28 @@ void m([void v]) {}
126136
]);
127137
}
128138

139+
test_functionExpression_blockBody_returnStatement_genericContext() async {
140+
await assertNoDiagnostics(r'''
141+
generics_with_function() {
142+
g<T>(T Function() p) => p();
143+
g(() {
144+
return 1;
145+
});
146+
}
147+
''');
148+
}
149+
150+
test_functionExpression_blockBody_returnStatement_voidContext() async {
151+
await assertNoDiagnostics(r'''
152+
void f() {
153+
void g(Function p) {}
154+
g(() async {
155+
return 1;
156+
});
157+
}
158+
''');
159+
}
160+
129161
// https://github.com/dart-lang/linter/issues/2685
130162
test_functionType_FutureOrVoidReturnType_Never() async {
131163
await assertNoDiagnostics(r'''
@@ -168,6 +200,93 @@ void f() {
168200
''');
169201
}
170202

203+
test_futureOrVoidField_assignDynamic() async {
204+
await assertNoDiagnostics(r'''
205+
import 'dart:async';
206+
void f(A a, dynamic p) {
207+
a.x = p; // OK
208+
}
209+
class A {
210+
FutureOr<void> x;
211+
A(this.x);
212+
}
213+
''');
214+
}
215+
216+
test_futureOrVoidField_assignFutureOrVoid() async {
217+
await assertNoDiagnostics(r'''
218+
import 'dart:async';
219+
void f(A a, FutureOr<void> p) {
220+
a.x = p;
221+
}
222+
class A {
223+
FutureOr<void> x;
224+
A(this.x);
225+
}
226+
''');
227+
}
228+
229+
test_futureOrVoidField_assignFutureVoid() async {
230+
await assertNoDiagnostics(r'''
231+
import 'dart:async';
232+
void f(A a) {
233+
a.x = Future.value();
234+
}
235+
class A {
236+
FutureOr<void> x;
237+
A(this.x);
238+
}
239+
''');
240+
}
241+
242+
test_futureOrVoidField_assignInt() async {
243+
await assertDiagnostics(r'''
244+
import 'dart:async';
245+
void f(A a) {
246+
a.x = 1;
247+
}
248+
class A {
249+
FutureOr<void> x;
250+
A(this.x);
251+
}
252+
''', [
253+
lint(37, 7),
254+
]);
255+
}
256+
257+
test_futureOrVoidField_assignNull() async {
258+
await assertNoDiagnostics(r'''
259+
import 'dart:async';
260+
void f(A a) {
261+
a.x = null; // OK
262+
}
263+
class A {
264+
FutureOr<void> x;
265+
A(this.x);
266+
}
267+
''');
268+
}
269+
270+
test_futureOrVoidFunction_blockBody_returnsFuture() async {
271+
await assertNoDiagnostics(r'''
272+
import 'dart:async';
273+
FutureOr<void> f() {
274+
return Future.value();
275+
}
276+
''');
277+
}
278+
279+
test_futureOrVoidFunction_blockBody_returnStatement() async {
280+
await assertDiagnostics(r'''
281+
import 'dart:async';
282+
FutureOr<void> f() {
283+
return 1;
284+
}
285+
''', [
286+
lint(44, 9),
287+
]);
288+
}
289+
171290
test_listPattern_local() async {
172291
await assertDiagnostics(r'''
173292
void f() {
@@ -190,6 +309,15 @@ void f(void p) {
190309
]);
191310
}
192311

312+
test_localFunction_emptyBlockBody_matchingFutureOrVoidSignature() async {
313+
await assertNoDiagnostics(r'''
314+
import 'dart:async';
315+
void f(FutureOr<void> Function() p) {
316+
p = () {};
317+
}
318+
''');
319+
}
320+
193321
test_neverReturningCallbackThrows() async {
194322
await assertNoDiagnostics(r'''
195323
import 'dart:async';
@@ -204,6 +332,15 @@ void f() async {
204332
''');
205333
}
206334

335+
test_nonVoidFunction_assignedToVoidFunction() async {
336+
await assertNoDiagnostics(r'''
337+
void f(void Function() p) {
338+
int g() => 1;
339+
p = g;
340+
}
341+
''');
342+
}
343+
207344
test_recordPattern() async {
208345
await assertDiagnostics(r'''
209346
void f(void p) {
@@ -260,4 +397,28 @@ class A<T> {
260397
lint(33, 7),
261398
]);
262399
}
400+
401+
test_voidFunction_blockBody_returnStatement() async {
402+
await assertDiagnostics(r'''
403+
void f(dynamic p) {
404+
return p;
405+
}
406+
''', [
407+
lint(22, 9),
408+
]);
409+
}
410+
411+
test_voidFunction_blockBody_returnStatement_empty() async {
412+
await assertNoDiagnostics(r'''
413+
void f() {
414+
return;
415+
}
416+
''');
417+
}
418+
419+
test_voidFunction_expressionBody() async {
420+
await assertNoDiagnostics(r'''
421+
void f() => 7;
422+
''');
423+
}
263424
}

0 commit comments

Comments
 (0)