Add a fix for HintCode.UNUSED_ELEMENT_PARAMETER.

This repurposes an existing lint CorrectionProducer to handle both
the lint and the hint.

Bug: https://github.com/dart-lang/sdk/issues/49074
Change-Id: Iacd597ca2151cdb80be9ddae0847128b4cc700a3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/246989
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Janice Collins <jcollins@google.com>
This commit is contained in:
Janice Collins 2022-06-08 16:57:02 +00:00 committed by Commit Bot
parent f681734aef
commit e63e4937e9
4 changed files with 153 additions and 2 deletions

View file

@ -25,7 +25,15 @@ class RemoveUnusedParameter extends CorrectionProducer {
@override
Future<void> compute(ChangeBuilder builder) async {
var parameter = node;
// To work for the unused_parameter hint as well as the lint, we must
// allow for passing in `SimpleIdentifier`s.
var maybeParameter = node;
var maybeParameterParent = maybeParameter.parent;
if (maybeParameter is SimpleIdentifier && maybeParameterParent != null) {
maybeParameter = maybeParameterParent;
}
var parameter = maybeParameter;
if (parameter is! FormalParameter) {
return;
}

View file

@ -1490,7 +1490,7 @@ HintCode.UNUSED_CATCH_STACK:
HintCode.UNUSED_ELEMENT:
status: hasFix
HintCode.UNUSED_ELEMENT_PARAMETER:
status: needsEvaluation
status: hasFix
HintCode.UNUSED_FIELD:
status: hasFix
HintCode.UNUSED_IMPORT:

View file

@ -1339,6 +1339,9 @@ class FixProcessor extends BaseProcessor {
HintCode.UNUSED_ELEMENT: [
RemoveUnusedElement.new,
],
HintCode.UNUSED_ELEMENT_PARAMETER: [
RemoveUnusedParameter.new,
],
HintCode.UNUSED_FIELD: [
RemoveUnusedField.new,
],

View file

@ -13,6 +13,8 @@ void main() {
defineReflectiveSuite(() {
defineReflectiveTests(RemoveUnusedParameterTest);
defineReflectiveTests(RemoveUnusedParameterBulkTest);
defineReflectiveTests(RemoveUnusedParameterTestHint);
defineReflectiveTests(RemoveUnusedParameterBulkTestHint);
});
}
@ -37,6 +39,52 @@ class C {
}
}
@reflectiveTest
class RemoveUnusedParameterBulkTestHint extends BulkFixProcessorTest {
Future<void> test_singleFile() async {
await resolveTestCode('''
class _C {
int? a;
int b;
int? c;
_C([this.a, this.b = 1, this.c]);
}
class _C2 {
int? a;
int b;
int? c;
_C2({this.a, this.b = 1, this.c});
}
void main() {
print(_C());
print(_C2());
}
''');
await assertHasFix('''
class _C {
int? a;
int b;
int? c;
_C([this.b = 1]);
}
class _C2 {
int? a;
int b;
int? c;
_C2({this.b = 1});
}
void main() {
print(_C());
print(_C2());
}
''');
}
}
@reflectiveTest
class RemoveUnusedParameterTest extends FixProcessorLintTest {
@override
@ -278,3 +326,95 @@ class C {
''');
}
}
/// Situations exist where unused parameters are flagged by hints, rather
/// than lints. Apply the same algorithm for parameter removal as for
/// lints.
@reflectiveTest
class RemoveUnusedParameterTestHint extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.REMOVE_UNUSED_PARAMETER;
Future<void> test_optionalNamed() async {
await resolveTestCode('''
class _C {
final int variable;
int? somethingElse;
_C({required this.variable, this.somethingElse});
}
void main() {
print(_C(variable: 123));
}
''');
await assertHasFix('''
class _C {
final int variable;
int? somethingElse;
_C({required this.variable});
}
void main() {
print(_C(variable: 123));
}
''');
}
Future<void> test_optionalPositional() async {
await resolveTestCode('''
class _C {
int? somethingElse;
int? entirely;
_C([this.somethingElse, this.entirely]);
}
void main() {
print(_C(123));
}
''');
await assertHasFix('''
class _C {
int? somethingElse;
int? entirely;
_C([this.somethingElse]);
}
void main() {
print(_C(123));
}
''');
}
Future<void> test_optionalSuperNamed() async {
await resolveTestCode('''
class B {
final int? key;
B({this.key});
}
class _C extends B {
final int variable;
_C({super.key, required this.variable});
}
void main() {
print(_C(variable: 123));
}
''');
await assertHasFix('''
class B {
final int? key;
B({this.key});
}
class _C extends B {
final int variable;
_C({required this.variable});
}
void main() {
print(_C(variable: 123));
}
''');
}
}