Fix for ConflictingEditException in RemoveUnusedLocalVariable.

R=brianwilkerson@google.com, pquitslund@google.com

Change-Id: I5614e83b5b7dca720b5de09a5ee7fd457a13b2e1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179820
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
index 5120c45..e3b5cf2 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_local_variable.dart
@@ -50,6 +50,19 @@
         return;
       }
 
+      var isCovered = false;
+      for (var other in sourceRanges) {
+        if (other.covers(sourceRange)) {
+          isCovered = true;
+        } else if (other.intersects(sourceRange)) {
+          return;
+        }
+      }
+
+      if (isCovered) {
+        continue;
+      }
+
       sourceRanges.add(sourceRange);
     }
 
diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_unused_local_variable_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_unused_local_variable_test.dart
index c73b174..dac33f8 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/remove_unused_local_variable_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/remove_unused_local_variable_test.dart
@@ -20,6 +20,21 @@
   @override
   FixKind get kind => DartFixKind.REMOVE_UNUSED_LOCAL_VARIABLE;
 
+  Future<void> test_assignmentInAssignment() async {
+    await resolveTestCode(r'''
+main() {
+  var v = 1;
+  v = (v = 2);
+  print(0);
+}
+''');
+    await assertHasFix(r'''
+main() {
+  print(0);
+}
+''');
+  }
+
   Future<void> test_inArgumentList() async {
     await resolveTestCode(r'''
 main() {