@@ -24,18 +24,96 @@ class UseBuildContextSynchronouslyTest extends LintRuleTest {
24
24
@override
25
25
String get testPackageRootPath => '$workspaceRootPath /lib' ;
26
26
27
+ test_await_afterReferenceToContextInBody () async {
28
+ await assertNoDiagnostics (r'''
29
+ import 'package:flutter/widgets.dart';
30
+
31
+ void foo(BuildContext context) async {
32
+ Navigator.of(context);
33
+ await f();
34
+ }
35
+
36
+ Future<void> f() async {}
37
+ ''' );
38
+ }
39
+
40
+ test_await_beforeReferenceToContextInBody () async {
41
+ await assertDiagnostics (r'''
42
+ import 'package:flutter/widgets.dart';
43
+
44
+ void foo(BuildContext context) async {
45
+ await f();
46
+ Navigator.of(context);
47
+ }
48
+
49
+ Future<void> f() async {}
50
+ ''' , [
51
+ lint (94 , 21 ),
52
+ ]);
53
+ }
54
+
55
+ test_awaitBeforeIf_mountedExitGuardInIfConditionWithOr_beforeReferenceToContext () async {
56
+ await assertNoDiagnostics (r'''
57
+ import 'package:flutter/widgets.dart';
58
+
59
+ void foo(BuildContext context) async {
60
+ await f();
61
+ if (c || !mounted) return;
62
+ Navigator.of(context);
63
+ }
64
+
65
+ bool mounted = false;
66
+ Future<void> f() async {}
67
+ bool get c => true;
68
+ ''' );
69
+ }
70
+
71
+ test_awaitBeforeIf_mountedGuardInIfCondition_referenceToContextInIfBody () async {
72
+ await assertNoDiagnostics (r'''
73
+ import 'package:flutter/widgets.dart';
74
+
75
+ void foo(BuildContext context) async {
76
+ await f();
77
+ if (mounted) {
78
+ Navigator.of(context);
79
+ }
80
+ }
81
+
82
+ bool mounted = false;
83
+ Future<void> f() async {}
84
+ ''' );
85
+ }
86
+
87
+ test_awaitBeforeIf_mountedGuardInIfConditionWithAnd_referenceToContextInIfBody () async {
88
+ await assertNoDiagnostics (r'''
89
+ import 'package:flutter/widgets.dart';
90
+
91
+ void foo(BuildContext context) async {
92
+ await f();
93
+ if (c && mounted) {
94
+ Navigator.of(context);
95
+ }
96
+ }
97
+
98
+ bool mounted = false;
99
+ Future<void> f() async {}
100
+ bool get c => true;
101
+ ''' );
102
+ }
103
+
27
104
test_awaitBeforeIfStatement_beforeReferenceToContext () async {
28
105
await assertDiagnostics (r'''
29
106
import 'package:flutter/widgets.dart';
30
107
31
- void foo(BuildContext context, Future<bool> condition ) async {
32
- var b = await condition ;
108
+ void foo(BuildContext context) async {
109
+ var b = await c() ;
33
110
if (b) {
34
111
Navigator.of(context);
35
112
}
36
113
}
114
+ Future<bool> c() async => true;
37
115
''' , [
38
- lint (145 , 21 ),
116
+ lint (115 , 21 ),
39
117
]);
40
118
}
41
119
@@ -44,15 +122,36 @@ void foo(BuildContext context, Future<bool> condition) async {
44
122
await assertNoDiagnostics (r'''
45
123
import 'package:flutter/widgets.dart';
46
124
47
- void foo(BuildContext context, Future<bool> condition ) async {
48
- await condition ;
125
+ void foo(BuildContext context) async {
126
+ await c() ;
49
127
f1(() {
50
128
f2(context);
51
129
});
52
130
}
53
131
54
132
void f1(Function f) {}
55
133
void f2(BuildContext c) {}
134
+ Future<bool> c() async => true;
135
+ ''' );
136
+ }
137
+
138
+ test_awaitBeforeSwitch_mountedExitGuardInCase_beforeReferenceToContext () async {
139
+ await assertNoDiagnostics (r'''
140
+ import 'package:flutter/widgets.dart';
141
+
142
+ void foo(BuildContext context) async {
143
+ await f();
144
+
145
+ switch ('') {
146
+ case 'a':
147
+ if (!mounted) {
148
+ break;
149
+ }
150
+ Navigator.of(context);
151
+ }
152
+ }
153
+ bool mounted = false;
154
+ Future<void> f() async {}
56
155
''' );
57
156
}
58
157
@@ -61,27 +160,28 @@ void f2(BuildContext c) {}
61
160
await assertDiagnostics (r'''
62
161
import 'package:flutter/widgets.dart';
63
162
64
- void foo(BuildContext context, Future<bool> condition ) async {
65
- if (await condition ) {
163
+ void foo(BuildContext context) async {
164
+ if (await c() ) {
66
165
Navigator.of(context);
67
166
}
68
167
}
69
-
168
+ Future<bool> c() async => true;
70
169
''' , [
71
- lint (132 , 21 ),
170
+ lint (102 , 21 ),
72
171
]);
73
172
}
74
173
75
174
test_awaitInIfCondition_beforeReferenceToContext () async {
76
175
await assertDiagnostics (r'''
77
176
import 'package:flutter/widgets.dart';
78
177
79
- void foo(BuildContext context, Future<bool> condition ) async {
80
- if (await condition ) return;
178
+ void foo(BuildContext context) async {
179
+ if (await c() ) return;
81
180
Navigator.of(context);
82
181
}
182
+ Future<bool> c() async => true;
83
183
''' , [
84
- lint (136 , 21 ),
184
+ lint (106 , 21 ),
85
185
]);
86
186
}
87
187
@@ -104,13 +204,14 @@ void foo(
104
204
await assertNoDiagnostics (r'''
105
205
import 'package:flutter/widgets.dart';
106
206
107
- void foo(BuildContext context, Future<bool> condition ) async {
207
+ void foo(BuildContext context) async {
108
208
Navigator.of(context);
109
209
if (1 == 2) {
110
- await condition ;
210
+ await c() ;
111
211
return;
112
212
}
113
213
}
214
+ Future<bool> c() async => true;
114
215
''' );
115
216
}
116
217
@@ -120,114 +221,141 @@ void foo(BuildContext context, Future<bool> condition) async {
120
221
await assertNoDiagnostics (r'''
121
222
import 'package:flutter/widgets.dart';
122
223
123
- void foo(BuildContext context, Future<bool> condition ) async {
224
+ void foo(BuildContext context) async {
124
225
if (1 == 2) {
125
- await condition ;
226
+ await c() ;
126
227
return;
127
228
}
128
229
Navigator.of(context);
129
230
}
231
+ Future<bool> c() async => true;
130
232
''' );
131
233
}
132
234
133
235
test_awaitInIfThenAndExitInElse_afterReferenceToContext () async {
134
236
await assertNoDiagnostics (r'''
135
237
import 'package:flutter/widgets.dart';
136
238
137
- void foo(BuildContext context, Future<bool> condition ) async {
239
+ void foo(BuildContext context) async {
138
240
Navigator.of(context);
139
241
if (1 == 2) {
140
- await condition ;
242
+ await c() ;
141
243
} else {
142
- await condition ;
244
+ await c() ;
143
245
return;
144
246
}
145
247
}
248
+ Future<bool> c() async => true;
146
249
''' );
147
250
}
148
251
149
252
test_awaitInIfThenAndExitInElse_beforeReferenceToContext () async {
150
253
await assertDiagnostics (r'''
151
254
import 'package:flutter/widgets.dart';
152
255
153
- void foo(BuildContext context, Future<bool> condition ) async {
256
+ void foo(BuildContext context) async {
154
257
if (1 == 2) {
155
- await condition ;
258
+ await c() ;
156
259
} else {
157
- await condition ;
260
+ await c() ;
158
261
return;
159
262
}
160
263
Navigator.of(context);
161
264
}
265
+ Future<bool> c() async => true;
162
266
''' , [
163
- lint (190 , 21 ),
267
+ lint (154 , 21 ),
164
268
]);
165
269
}
166
270
271
+ @FailingTest (reason: 'Logic not implemented yet.' )
167
272
test_awaitInWhileBody_afterReferenceToContext () async {
273
+ await assertDiagnostics (r'''
274
+ import 'package:flutter/widgets.dart';
275
+
276
+ void foo(BuildContext context) async {
277
+ while (true) {
278
+ // OK the first time only!
279
+ Navigator.of(context);
280
+ await f();
281
+ }
282
+ }
283
+
284
+ bool mounted = false;
285
+ Future<void> f() async {}
286
+ ''' , [
287
+ lint (149 , 21 ),
288
+ ]);
289
+ }
290
+
291
+ test_awaitInWhileBody_afterReferenceToContextOutsideWait () async {
168
292
await assertNoDiagnostics (r'''
169
293
import 'package:flutter/widgets.dart';
170
294
171
- void foo(BuildContext context, Future<void> condition ) async {
295
+ void foo(BuildContext context) async {
172
296
Navigator.of(context);
173
297
while (true) {
174
- await condition ;
298
+ await f() ;
175
299
break;
176
300
}
177
301
}
302
+ Future<void> f() async {}
178
303
''' );
179
304
}
180
305
181
306
test_awaitInWhileBody_beforeReferenceToContext () async {
182
307
await assertDiagnostics (r'''
183
308
import 'package:flutter/widgets.dart';
184
309
185
- void foo(BuildContext context, Future<void> condition ) async {
310
+ void foo(BuildContext context) async {
186
311
while (true) {
187
- await condition ;
312
+ await f() ;
188
313
break;
189
314
}
190
315
Navigator.of(context);
191
316
}
317
+ Future<void> f() async {}
192
318
''' , [
193
- lint (158 , 21 ),
319
+ lint (128 , 21 ),
194
320
]);
195
321
}
196
322
197
323
test_awaitThenExitInIfThenAndElse_afterReferenceToContext () async {
198
324
await assertNoDiagnostics (r'''
199
325
import 'package:flutter/widgets.dart';
200
326
201
- void foo(BuildContext context, Future<bool> condition ) async {
327
+ void foo(BuildContext context) async {
202
328
Navigator.of(context);
203
329
if (1 == 2) {
204
- await condition ;
330
+ await c() ;
205
331
return;
206
332
} else {
207
- await condition ;
333
+ await c() ;
208
334
return;
209
335
}
210
336
}
337
+ Future<bool> c() async => true;
211
338
''' );
212
339
}
213
340
214
341
test_awaitThenExitInIfThenAndElse_beforeReferenceToContext () async {
215
342
await assertDiagnostics (r'''
216
343
import 'package:flutter/widgets.dart';
217
344
218
- void foo(BuildContext context, Future<bool> condition ) async {
345
+ void foo(BuildContext context) async {
219
346
if (1 == 2) {
220
- await condition ;
347
+ await c() ;
221
348
return;
222
349
} else {
223
- await condition ;
350
+ await c() ;
224
351
return;
225
352
}
226
353
Navigator.of(context);
227
354
}
355
+ Future<bool> c() async => true;
228
356
''' , [
229
357
// No lint.
230
- error (WarningCode .DEAD_CODE , 202 , 22 ),
358
+ error (WarningCode .DEAD_CODE , 166 , 22 ),
231
359
]);
232
360
}
233
361
0 commit comments