quick fix for unnecessary_to_list_in_spreads

See: https://github.com/dart-lang/lints/issues/80

Change-Id: I4c77c69e8449a4db1f267143d3dcad19b89d3bed
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/303785
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
pq 2023-05-16 18:08:28 +00:00 committed by Commit Queue
parent 94c2f6528c
commit 8d6bc8dd60
7 changed files with 114 additions and 1 deletions

View file

@ -0,0 +1,35 @@
// Copyright (c) 2023, 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/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
class RemoveToList extends CorrectionProducer {
@override
bool get canBeAppliedInBulk => true;
@override
bool get canBeAppliedToFile => true;
@override
FixKind get fixKind => DartFixKind.REMOVE_UNNECESSARY_TO_LIST;
@override
FixKind get multiFixKind => DartFixKind.REMOVE_UNNECESSARY_TO_LIST_MULTI;
@override
Future<void> compute(ChangeBuilder builder) async {
var invocation = node.thisOrAncestorOfType<MethodInvocation>();
if (invocation == null) return;
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(
range.startEnd(invocation.operator!, invocation.argumentList));
});
}
}

View file

@ -2378,7 +2378,7 @@ LintCode.unnecessary_string_interpolations:
LintCode.unnecessary_this:
status: hasFix
LintCode.unnecessary_to_list_in_spreads:
status: needsFix
status: hasFix
LintCode.unreachable_from_main:
status: noFix
notes: |-

View file

@ -1403,6 +1403,16 @@ class DartFixKind {
DartFixKindPriority.IN_FILE,
'Remove all unnecessary string interpolations in file',
);
static const REMOVE_UNNECESSARY_TO_LIST = FixKind(
'dart.fix.remove.unnecessaryToList',
DartFixKindPriority.DEFAULT,
"Remove unnecessary 'toList' call",
);
static const REMOVE_UNNECESSARY_TO_LIST_MULTI = FixKind(
'dart.fix.remove.unnecessaryToList.multi',
DartFixKindPriority.IN_FILE,
"Remove unnecessary 'toList' calls in file",
);
static const REMOVE_UNNECESSARY_WILDCARD_PATTERN = FixKind(
'dart.fix.remove.unnecessaryWildcardPattern',
DartFixKindPriority.DEFAULT,

View file

@ -155,6 +155,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_required.dar
import 'package:analysis_server/src/services/correction/dart/remove_returned_value.dart';
import 'package:analysis_server/src/services/correction/dart/remove_set_literal.dart';
import 'package:analysis_server/src/services/correction/dart/remove_this_expression.dart';
import 'package:analysis_server/src/services/correction/dart/remove_to_list.dart';
import 'package:analysis_server/src/services/correction/dart/remove_type_annotation.dart';
import 'package:analysis_server/src/services/correction/dart/remove_type_arguments.dart';
import 'package:analysis_server/src/services/correction/dart/remove_unnecessary_cast.dart';
@ -755,6 +756,9 @@ class FixProcessor extends BaseProcessor {
LintNames.unnecessary_string_interpolations: [
RemoveUnnecessaryStringInterpolation.new,
],
LintNames.unnecessary_to_list_in_spreads: [
RemoveToList.new,
],
LintNames.unnecessary_this: [
RemoveThisExpression.new,
],

View file

@ -176,6 +176,8 @@ class LintNames {
static const String unnecessary_string_escapes = 'unnecessary_string_escapes';
static const String unnecessary_string_interpolations =
'unnecessary_string_interpolations';
static const String unnecessary_to_list_in_spreads =
'unnecessary_to_list_in_spreads';
static const String unnecessary_this = 'unnecessary_this';
static const String use_decorated_box = 'use_decorated_box';
static const String use_enums = 'use_enums';

View file

@ -0,0 +1,60 @@
// Copyright (c) 2023, 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:analysis_server/src/services/linter/lint_names.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(RemoveUnnecessaryToListBulkTest);
defineReflectiveTests(RemoveUnnecessaryToListTest);
});
}
@reflectiveTest
class RemoveUnnecessaryToListBulkTest extends BulkFixProcessorTest {
@override
String get lintCode => LintNames.unnecessary_to_list_in_spreads;
Future<void> test_in_file() async {
await resolveTestCode(r'''
var t1 = [
...[1, 2].toList(),
...[3, 4].toList(),
];
''');
await assertHasFix(r'''
var t1 = [
...[1, 2],
...[3, 4],
];
''');
}
}
@reflectiveTest
class RemoveUnnecessaryToListTest extends FixProcessorLintTest {
@override
FixKind get kind => DartFixKind.REMOVE_UNNECESSARY_TO_LIST;
@override
String get lintCode => LintNames.unnecessary_to_list_in_spreads;
Future<void> test_list() async {
await resolveTestCode(r'''
var t1 = [
...[1, 2].toList(),
];
''');
await assertHasFix(r'''
var t1 = [
...[1, 2],
];
''');
}
}

View file

@ -205,6 +205,7 @@ import 'remove_unnecessary_string_escapes_test.dart'
as remove_unnecessary_string_escapes;
import 'remove_unnecessary_string_interpolation_test.dart'
as remove_unnecessary_string_interpolation;
import 'remove_unnecessary_to_list_test.dart' as remove_unnecessary_to_list;
import 'remove_unnecessary_wildcard_pattern_test.dart'
as remove_unnecessary_wildcard_pattern;
import 'remove_unused_catch_clause_test.dart' as remove_unused_catch_clause;
@ -445,6 +446,7 @@ void main() {
remove_unnecessary_raw_string.main();
remove_unnecessary_string_escapes.main();
remove_unnecessary_string_interpolation.main();
remove_unnecessary_to_list.main();
remove_unnecessary_wildcard_pattern.main();
remove_unused_catch_clause.main();
remove_unused_catch_stack.main();