Skip to content

Commit cd54936

Browse files
FMorschelbwilkerson
authored andcommitted
[DAS] Fixes broken discarded futures tests
Bug: #59887 Change-Id: I6c8204771b70d49091f882e5b7f875b158724b89 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417900 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent 2b82a40 commit cd54936

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,19 @@ import 'dart:async';
7272
7373
FutureOr<void> g() async { }
7474
75-
Future<void> f() {
75+
void f() {
7676
g();
7777
}
7878
''');
79-
await assertNoFix();
79+
await assertHasFix('''
80+
import 'dart:async';
81+
82+
FutureOr<void> g() async { }
83+
84+
void f() {
85+
unawaited(g());
86+
}
87+
''');
8088
}
8189
}
8290

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

+15-12
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ class _Visitor extends SimpleAstVisitor<void> {
4949
var expr = node.expression;
5050
if (expr is AssignmentExpression) return;
5151

52+
if (_isEnclosedInAsyncFunctionBody(node)) {
53+
return;
54+
}
55+
56+
if (expr case AwaitExpression(:var expression)) {
57+
expr = expression;
58+
}
59+
5260
var type = expr.staticType;
5361
if (type == null) {
5462
return;
@@ -60,11 +68,7 @@ class _Visitor extends SimpleAstVisitor<void> {
6068
return;
6169
}
6270

63-
if (_isNotEnclosedInAsyncFunctionBody(node)) {
64-
// Future expression statement that isn't awaited in synchronous
65-
// function: while this is legal, it's a very frequent sign of an error.
66-
_reportOnExpression(expr);
67-
}
71+
_reportOnExpression(expr);
6872
}
6973
}
7074

@@ -73,6 +77,11 @@ class _Visitor extends SimpleAstVisitor<void> {
7377
_visit(node.expression);
7478
}
7579

80+
bool _isEnclosedInAsyncFunctionBody(AstNode node) {
81+
var enclosingFunctionBody = node.thisOrAncestorOfType<FunctionBody>();
82+
return enclosingFunctionBody?.isAsynchronous ?? false;
83+
}
84+
7685
/// Detects `Future.delayed(duration, [computation])` creations with a
7786
/// computation.
7887
bool _isFutureDelayedInstanceCreationWithComputation(Expression expr) =>
@@ -90,12 +99,6 @@ class _Visitor extends SimpleAstVisitor<void> {
9099
expr.methodName.name == 'putIfAbsent' &&
91100
_isMapClass(expr.methodName.element?.enclosingElement2);
92101

93-
bool _isNotEnclosedInAsyncFunctionBody(AstNode node) {
94-
var enclosingFunctionBody = node.thisOrAncestorOfType<FunctionBody>();
95-
var isAsyncBody = enclosingFunctionBody?.isAsynchronous ?? false;
96-
return !isAsyncBody;
97-
}
98-
99102
void _reportOnExpression(Expression expr) {
100103
rule.reportLint(switch (expr) {
101104
MethodInvocation(:var methodName) => methodName,
@@ -109,7 +112,7 @@ class _Visitor extends SimpleAstVisitor<void> {
109112

110113
void _visit(Expression expr) {
111114
if ((expr.staticType.isFutureOrFutureOr) &&
112-
_isNotEnclosedInAsyncFunctionBody(expr) &&
115+
!_isEnclosedInAsyncFunctionBody(expr) &&
113116
expr is! AssignmentExpression) {
114117
_reportOnExpression(expr);
115118
}

pkg/linter/test/rules/discarded_futures_test.dart

+30
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,36 @@ Future<int> g() async => 0;
258258
''');
259259
}
260260

261+
Future<void> test_discardedFuture_awaited() async {
262+
await assertDiagnostics(
263+
'''
264+
void f() {
265+
// ignore: await_in_wrong_context
266+
await g();
267+
}
268+
269+
Future<void> g() async { }
270+
''',
271+
[lint(55, 1)],
272+
);
273+
}
274+
275+
Future<void> test_discardedFuture_awaited_method() async {
276+
await assertDiagnostics(
277+
'''
278+
class C {
279+
void f() {
280+
// ignore: await_in_wrong_context
281+
await g();
282+
}
283+
284+
Future<void> g() async { }
285+
}
286+
''',
287+
[lint(71, 1)],
288+
);
289+
}
290+
261291
Future<void> test_field_assignment() async {
262292
await assertDiagnostics(
263293
r'''

0 commit comments

Comments
 (0)