diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart index 25f5881541b6..152ec5008bef 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_dead_code.dart @@ -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)); }); } } diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_dead_code_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_dead_code_test.dart index 6e60affcfbf1..5baf4ace10d0 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/remove_dead_code_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/remove_dead_code_test.dart @@ -493,6 +493,30 @@ void f() { '''); } + Future 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 test_switchCase_uniqueStatements() async { await resolveTestCode(''' void f() { diff --git a/pkg/analyzer_plugin/lib/utilities/range_factory.dart b/pkg/analyzer_plugin/lib/utilities/range_factory.dart index 20a8c081171a..359a03550ddf 100644 --- a/pkg/analyzer_plugin/lib/utilities/range_factory.dart +++ b/pkg/analyzer_plugin/lib/utilities/range_factory.dart @@ -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; @@ -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;