Skip to content

Commit 46348af

Browse files
FMorschelCommit Queue
authored and
Commit Queue
committed
[DAS] Fixes Removes unused local variable with awaited expressions
Fixes: #60663 Change-Id: I290f29c3a07e2f20cfd6e78e87b5543fa8131a9c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426380 Commit-Queue: Samuel Rawlins <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 3c6af25 commit 46348af

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@ class RemoveUnusedLocalVariable extends ResolvedCorrectionProducer {
5353
if (declarationStatement is VariableDeclarationStatement) {
5454
if (declarationList.variables.length == 1) {
5555
var initializer = declarationList.variables.first.initializer;
56-
if (initializer is MethodInvocation) {
56+
if (initializer?.unParenthesized
57+
case MethodInvocation() ||
58+
FunctionExpressionInvocation() ||
59+
AwaitExpression()) {
5760
_commands.add(
5861
_DeleteSourceRangeCommand(
5962
sourceRange: SourceRange(
6063
declarationStatement.offset,
61-
initializer.offset - declarationStatement.offset,
64+
initializer!.offset - declarationStatement.offset,
6265
),
6366
),
6467
);

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,81 @@ void f() {
3636
''');
3737
}
3838

39+
Future<void> test_assigned_awaitedExpression() async {
40+
await resolveTestCode(r'''
41+
Future<int> foo = Future.value(0);
42+
void f() async {
43+
final removed = await foo;
44+
}
45+
''');
46+
await assertHasFix(r'''
47+
Future<int> foo = Future.value(0);
48+
void f() async {
49+
await foo;
50+
}
51+
''');
52+
}
53+
54+
Future<void> test_assigned_awaitedInvocation() async {
55+
await resolveTestCode(r'''
56+
Future<int> foo() async => 0;
57+
void f() async {
58+
final removed = await foo();
59+
}
60+
''');
61+
await assertHasFix(r'''
62+
Future<int> foo() async => 0;
63+
void f() async {
64+
await foo();
65+
}
66+
''');
67+
}
68+
69+
Future<void> test_assigned_doubleParenthesised_awaitedInvocation() async {
70+
await resolveTestCode(r'''
71+
Future<int> foo() async => 0;
72+
void f() async {
73+
final removed = ((await foo()));
74+
}
75+
''');
76+
await assertHasFix(r'''
77+
Future<int> foo() async => 0;
78+
void f() async {
79+
((await foo()));
80+
}
81+
''');
82+
}
83+
84+
Future<void> test_assigned_functionExpressionInvocation() async {
85+
await resolveTestCode(r'''
86+
void Function() foo() => () {};
87+
void f() async {
88+
final removed = foo()();
89+
}
90+
''');
91+
await assertHasFix(r'''
92+
void Function() foo() => () {};
93+
void f() async {
94+
foo()();
95+
}
96+
''');
97+
}
98+
99+
Future<void> test_assigned_functionInvocation() async {
100+
await resolveTestCode(r'''
101+
int foo() => 0;
102+
void f() {
103+
final removed = foo();
104+
}
105+
''');
106+
await assertHasFix(r'''
107+
int foo() => 0;
108+
void f() {
109+
foo();
110+
}
111+
''');
112+
}
113+
39114
Future<void> test_assigned_inArgumentList() async {
40115
await resolveTestCode(r'''
41116
void f() {
@@ -127,6 +202,21 @@ void f(str) {
127202
''');
128203
}
129204

205+
Future<void> test_assigned_parenthesised_awaitedInvocation() async {
206+
await resolveTestCode(r'''
207+
Future<int> foo() async => 0;
208+
void f() async {
209+
final removed = (await foo());
210+
}
211+
''');
212+
await assertHasFix(r'''
213+
Future<int> foo() async => 0;
214+
void f() async {
215+
(await foo());
216+
}
217+
''');
218+
}
219+
130220
Future<void> test_notInFunctionBody() async {
131221
await resolveTestCode(r'''
132222
var a = [for (var v = 0;;) 0];

0 commit comments

Comments
 (0)