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 <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Sam Rawlins
2025-05-16 15:34:20 -07:00
committed by Commit Queue
parent 5b4dc61717
commit dfaf1514ea
3 changed files with 13 additions and 60 deletions
@@ -549,48 +549,6 @@ Future<void> g() async { }
''');
}
Future<void> test_discardedFuture_awaited() async {
await resolveTestCode('''
void f() {
// ignore: await_in_wrong_context
await g();
}
Future<void> g() async { }
''');
await assertHasFix('''
Future<void> f() async {
// ignore: await_in_wrong_context
await g();
}
Future<void> g() async { }
''');
}
Future<void> test_discardedFuture_awaited_method() async {
await resolveTestCode('''
class C {
void f() {
// ignore: await_in_wrong_context
await g();
}
Future<void> g() async { }
}
''');
await assertHasFix('''
class C {
Future<void> f() async {
// ignore: await_in_wrong_context
await g();
}
Future<void> g() async { }
}
''');
}
Future<void> test_discardedFuture_method() async {
await resolveTestCode('''
class C {
@@ -50,18 +50,9 @@ class _Visitor extends SimpleAstVisitor<void> {
void visitExpressionStatement(ExpressionStatement node) {
var expr = node.expression;
if (expr is AssignmentExpression) return;
if (_isEnclosedInAsyncFunctionBody(node)) {
return;
}
if (expr case AwaitExpression(:var expression)) {
expr = expression;
}
if (expr.isAwaitNotRequired) {
return;
}
if (_isEnclosedInAsyncFunctionBody(node)) return;
if (expr is AwaitExpression) return;
if (expr.isAwaitNotRequired) return;
var type = expr.staticType;
if (type == null) {
@@ -265,13 +265,15 @@ Future<int> g() async => 0;
await assertDiagnostics(
'''
void f() {
// ignore: await_in_wrong_context
await g();
}
Future<void> g() async { }
Future<void> g() async {}
''',
[lint(55, 1)],
[
// No lint.
error(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 13, 5),
],
);
}
@@ -280,14 +282,16 @@ Future<void> g() async { }
'''
class C {
void f() {
// ignore: await_in_wrong_context
await g();
}
Future<void> g() async { }
Future<void> g() async {}
}
''',
[lint(71, 1)],
[
// No lint.
error(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 27, 5),
],
);
}