Skip to content

Commit 57e71e3

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Fix RemoveUnusedField when some SourceRange(s) being removed are fully covered.
Change-Id: I7ae8b9286c473a368def9c9db22b2f2b828c7b81 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214260 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 19b7a3e commit 57e71e3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ class RemoveUnusedField extends _RemoveUnused {
116116
sourceRanges.add(sourceRange);
117117
}
118118

119+
final uniqueSourceRanges = _uniqueSourceRanges(sourceRanges);
119120
await builder.addDartFileEdit(file, (builder) {
120-
for (var sourceRange in sourceRanges) {
121+
for (var sourceRange in uniqueSourceRanges) {
121122
builder.addDeletion(sourceRange);
122123
}
123124
});
@@ -182,6 +183,24 @@ class RemoveUnusedField extends _RemoveUnused {
182183
}
183184
}
184185

186+
/// Return [SourceRange]s that are not covered by other in [ranges].
187+
/// If there is any intersection, it must be fully covered, never partially.
188+
List<SourceRange> _uniqueSourceRanges(List<SourceRange> ranges) {
189+
var result = <SourceRange>[];
190+
candidates:
191+
for (var candidate in ranges) {
192+
for (var other in ranges) {
193+
if (identical(candidate, other)) {
194+
continue;
195+
} else if (candidate.coveredBy(other)) {
196+
continue candidates;
197+
}
198+
}
199+
result.add(candidate);
200+
}
201+
return result;
202+
}
203+
185204
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
186205
static RemoveUnusedField newInstance() => RemoveUnusedField();
187206
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,23 @@ class A {
192192
''');
193193
}
194194

195+
Future<void> test_unusedField_notUsed_assign_nested() async {
196+
await resolveTestCode(r'''
197+
class A {
198+
var _f;
199+
main() {
200+
_f = () { _f = 0; };
201+
}
202+
}
203+
''');
204+
await assertHasFix(r'''
205+
class A {
206+
main() {
207+
}
208+
}
209+
''');
210+
}
211+
195212
Future<void> test_unusedField_notUsed_compoundAssign() async {
196213
await resolveTestCode(r'''
197214
class A {

0 commit comments

Comments
 (0)