From dfaf1514eaab8667f1c60e44f74e09427f1ceba2 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Fri, 16 May 2025 15:34:20 -0700 Subject: [PATCH] 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 Commit-Queue: Samuel Rawlins --- .../correction/fix/add_async_test.dart | 42 ------------------- .../lib/src/rules/discarded_futures.dart | 15 ++----- .../test/rules/discarded_futures_test.dart | 16 ++++--- 3 files changed, 13 insertions(+), 60 deletions(-) diff --git a/pkg/analysis_server/test/src/services/correction/fix/add_async_test.dart b/pkg/analysis_server/test/src/services/correction/fix/add_async_test.dart index 7f3e9c0eca9..b6a4a5115fa 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/add_async_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/add_async_test.dart @@ -549,48 +549,6 @@ Future g() async { } '''); } - Future test_discardedFuture_awaited() async { - await resolveTestCode(''' -void f() { - // ignore: await_in_wrong_context - await g(); -} - -Future g() async { } -'''); - await assertHasFix(''' -Future f() async { - // ignore: await_in_wrong_context - await g(); -} - -Future g() async { } -'''); - } - - Future test_discardedFuture_awaited_method() async { - await resolveTestCode(''' -class C { - void f() { - // ignore: await_in_wrong_context - await g(); - } - - Future g() async { } -} -'''); - await assertHasFix(''' -class C { - Future f() async { - // ignore: await_in_wrong_context - await g(); - } - - Future g() async { } -} -'''); - } - Future test_discardedFuture_method() async { await resolveTestCode(''' class C { diff --git a/pkg/linter/lib/src/rules/discarded_futures.dart b/pkg/linter/lib/src/rules/discarded_futures.dart index 1b149ac2f86..1f5470610c9 100644 --- a/pkg/linter/lib/src/rules/discarded_futures.dart +++ b/pkg/linter/lib/src/rules/discarded_futures.dart @@ -50,18 +50,9 @@ class _Visitor extends SimpleAstVisitor { 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) { diff --git a/pkg/linter/test/rules/discarded_futures_test.dart b/pkg/linter/test/rules/discarded_futures_test.dart index d1233cd9f5d..cc27c2317ab 100644 --- a/pkg/linter/test/rules/discarded_futures_test.dart +++ b/pkg/linter/test/rules/discarded_futures_test.dart @@ -265,13 +265,15 @@ Future g() async => 0; await assertDiagnostics( ''' void f() { - // ignore: await_in_wrong_context await g(); } -Future g() async { } +Future g() async {} ''', - [lint(55, 1)], + [ + // No lint. + error(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 13, 5), + ], ); } @@ -280,14 +282,16 @@ Future g() async { } ''' class C { void f() { - // ignore: await_in_wrong_context await g(); } - Future g() async { } + Future g() async {} } ''', - [lint(71, 1)], + [ + // No lint. + error(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 27, 5), + ], ); }