Skip to content

Commit

Permalink
Fix dead code removal when multiple cases share a body.
Browse files Browse the repository at this point in the history
Fixes dart-lang#56485.

Bug: dart-lang#56485
Change-Id: Idea150d9b00bdf90ecff184da14ecbdbe5902a30
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/380901
Commit-Queue: Paul Berry <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Aug 16, 2024
1 parent e5704ea commit eb887c2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,19 @@ class RemoveDeadCode extends ResolvedCorrectionProducer {
if (forStatement is! ForStatement) return;

await _computeForStatementParts(builder, forStatement, coveringNode);
} else if (coveringNode is SwitchPatternCase) {
} else if (coveringNode is SwitchMember) {
var parent = coveringNode.parent as SwitchStatement;
var memberIndex = parent.members.indexOf(coveringNode);
Token? overrideEnd;
if (memberIndex > 0 &&
parent.members[memberIndex - 1].statements.isEmpty) {
// Previous member "falls through" to the one being removed, so don't
// remove the statements.
overrideEnd = coveringNode.colon;
}
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(range.deletionRange(coveringNode));
builder.addDeletion(
range.deletionRange(coveringNode, overrideEnd: overrideEnd));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,30 @@ void f() {
''');
}

Future<void> test_switchCase_sharedStatements_last() async {
await resolveTestCode('''
void f() {
var m = 5;
switch(m) {
case 5:
case 3:
case 5:
break;
}
}
''');
await assertHasFix('''
void f() {
var m = 5;
switch(m) {
case 5:
case 3:
break;
}
}
''');
}

Future<void> test_switchCase_uniqueStatements() async {
await resolveTestCode('''
void f() {
Expand Down
10 changes: 7 additions & 3 deletions pkg/analyzer_plugin/lib/utilities/range_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ class RangeFactory {

/// Return the deletion range of the [node], considering the spaces and
/// comments before and after it.
SourceRange deletionRange(AstNode node) {
///
/// If a non-`null` [overrideEnd] is supplied, it will be used in place of
/// [AstNode.endToken] to determine the range of tokens to delete.
SourceRange deletionRange(AstNode node, {Token? overrideEnd}) {
var begin = node.beginToken;
begin = begin.precedingComments ?? begin;

var end = node.endToken.next!;
var initialEndToken = overrideEnd ?? node.endToken;
var end = initialEndToken.next!;
end = end.precedingComments ?? end;

int startOffset;
Expand All @@ -84,7 +88,7 @@ class RangeFactory {
} else {
startOffset = begin.previous!.end;
}
endOffset = node.endToken.end;
endOffset = initialEndToken.end;
} else {
startOffset = begin.offset;
endOffset = end.offset;
Expand Down

0 comments on commit eb887c2

Please sign in to comment.