Add a fix to remove `var` for `VAR_RETURN_TYPE`
Bug: #48779
Change-Id: I753a7205594457c1f7de29e146ed7dd05a772359
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/240841
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_var.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_var.dart
new file mode 100644
index 0000000..e0c65fe
--- /dev/null
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_var.dart
@@ -0,0 +1,25 @@
+// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
+import 'package:analysis_server/src/services/correction/fix.dart';
+import 'package:analyzer/source/source_range.dart';
+import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
+import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
+
+class RemoveVar extends CorrectionProducer {
+ @override
+ FixKind get fixKind => DartFixKind.REMOVE_VAR;
+
+ @override
+ Future<void> compute(ChangeBuilder builder) async {
+ var diagnostic = this.diagnostic;
+ if (diagnostic == null) return;
+ var message = diagnostic.problemMessage;
+
+ await builder.addDartFileEdit(file, (builder) {
+ builder.addDeletion(SourceRange(message.offset, message.length + 1));
+ });
+ }
+}
diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
index c63b94e..c0147af 100644
--- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
+++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
@@ -2338,7 +2338,7 @@
ParserErrorCode.VAR_ENUM:
status: needsEvaluation
ParserErrorCode.VAR_RETURN_TYPE:
- status: needsEvaluation
+ status: hasFix
ParserErrorCode.VAR_TYPEDEF:
status: needsEvaluation
ParserErrorCode.VOID_WITH_TYPE_ARGUMENTS:
diff --git a/pkg/analysis_server/lib/src/services/correction/fix.dart b/pkg/analysis_server/lib/src/services/correction/fix.dart
index 1909be2..cd161c2 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix.dart
@@ -1253,6 +1253,11 @@
DartFixKindPriority.IN_FILE,
'Remove unused parameters everywhere in file',
);
+ static const REMOVE_VAR = FixKind(
+ 'dart.fix.remove.var',
+ DartFixKindPriority.DEFAULT,
+ "Remove 'var'",
+ );
static const RENAME_TO_CAMEL_CASE = FixKind(
'dart.fix.rename.toCamelCase',
DartFixKindPriority.DEFAULT,
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
index f0c4310..494f1ae 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -150,6 +150,7 @@
import 'package:analysis_server/src/services/correction/dart/remove_unused_label.dart';
import 'package:analysis_server/src/services/correction/dart/remove_unused_local_variable.dart';
import 'package:analysis_server/src/services/correction/dart/remove_unused_parameter.dart';
+import 'package:analysis_server/src/services/correction/dart/remove_var.dart';
import 'package:analysis_server/src/services/correction/dart/rename_to_camel_case.dart';
import 'package:analysis_server/src/services/correction/dart/replace_Null_with_void.dart';
import 'package:analysis_server/src/services/correction/dart/replace_boolean_with_bool.dart';
@@ -1351,6 +1352,9 @@
ParserErrorCode.VAR_AS_TYPE_NAME: [
ReplaceVarWithDynamic.new,
],
+ ParserErrorCode.VAR_RETURN_TYPE: [
+ RemoveVar.new,
+ ],
StaticWarningCode.DEAD_NULL_AWARE_EXPRESSION: [
RemoveDeadIfNull.new,
],
diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_var_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_var_test.dart
new file mode 100644
index 0000000..642d2d4
--- /dev/null
+++ b/pkg/analysis_server/test/src/services/correction/fix/remove_var_test.dart
@@ -0,0 +1,55 @@
+// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:analysis_server/src/services/correction/fix.dart';
+import 'package:analyzer/src/generated/parser.dart';
+import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
+import 'package:test_reflective_loader/test_reflective_loader.dart';
+
+import 'fix_processor.dart';
+
+void main() {
+ defineReflectiveSuite(() {
+ defineReflectiveTests(RemoveVarTest);
+ });
+}
+
+@reflectiveTest
+class RemoveVarTest extends FixProcessorTest {
+ @override
+ FixKind get kind => DartFixKind.REMOVE_VAR;
+
+ Future<void> test_function() async {
+ await resolveTestCode('''
+var f() {}
+''');
+ await assertHasFix('''
+f() {}
+''');
+ }
+
+ Future<void> test_setter() async {
+ await resolveTestCode('''
+class C {
+ var set s(int i) {}
+}
+''');
+ await assertHasFix('''
+class C {
+ set s(int i) {}
+}
+''');
+ }
+
+ Future<void> test_typedef() async {
+ await resolveTestCode('''
+typedef F = var Function();
+''');
+ await assertHasFix('''
+typedef F = Function();
+''', errorFilter: (error) {
+ return error.errorCode == ParserErrorCode.VAR_RETURN_TYPE;
+ });
+ }
+}
diff --git a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart
index 278017c..efbc6aa 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart
@@ -185,6 +185,7 @@
import 'remove_unused_label_test.dart' as remove_unused_label;
import 'remove_unused_local_variable_test.dart' as remove_unused_local_variable;
import 'remove_unused_parameter_test.dart' as remove_unused_parameter;
+import 'remove_var_test.dart' as remove_var;
import 'rename_to_camel_case_test.dart' as rename_to_camel_case;
import 'replace_Null_with_void_test.dart' as replace_null_with_void;
import 'replace_boolean_with_bool_test.dart' as replace_boolean_with_bool;
@@ -390,6 +391,7 @@
remove_unused_label.main();
remove_unused_local_variable.main();
remove_unused_parameter.main();
+ remove_var.main();
rename_to_camel_case.main();
replace_boolean_with_bool.main();
replace_cascade_with_dot.main();