[wildcards] quick fix to convert UNUSED_LOCAL to wildcard

Fixes: https://github.com/dart-lang/sdk/issues/55965

Change-Id: I3585ce3823e0b7f7d2232611b27bb086b8b30d14
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/370505
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
pq 2024-06-10 19:42:06 +00:00 committed by Commit Queue
parent 141bb5417b
commit 6cfe153e1e
6 changed files with 108 additions and 0 deletions

View file

@ -0,0 +1,37 @@
// Copyright (c) 2024, 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_plugin/edit/dart/correction_producer.dart';
import 'package:analyzer/dart/analysis/features.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 ConvertToWildcardVariable extends ResolvedCorrectionProducer {
ConvertToWildcardVariable({required super.context});
@override
CorrectionApplicability get applicability =>
CorrectionApplicability.singleLocation;
@override
FixKind get fixKind => DartFixKind.CONVERT_TO_WILDCARD_VARIABLE;
bool get wildcardVariablesEnabled =>
libraryElement.featureSet.isEnabled(Feature.wildcard_variables);
@override
Future<void> compute(ChangeBuilder builder) async {
if (!wildcardVariablesEnabled) return;
var node = this.node;
if (node is! VariableDeclaration) return;
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.token(node.name), '_');
});
}
}

View file

@ -656,6 +656,11 @@ class DartFixKind {
DartFixKindPriority.DEFAULT,
'Convert to wildcard pattern',
);
static const CONVERT_TO_WILDCARD_VARIABLE = FixKind(
'dart.fix.convert.toWildcardVariable',
DartFixKindPriority.DEFAULT,
'Convert to wildcard variable',
);
static const CREATE_CLASS = FixKind(
'dart.fix.create.class',
DartFixKindPriority.DEFAULT,

View file

@ -77,6 +77,7 @@ import 'package:analysis_server/src/services/correction/dart/convert_to_set_lite
import 'package:analysis_server/src/services/correction/dart/convert_to_super_parameters.dart';
import 'package:analysis_server/src/services/correction/dart/convert_to_where_type.dart';
import 'package:analysis_server/src/services/correction/dart/convert_to_wildcard_pattern.dart';
import 'package:analysis_server/src/services/correction/dart/convert_to_wildcard_variable.dart';
import 'package:analysis_server/src/services/correction/dart/create_class.dart';
import 'package:analysis_server/src/services/correction/dart/create_constructor.dart';
import 'package:analysis_server/src/services/correction/dart/create_constructor_for_final_fields.dart';
@ -1696,6 +1697,7 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
],
WarningCode.UNUSED_LOCAL_VARIABLE: [
RemoveUnusedLocalVariable.new,
ConvertToWildcardVariable.new,
],
WarningCode.UNUSED_SHOWN_NAME: [
RemoveNameFromCombinator.new,

View file

@ -66,6 +66,7 @@ class AbstractContextTest
/// class, an empty list if there are no experiments that should be enabled.
List<String> get experiments => [
Feature.macros.enableString,
Feature.wildcard_variables.enableString,
];
/// The path that is not in [workspaceRootPath], contains external packages.

View file

@ -0,0 +1,61 @@
// Copyright (c) 2024, 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_plugin/utilities/fixes/fixes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'fix_processor.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(ConvertToWildcardVariableTest);
});
}
@reflectiveTest
class ConvertToWildcardVariableTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.CONVERT_TO_WILDCARD_VARIABLE;
Future<void> test_convertUnusedLocalVariable() async {
await resolveTestCode('''
void f() {
var x = '';
}
''');
await assertHasFix('''
void f() {
var _ = '';
}
''');
}
Future<void> test_convertUnusedLocalVariable_list() async {
await resolveTestCode('''
void f() {
int? x, y;
y;
}
''');
await assertHasFix('''
void f() {
int? _, y;
y;
}
''');
}
Future<void> test_convertUnusedLocalVariable_preWildcards() async {
await resolveTestCode('''
// @dart = 3.4
// (pre wildcard-variables)
void f() {
var x = '';
}
''');
await assertNoFix();
}
}

View file

@ -104,6 +104,7 @@ import 'convert_to_spread_test.dart' as convert_to_spread;
import 'convert_to_super_parameters_test.dart' as convert_to_super_parameters;
import 'convert_to_where_type_test.dart' as convert_to_where_type;
import 'convert_to_wildcard_pattern_test.dart' as convert_to_wildcard_pattern;
import 'convert_to_wildcard_variable_test.dart' as convert_to_wildcard_variable;
import 'create_class_test.dart' as create_class;
import 'create_constructor_for_final_fields_test.dart'
as create_constructor_for_final_field;
@ -376,6 +377,7 @@ void main() {
convert_to_super_parameters.main();
convert_to_where_type.main();
convert_to_wildcard_pattern.main();
convert_to_wildcard_variable.main();
create_class.main();
create_constructor_for_final_field.main();
create_constructor_super.main();