+ quickfix for noop_primitive_operations

Change-Id: I458cbe9213c090b8f4c34d9d81c0f1251fa10f03
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/285882
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
pq 2023-02-28 18:45:30 +00:00 committed by Commit Queue
parent 2d1373bc56
commit 47a524afeb
7 changed files with 113 additions and 3 deletions

View file

@ -0,0 +1,45 @@
// 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 RemoveInvocation extends CorrectionProducer {
String _methodName = '';
@override
bool get canBeAppliedInBulk => true;
@override
bool get canBeAppliedToFile => true;
@override
List<Object> get fixArguments => [_methodName];
@override
FixKind get fixKind => DartFixKind.REMOVE_INVOCATION;
@override
FixKind get multiFixKind => DartFixKind.REMOVE_INVOCATION_MULTI;
@override
Future<void> compute(ChangeBuilder builder) async {
var id = node;
if (id is! SimpleIdentifier) return;
var invocation = id.parent;
if (invocation is! MethodInvocation) return;
_methodName = id.name;
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(
range.startEnd(invocation.operator!, invocation.argumentList));
});
}
}

View file

@ -1762,9 +1762,7 @@ LintCode.no_runtimeType_toString:
LintCode.non_constant_identifier_names:
status: hasFix
LintCode.noop_primitive_operations:
status: needsFix
notes: |-
There should be a fix to remove the invocation of `toString`.
status: hasFix
LintCode.null_check_on_nullable_type_parameter:
status: hasFix
notes: |-

View file

@ -1066,6 +1066,16 @@ class DartFixKind {
DartFixKindPriority.IN_FILE,
"Remove unnecessary '??' operators everywhere in file",
);
static const REMOVE_INVOCATION = FixKind(
'dart.fix.remove.invocation',
DartFixKindPriority.DEFAULT,
'Remove unnecessary {0} invocation',
);
static const REMOVE_INVOCATION_MULTI = FixKind(
'dart.fix.remove.invocation.multi',
DartFixKindPriority.IN_FILE,
'Remove unnecessary {0} invocations in file',
);
static const REMOVE_INITIALIZER = FixKind(
'dart.fix.remove.initializer',
DartFixKindPriority.DEFAULT,

View file

@ -135,6 +135,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_empty_statem
import 'package:analysis_server/src/services/correction/dart/remove_if_null_operator.dart';
import 'package:analysis_server/src/services/correction/dart/remove_initializer.dart';
import 'package:analysis_server/src/services/correction/dart/remove_interpolation_braces.dart';
import 'package:analysis_server/src/services/correction/dart/remove_invocation.dart';
import 'package:analysis_server/src/services/correction/dart/remove_leading_underscore.dart';
import 'package:analysis_server/src/services/correction/dart/remove_method_declaration.dart';
import 'package:analysis_server/src/services/correction/dart/remove_name_from_combinator.dart';
@ -526,6 +527,9 @@ class FixProcessor extends BaseProcessor {
LintNames.non_constant_identifier_names: [
RenameToCamelCase.new,
],
LintNames.noop_primitive_operations: [
RemoveInvocation.new,
],
LintNames.null_check_on_nullable_type_parameter: [
ReplaceNullCheckWithCast.new,
],

View file

@ -79,6 +79,7 @@ class LintNames {
'no_leading_underscores_for_local_identifiers';
static const String non_constant_identifier_names =
'non_constant_identifier_names';
static const String noop_primitive_operations = 'noop_primitive_operations';
static const String null_check_on_nullable_type_parameter =
'null_check_on_nullable_type_parameter';
static const String null_closures = 'null_closures';

View file

@ -0,0 +1,50 @@
// 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(RemoveInvocationBulkTest);
defineReflectiveTests(RemoveInvocationTest);
});
}
@reflectiveTest
class RemoveInvocationBulkTest extends BulkFixProcessorTest {
@override
String get lintCode => LintNames.noop_primitive_operations;
Future<void> test_singleFile() async {
await resolveTestCode(r'''
var s = '${1.toString()}${2.toString()}';
''');
await assertHasFix(r'''
var s = '${1}${2}';
''');
}
}
@reflectiveTest
class RemoveInvocationTest extends FixProcessorLintTest {
@override
FixKind get kind => DartFixKind.REMOVE_INVOCATION;
@override
String get lintCode => LintNames.noop_primitive_operations;
Future<void> test_intLiteral() async {
await resolveTestCode(r'''
var s = '${1.toString()}';
''');
await assertHasFix(r'''
var s = '${1}';
''');
}
}

View file

@ -165,6 +165,7 @@ import 'remove_empty_statement_test.dart' as remove_empty_statement;
import 'remove_if_null_operator_test.dart' as remove_if_null_operator;
import 'remove_initializer_test.dart' as remove_initializer;
import 'remove_interpolation_braces_test.dart' as remove_interpolation_braces;
import 'remove_invocation_test.dart' as remove_invocation;
import 'remove_leading_underscore_test.dart' as remove_leading_underscore;
import 'remove_method_declaration_test.dart' as remove_method_declaration;
import 'remove_name_from_combinator_test.dart' as remove_name_from_combinator;
@ -400,6 +401,7 @@ void main() {
remove_if_null_operator.main();
remove_initializer.main();
remove_interpolation_braces.main();
remove_invocation.main();
remove_leading_underscore.main();
remove_method_declaration.main();
remove_name_from_combinator.main();