Skip to content

Commit 949b259

Browse files
author
Michael Wright
committed
Change Bound to EndBound
Only the end bounds of ranges can actually be included or excluded. This commit changes the SpannedRange type to reflect that. Update `Kind::value` to and `Kind::cmp` for this change. `Kind::cmp` gets flipped to check value first and then the bound details and is much shorter.
1 parent 98416d7 commit 949b259

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

clippy_lints/src/matches.rs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,8 +1614,8 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
16141614
let rhs_val = rhs.int_value(cx, ty)?;
16151615

16161616
let rhs_bound = match range_end {
1617-
RangeEnd::Included => Bound::Included(rhs_val),
1618-
RangeEnd::Excluded => Bound::Excluded(rhs_val),
1617+
RangeEnd::Included => EndBound::Included(rhs_val),
1618+
RangeEnd::Excluded => EndBound::Excluded(rhs_val),
16191619
};
16201620
return Some(SpannedRange {
16211621
span: pat.span,
@@ -1627,7 +1627,7 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
16271627
let value = constant_full_int(cx, cx.typeck_results(), value)?;
16281628
return Some(SpannedRange {
16291629
span: pat.span,
1630-
node: (value, Bound::Included(value)),
1630+
node: (value, EndBound::Included(value)),
16311631
});
16321632
}
16331633
}
@@ -1636,16 +1636,16 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
16361636
.collect()
16371637
}
16381638

1639-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1640-
pub enum Bound<T> {
1639+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1640+
pub enum EndBound<T> {
16411641
Included(T),
16421642
Excluded(T),
16431643
}
16441644

16451645
#[derive(Debug, Eq, PartialEq)]
16461646
pub struct SpannedRange<T> {
16471647
pub span: Span,
1648-
pub node: (T, Bound<T>),
1648+
pub node: (T, EndBound<T>),
16491649
}
16501650

16511651
// Checks if arm has the form `None => None`
@@ -1701,14 +1701,13 @@ where
17011701
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
17021702
enum Kind<'a, T> {
17031703
Start(T, &'a SpannedRange<T>),
1704-
End(Bound<T>, &'a SpannedRange<T>),
1704+
End(EndBound<T>, &'a SpannedRange<T>),
17051705
}
17061706

17071707
impl<'a, T: Copy> Kind<'a, T> {
1708-
fn value(self) -> Bound<T> {
1708+
fn value(self) -> T {
17091709
match self {
1710-
Kind::Start(t, _) => Bound::Included(t),
1711-
Kind::End(t, _) => t,
1710+
Kind::Start(t, _) | Kind::End(EndBound::Included(t) | EndBound::Excluded(t), _) => t,
17121711
}
17131712
}
17141713
}
@@ -1721,28 +1720,23 @@ where
17211720

17221721
impl<'a, T: Copy + Ord> Ord for Kind<'a, T> {
17231722
fn cmp(&self, other: &Self) -> Ordering {
1724-
match (self.value(), other.value()) {
1725-
(Bound::Included(a), Bound::Included(b)) | (Bound::Excluded(a), Bound::Excluded(b)) => {
1726-
let value_cmp = a.cmp(&b);
1727-
// In the case of ties, starts come before ends
1728-
if value_cmp == Ordering::Equal {
1729-
match (self, other) {
1730-
(Kind::Start(..), Kind::End(..)) => Ordering::Less,
1731-
(Kind::End(..), Kind::Start(..)) => Ordering::Greater,
1732-
_ => Ordering::Equal,
1733-
}
1734-
} else {
1735-
value_cmp
1736-
}
1737-
},
1738-
(Bound::Included(a), Bound::Excluded(b)) => match a.cmp(&b) {
1739-
Ordering::Equal => Ordering::Greater,
1740-
other => other,
1741-
},
1742-
(Bound::Excluded(a), Bound::Included(b)) => match a.cmp(&b) {
1743-
Ordering::Equal => Ordering::Less,
1744-
other => other,
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,
17451738
},
1739+
other => other,
17461740
}
17471741
}
17481742
}
@@ -2224,29 +2218,29 @@ fn test_overlapping() {
22242218
};
22252219

22262220
assert_eq!(None, overlapping::<u8>(&[]));
2227-
assert_eq!(None, overlapping(&[sp(1, Bound::Included(4))]));
2221+
assert_eq!(None, overlapping(&[sp(1, EndBound::Included(4))]));
22282222
assert_eq!(
22292223
None,
2230-
overlapping(&[sp(1, Bound::Included(4)), sp(5, Bound::Included(6))])
2224+
overlapping(&[sp(1, EndBound::Included(4)), sp(5, EndBound::Included(6))])
22312225
);
22322226
assert_eq!(
22332227
None,
22342228
overlapping(&[
2235-
sp(1, Bound::Included(4)),
2236-
sp(5, Bound::Included(6)),
2237-
sp(10, Bound::Included(11))
2229+
sp(1, EndBound::Included(4)),
2230+
sp(5, EndBound::Included(6)),
2231+
sp(10, EndBound::Included(11))
22382232
],)
22392233
);
22402234
assert_eq!(
2241-
Some((&sp(1, Bound::Included(4)), &sp(3, Bound::Included(6)))),
2242-
overlapping(&[sp(1, Bound::Included(4)), sp(3, Bound::Included(6))])
2235+
Some((&sp(1, EndBound::Included(4)), &sp(3, EndBound::Included(6)))),
2236+
overlapping(&[sp(1, EndBound::Included(4)), sp(3, EndBound::Included(6))])
22432237
);
22442238
assert_eq!(
2245-
Some((&sp(5, Bound::Included(6)), &sp(6, Bound::Included(11)))),
2239+
Some((&sp(5, EndBound::Included(6)), &sp(6, EndBound::Included(11)))),
22462240
overlapping(&[
2247-
sp(1, Bound::Included(4)),
2248-
sp(5, Bound::Included(6)),
2249-
sp(6, Bound::Included(11))
2241+
sp(1, EndBound::Included(4)),
2242+
sp(5, EndBound::Included(6)),
2243+
sp(6, EndBound::Included(11))
22502244
],)
22512245
);
22522246
}

0 commit comments

Comments
 (0)