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 <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2021-09-23 16:34:03 +00:00 committed by commit-bot@chromium.org
parent 19b7a3e2b5
commit 57e71e33c5
2 changed files with 37 additions and 1 deletions

View file

@ -116,8 +116,9 @@ class RemoveUnusedField extends _RemoveUnused {
sourceRanges.add(sourceRange);
}
final uniqueSourceRanges = _uniqueSourceRanges(sourceRanges);
await builder.addDartFileEdit(file, (builder) {
for (var sourceRange in sourceRanges) {
for (var sourceRange in uniqueSourceRanges) {
builder.addDeletion(sourceRange);
}
});
@ -182,6 +183,24 @@ class RemoveUnusedField extends _RemoveUnused {
}
}
/// Return [SourceRange]s that are not covered by other in [ranges].
/// If there is any intersection, it must be fully covered, never partially.
List<SourceRange> _uniqueSourceRanges(List<SourceRange> ranges) {
var result = <SourceRange>[];
candidates:
for (var candidate in ranges) {
for (var other in ranges) {
if (identical(candidate, other)) {
continue;
} else if (candidate.coveredBy(other)) {
continue candidates;
}
}
result.add(candidate);
}
return result;
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static RemoveUnusedField newInstance() => RemoveUnusedField();
}

View file

@ -192,6 +192,23 @@ class A {
''');
}
Future<void> test_unusedField_notUsed_assign_nested() async {
await resolveTestCode(r'''
class A {
var _f;
main() {
_f = () { _f = 0; };
}
}
''');
await assertHasFix(r'''
class A {
main() {
}
}
''');
}
Future<void> test_unusedField_notUsed_compoundAssign() async {
await resolveTestCode(r'''
class A {