Skip to content

Commit f829523

Browse files
author
Michael Wright
committed
Simplify range comparison code
Reword the `Kind` type so that the `cmp` function is simpler.
1 parent 949b259 commit f829523

File tree

1 file changed

+24
-41
lines changed

1 file changed

+24
-41
lines changed

clippy_lints/src/matches.rs

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,75 +1698,58 @@ pub fn overlapping<T>(ranges: &[SpannedRange<T>]) -> Option<(&SpannedRange<T>, &
16981698
where
16991699
T: Copy + Ord,
17001700
{
1701-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1702-
enum Kind<'a, T> {
1703-
Start(T, &'a SpannedRange<T>),
1704-
End(EndBound<T>, &'a SpannedRange<T>),
1701+
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
1702+
enum BoundKind {
1703+
EndExcluded,
1704+
Start,
1705+
EndIncluded,
17051706
}
17061707

1707-
impl<'a, T: Copy> Kind<'a, T> {
1708-
fn value(self) -> T {
1709-
match self {
1710-
Kind::Start(t, _) | Kind::End(EndBound::Included(t) | EndBound::Excluded(t), _) => t,
1711-
}
1712-
}
1713-
}
1708+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1709+
struct RangeBound<'a, T>(T, BoundKind, &'a SpannedRange<T>);
17141710

1715-
impl<'a, T: Copy + Ord> PartialOrd for Kind<'a, T> {
1711+
impl<'a, T: Copy + Ord> PartialOrd for RangeBound<'a, T> {
17161712
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
17171713
Some(self.cmp(other))
17181714
}
17191715
}
17201716

1721-
impl<'a, T: Copy + Ord> Ord for Kind<'a, T> {
1722-
fn cmp(&self, other: &Self) -> Ordering {
1723-
match self.value().cmp(&other.value()) {
1724-
Ordering::Equal => match (self, other) {
1725-
// End excluded before start and end included
1726-
(Kind::End(EndBound::Excluded(_), _), Kind::Start(..) | Kind::End(EndBound::Included(_), _)) => {
1727-
Ordering::Less
1728-
},
1729-
(Kind::Start(..) | Kind::End(EndBound::Included(_), _), Kind::End(EndBound::Excluded(_), _)) => {
1730-
Ordering::Greater
1731-
},
1732-
1733-
// Start before end included
1734-
(Kind::Start(..), Kind::End(EndBound::Included(_), _)) => Ordering::Less,
1735-
(Kind::End(EndBound::Included(_), _), Kind::Start(..)) => Ordering::Greater,
1736-
1737-
_ => Ordering::Equal,
1738-
},
1739-
other => other,
1740-
}
1717+
impl<'a, T: Copy + Ord> Ord for RangeBound<'a, T> {
1718+
fn cmp(&self, RangeBound(other_value, other_kind, _): &Self) -> Ordering {
1719+
let RangeBound(self_value, self_kind, _) = *self;
1720+
(self_value, self_kind).cmp(&(*other_value, *other_kind))
17411721
}
17421722
}
17431723

17441724
let mut values = Vec::with_capacity(2 * ranges.len());
17451725

1746-
for r in ranges {
1747-
values.push(Kind::Start(r.node.0, r));
1748-
values.push(Kind::End(r.node.1, r));
1726+
for r @ SpannedRange { node: (start, end), .. } in ranges {
1727+
values.push(RangeBound(*start, BoundKind::Start, r));
1728+
values.push(match end {
1729+
EndBound::Excluded(val) => RangeBound(*val, BoundKind::EndExcluded, r),
1730+
EndBound::Included(val) => RangeBound(*val, BoundKind::EndIncluded, r),
1731+
});
17491732
}
17501733

17511734
values.sort();
17521735

17531736
let mut started = vec![];
17541737

1755-
for value in values {
1756-
match value {
1757-
Kind::Start(_, r) => started.push(r),
1758-
Kind::End(_, er) => {
1738+
for RangeBound(_, kind, r) in values {
1739+
match kind {
1740+
BoundKind::Start => started.push(r),
1741+
BoundKind::EndExcluded | BoundKind::EndIncluded => {
17591742
let mut overlap = None;
17601743

17611744
while let Some(sr) = started.pop() {
1762-
if sr == er {
1745+
if sr == r {
17631746
break;
17641747
}
17651748
overlap = Some(sr);
17661749
}
17671750

17681751
if let Some(sr) = overlap {
1769-
return Some((er, sr));
1752+
return Some((r, sr));
17701753
}
17711754
},
17721755
}

0 commit comments

Comments
 (0)