Skip to content

Commit dfaf151

Browse files
srawlinsCommit Queue
authored andcommitted
linter: discarded_futures: do not report on await-in-synchronous function
When a synchronous function body has an `await` expression, we report an error. We don't need to _also_ report a lint. The noted error, `CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT`, already is associated with the "add async" fix. Reporting a lint when an error is also definitely reported is just double reporting. Change-Id: If6d260a01cc654c87897480996d3f6f4b61373dd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429063 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 5b4dc61 commit dfaf151

File tree

3 files changed

+13
-60
lines changed

3 files changed

+13
-60
lines changed

pkg/analysis_server/test/src/services/correction/fix/add_async_test.dart

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -549,48 +549,6 @@ Future<void> g() async { }
549549
''');
550550
}
551551

552-
Future<void> test_discardedFuture_awaited() async {
553-
await resolveTestCode('''
554-
void f() {
555-
// ignore: await_in_wrong_context
556-
await g();
557-
}
558-
559-
Future<void> g() async { }
560-
''');
561-
await assertHasFix('''
562-
Future<void> f() async {
563-
// ignore: await_in_wrong_context
564-
await g();
565-
}
566-
567-
Future<void> g() async { }
568-
''');
569-
}
570-
571-
Future<void> test_discardedFuture_awaited_method() async {
572-
await resolveTestCode('''
573-
class C {
574-
void f() {
575-
// ignore: await_in_wrong_context
576-
await g();
577-
}
578-
579-
Future<void> g() async { }
580-
}
581-
''');
582-
await assertHasFix('''
583-
class C {
584-
Future<void> f() async {
585-
// ignore: await_in_wrong_context
586-
await g();
587-
}
588-
589-
Future<void> g() async { }
590-
}
591-
''');
592-
}
593-
594552
Future<void> test_discardedFuture_method() async {
595553
await resolveTestCode('''
596554
class C {

pkg/linter/lib/src/rules/discarded_futures.dart

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,9 @@ class _Visitor extends SimpleAstVisitor<void> {
5050
void visitExpressionStatement(ExpressionStatement node) {
5151
var expr = node.expression;
5252
if (expr is AssignmentExpression) return;
53-
54-
if (_isEnclosedInAsyncFunctionBody(node)) {
55-
return;
56-
}
57-
58-
if (expr case AwaitExpression(:var expression)) {
59-
expr = expression;
60-
}
61-
62-
if (expr.isAwaitNotRequired) {
63-
return;
64-
}
53+
if (_isEnclosedInAsyncFunctionBody(node)) return;
54+
if (expr is AwaitExpression) return;
55+
if (expr.isAwaitNotRequired) return;
6556

6657
var type = expr.staticType;
6758
if (type == null) {

pkg/linter/test/rules/discarded_futures_test.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,15 @@ Future<int> g() async => 0;
265265
await assertDiagnostics(
266266
'''
267267
void f() {
268-
// ignore: await_in_wrong_context
269268
await g();
270269
}
271270
272-
Future<void> g() async { }
271+
Future<void> g() async {}
273272
''',
274-
[lint(55, 1)],
273+
[
274+
// No lint.
275+
error(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 13, 5),
276+
],
275277
);
276278
}
277279

@@ -280,14 +282,16 @@ Future<void> g() async { }
280282
'''
281283
class C {
282284
void f() {
283-
// ignore: await_in_wrong_context
284285
await g();
285286
}
286287
287-
Future<void> g() async { }
288+
Future<void> g() async {}
288289
}
289290
''',
290-
[lint(71, 1)],
291+
[
292+
// No lint.
293+
error(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 27, 5),
294+
],
291295
);
292296
}
293297

0 commit comments

Comments
 (0)