Skip to content

Commit c913e9d

Browse files
fshcheglovCommit Queue
authored andcommitted
Add test and implementation for replacing final with var in for loops.
Change-Id: If32b0f77cae3f1e3d3b8880b70d161fc22a973b7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/436541 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 05026eb commit c913e9d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class ReplaceFinalWithVar extends ResolvedCorrectionProducer {
4646
keyword: var keywordToken,
4747
)) {
4848
finalKeyword = keywordToken;
49+
} else if (context.node case DeclaredIdentifier(
50+
keyword: var keywordToken?,
51+
)) {
52+
assert(keywordToken.keyword == Keyword.FINAL);
53+
finalKeyword = keywordToken;
4954
}
5055

5156
return ReplaceFinalWithVar._(

pkg/analysis_server/test/src/services/correction/fix/replace_final_with_var_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ class ReplaceFinalWithVarTest extends FixProcessorLintTest {
5050
@override
5151
String get lintCode => LintNames.unnecessary_final;
5252

53+
Future<void> test_function_forLoop() async {
54+
await resolveTestCode('''
55+
void f(List<int> values) {
56+
// ignore:unused_local_variable
57+
for (final v in values) {}
58+
}
59+
''');
60+
await assertHasFix('''
61+
void f(List<int> values) {
62+
// ignore:unused_local_variable
63+
for (var v in values) {}
64+
}
65+
''');
66+
}
67+
5368
Future<void> test_function_variableTyped() async {
5469
await resolveTestCode('''
5570
void f() {

0 commit comments

Comments
 (0)