Skip to content

Commit c5af544

Browse files
authored
refactor: Simplify some conditional logic to remove unreachable branches. (#105)
``` if !lineRange.empty() { finishGroup() } ``` guarantees that `lineRange.empty()` will be true after it (`finishGroup` zeroes out `lineRange`). That lets us get rid of one of the branches in the `metadata.opts.Group` conditional. But now the remaining branch shares a lot in common with the `else` branch, so we can simplify a bit further.
1 parent 975d9a8 commit c5af544

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

keepsorted/line_group.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,13 @@ func groupLines(lines []string, metadata blockMetadata) []*lineGroup {
137137
finishGroup()
138138
}
139139

140-
if metadata.opts.Group && strings.Contains(l, metadata.startDirective) {
141-
// We don't need to check for end directives here because this makes
142-
// numUnmatchedStartDirectives > 0, so we'll take the code path above through appendLine.
143-
if lineRange.empty() {
144-
commentRange.append(i)
145-
countStartDirectives(l)
146-
} else {
147-
appendLine(i, l)
148-
}
149-
} else {
150-
commentRange.append(i)
140+
commentRange.append(i)
141+
if metadata.opts.Group {
142+
// Note: This will not count end directives. If this call ever finds a
143+
// start directive, it will set numUnmatchedStartDirectives > 0 and then
144+
// we will enter the branch above where we'll count end directives via
145+
// its appendLine call.
146+
countStartDirectives(l)
151147
}
152148
} else {
153149
if !lineRange.empty() {

0 commit comments

Comments
 (0)