Skip to content

Commit 6fa3c8b

Browse files
fshcheglovCommit Queue
authored andcommitted
Fix a bug relating to the replace_final_with_var quick fix in which for (final int x) would result in the final being replaced with var rather than removed.
Change-Id: I7736a8138d53d5721457354ce6bb47ef7c2fdd37 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/437680 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 5ef7dac commit 6fa3c8b

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

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

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,17 @@ class ReplaceFinalWithVar extends ResolvedCorrectionProducer {
2626
);
2727
}
2828

29-
var removeFinal = false;
30-
Token? finalKeyword;
31-
32-
// Ensure we have set `removeFinal` so that fixKind is accurate after
33-
// configure is completed.
34-
if (context.node case VariableDeclarationList(
35-
keyword: var keywordToken?,
36-
type: var type,
37-
)) {
38-
if (type != null) {
39-
// If a type and keyword is present, the keyword is `final`.
40-
finalKeyword = keywordToken;
41-
removeFinal = true;
42-
} else if (keywordToken.keyword == Keyword.FINAL) {
43-
finalKeyword = keywordToken;
44-
}
45-
} else if (context.node case PatternVariableDeclaration(
46-
keyword: var keywordToken,
47-
)) {
48-
finalKeyword = keywordToken;
49-
} else if (context.node case DeclaredIdentifier(
50-
keyword: var keywordToken?,
51-
)) {
52-
assert(keywordToken.keyword == Keyword.FINAL);
53-
finalKeyword = keywordToken;
54-
}
29+
var (finalKeyword, type) = switch (context.node) {
30+
VariableDeclarationList node => (node.keyword, node.type),
31+
PatternVariableDeclaration node => (node.keyword, null),
32+
DeclaredIdentifier node => (node.keyword, node.type),
33+
_ => (null, null),
34+
};
5535

5636
return ReplaceFinalWithVar._(
5737
context: context,
5838
finalKeyword: finalKeyword,
59-
removeFinal: removeFinal,
39+
removeFinal: type != null,
6040
);
6141
}
6242

@@ -86,6 +66,7 @@ class ReplaceFinalWithVar extends ResolvedCorrectionProducer {
8666
@override
8767
Future<void> compute(ChangeBuilder builder) async {
8868
if (_finalKeyword case var finalKeyword?) {
69+
assert(finalKeyword.keyword == Keyword.FINAL);
8970
if (_removeFinal) {
9071
await builder.addDartFileEdit(file, (builder) {
9172
builder.addDeletion(

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ class ReplaceFinalWithVarTypedRemoveTest extends FixProcessorLintTest {
150150
@override
151151
String get lintCode => LintNames.unnecessary_final;
152152

153+
Future<void> test_final_type() async {
154+
await resolveTestCode(r'''
155+
void f(List<int> values) {
156+
for (final int value in values) {
157+
value;
158+
}
159+
}
160+
''');
161+
await assertHasFix(r'''
162+
void f(List<int> values) {
163+
for (int value in values) {
164+
value;
165+
}
166+
}
167+
''');
168+
}
169+
153170
Future<void> test_function_variableTyped() async {
154171
await resolveTestCode('''
155172
void f() {

0 commit comments

Comments
 (0)