[DAS] Fixes `Removes unused local variable` with awaited expressions
Fixes: https://github.com/dart-lang/sdk/issues/60663
Change-Id: I290f29c3a07e2f20cfd6e78e87b5543fa8131a9c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426380
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Auto-Submit: Felipe Morschel <git@fmorschel.dev>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
index 16ab0bd..047a0a9 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
@@ -53,12 +53,15 @@
if (declarationStatement is VariableDeclarationStatement) {
if (declarationList.variables.length == 1) {
var initializer = declarationList.variables.first.initializer;
- if (initializer is MethodInvocation) {
+ if (initializer?.unParenthesized
+ case MethodInvocation() ||
+ FunctionExpressionInvocation() ||
+ AwaitExpression()) {
_commands.add(
_DeleteSourceRangeCommand(
sourceRange: SourceRange(
declarationStatement.offset,
- initializer.offset - declarationStatement.offset,
+ initializer!.offset - declarationStatement.offset,
),
),
);
diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_unused_local_variable_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_unused_local_variable_test.dart
index e0a1acf..55fb8f3 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/remove_unused_local_variable_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/remove_unused_local_variable_test.dart
@@ -36,6 +36,81 @@
''');
}
+ Future<void> test_assigned_awaitedExpression() async {
+ await resolveTestCode(r'''
+Future<int> foo = Future.value(0);
+void f() async {
+ final removed = await foo;
+}
+''');
+ await assertHasFix(r'''
+Future<int> foo = Future.value(0);
+void f() async {
+ await foo;
+}
+''');
+ }
+
+ Future<void> test_assigned_awaitedInvocation() async {
+ await resolveTestCode(r'''
+Future<int> foo() async => 0;
+void f() async {
+ final removed = await foo();
+}
+''');
+ await assertHasFix(r'''
+Future<int> foo() async => 0;
+void f() async {
+ await foo();
+}
+''');
+ }
+
+ Future<void> test_assigned_doubleParenthesised_awaitedInvocation() async {
+ await resolveTestCode(r'''
+Future<int> foo() async => 0;
+void f() async {
+ final removed = ((await foo()));
+}
+''');
+ await assertHasFix(r'''
+Future<int> foo() async => 0;
+void f() async {
+ ((await foo()));
+}
+''');
+ }
+
+ Future<void> test_assigned_functionExpressionInvocation() async {
+ await resolveTestCode(r'''
+void Function() foo() => () {};
+void f() async {
+ final removed = foo()();
+}
+''');
+ await assertHasFix(r'''
+void Function() foo() => () {};
+void f() async {
+ foo()();
+}
+''');
+ }
+
+ Future<void> test_assigned_functionInvocation() async {
+ await resolveTestCode(r'''
+int foo() => 0;
+void f() {
+ final removed = foo();
+}
+''');
+ await assertHasFix(r'''
+int foo() => 0;
+void f() {
+ foo();
+}
+''');
+ }
+
Future<void> test_assigned_inArgumentList() async {
await resolveTestCode(r'''
void f() {
@@ -127,6 +202,21 @@
''');
}
+ Future<void> test_assigned_parenthesised_awaitedInvocation() async {
+ await resolveTestCode(r'''
+Future<int> foo() async => 0;
+void f() async {
+ final removed = (await foo());
+}
+''');
+ await assertHasFix(r'''
+Future<int> foo() async => 0;
+void f() async {
+ (await foo());
+}
+''');
+ }
+
Future<void> test_notInFunctionBody() async {
await resolveTestCode(r'''
var a = [for (var v = 0;;) 0];