Skip to content

Commit

Permalink
[quick fix] new correction for unnecessary_underscores
Browse files Browse the repository at this point in the history
Bug: #56595
Change-Id: If7faeca31b4340fcd4b7d5f4642caf284cf36308
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403383
Commit-Queue: Phil Quitslund <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Jan 7, 2025
1 parent a33ae0f commit 2258ea7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class ConvertToWildcardVariable extends ResolvedCorrectionProducer {
if (!wildcardVariablesEnabled) return;

var node = this.node;

if (node is FormalParameter) {
await computeFormalParameterConversion(builder, node);
return;
}

if (node is! VariableDeclaration) return;

var nameToken = node.name;
Expand All @@ -55,4 +61,13 @@ class ConvertToWildcardVariable extends ResolvedCorrectionProducer {
}
});
}

Future<void> computeFormalParameterConversion(
ChangeBuilder builder,
FormalParameter node,
) async {
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.token(node.name!), '_');
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,7 @@ LintCode.unnecessary_this:
LintCode.unnecessary_to_list_in_spreads:
status: hasFix
LintCode.unnecessary_underscores:
status: needsFix
status: hasFix
LintCode.unreachable_from_main:
status: hasFix
LintCode.unrelated_type_equality_checks_in_expression:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ final _builtInLintProducers = <LintCode, List<ProducerGenerator>>{
],
LinterLintCode.unnecessary_to_list_in_spreads: [RemoveToList.new],
LinterLintCode.unnecessary_this: [RemoveThisExpression.new],
LinterLintCode.unnecessary_underscores: [ConvertToWildcardVariable.new],
LinterLintCode.unreachable_from_main: [RemoveUnusedElement.new],
LinterLintCode.use_colored_box: [ReplaceContainerWithColoredBox.new],
LinterLintCode.use_decorated_box: [ReplaceWithDecoratedBox.new],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:linter/src/lint_names.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import 'fix_processor.dart';

void main() {
defineReflectiveSuite(() {
defineReflectiveTests(ConvertToWildcardVariableTest);
defineReflectiveTests(ConvertUnnecessaryUnderscoresTest);
});
}

Expand Down Expand Up @@ -116,3 +118,36 @@ void f() {
await assertNoFix();
}
}

@reflectiveTest
class ConvertUnnecessaryUnderscoresTest extends FixProcessorLintTest {
@override
FixKind get kind => DartFixKind.CONVERT_TO_WILDCARD_VARIABLE;

@override
String get lintCode => LintNames.unnecessary_underscores;

Future<void> test_functionParameter() async {
await resolveTestCode(r'''
void f(int __) {}
''');
await assertHasFix(r'''
void f(int _) {}
''');
}

Future<void> test_localVariable() async {
await resolveTestCode(r'''
void f() {
// ignore: UNUSED_LOCAL_VARIABLE
int __ = 0;
}
''');
await assertHasFix(r'''
void f() {
// ignore: UNUSED_LOCAL_VARIABLE
int _ = 0;
}
''');
}
}

0 comments on commit 2258ea7

Please sign in to comment.