Skip to content

Commit

Permalink
Fix RemoveUnusedField when some SourceRange(s) being removed are full…
Browse files Browse the repository at this point in the history
…y 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]>
  • Loading branch information
scheglov authored and [email protected] committed Sep 23, 2021
1 parent 19b7a3e commit 57e71e3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 57e71e3

Please sign in to comment.