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 <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.dart
index b36fcbd..b193c5f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_final_with_var.dart
@@ -26,37 +26,17 @@
       );
     }
 
-    var removeFinal = false;
-    Token? finalKeyword;
-
-    // Ensure we have set `removeFinal` so that fixKind is accurate after
-    // configure is completed.
-    if (context.node case VariableDeclarationList(
-      keyword: var keywordToken?,
-      type: var type,
-    )) {
-      if (type != null) {
-        // If a type and keyword is present, the keyword is `final`.
-        finalKeyword = keywordToken;
-        removeFinal = true;
-      } else if (keywordToken.keyword == Keyword.FINAL) {
-        finalKeyword = keywordToken;
-      }
-    } else if (context.node case PatternVariableDeclaration(
-      keyword: var keywordToken,
-    )) {
-      finalKeyword = keywordToken;
-    } else if (context.node case DeclaredIdentifier(
-      keyword: var keywordToken?,
-    )) {
-      assert(keywordToken.keyword == Keyword.FINAL);
-      finalKeyword = keywordToken;
-    }
+    var (finalKeyword, type) = switch (context.node) {
+      VariableDeclarationList node => (node.keyword, node.type),
+      PatternVariableDeclaration node => (node.keyword, null),
+      DeclaredIdentifier node => (node.keyword, node.type),
+      _ => (null, null),
+    };
 
     return ReplaceFinalWithVar._(
       context: context,
       finalKeyword: finalKeyword,
-      removeFinal: removeFinal,
+      removeFinal: type != null,
     );
   }
 
@@ -86,6 +66,7 @@
   @override
   Future<void> compute(ChangeBuilder builder) async {
     if (_finalKeyword case var finalKeyword?) {
+      assert(finalKeyword.keyword == Keyword.FINAL);
       if (_removeFinal) {
         await builder.addDartFileEdit(file, (builder) {
           builder.addDeletion(
diff --git a/pkg/analysis_server/test/src/services/correction/fix/replace_final_with_var_test.dart b/pkg/analysis_server/test/src/services/correction/fix/replace_final_with_var_test.dart
index 8db98de..b8d765e 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/replace_final_with_var_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/replace_final_with_var_test.dart
@@ -150,6 +150,23 @@
   @override
   String get lintCode => LintNames.unnecessary_final;
 
+  Future<void> test_final_type() async {
+    await resolveTestCode(r'''
+void f(List<int> values) {
+  for (final int value in values) {
+    value;
+  }
+}
+''');
+    await assertHasFix(r'''
+void f(List<int> values) {
+  for (int value in values) {
+    value;
+  }
+}
+''');
+  }
+
   Future<void> test_function_variableTyped() async {
     await resolveTestCode('''
 void f() {