Replace an unnecessary slice pattern with has_dot_dot: bool

This commit is contained in:
Zalathar 2024-08-02 20:09:12 +10:00
parent 4cd800503f
commit e98e19e491
2 changed files with 10 additions and 14 deletions

View File

@ -872,7 +872,7 @@ fn hoist_witness_pat(&self, pat: &WitnessPat<'p, 'tcx>) -> print::Pat<'tcx> {
match slice.kind {
SliceKind::FixedLen(_) => PatKind::Slice {
prefix: subpatterns.collect(),
slice: None,
has_dot_dot: false,
suffix: Box::new([]),
},
SliceKind::VarLen(prefix, _) => {
@ -893,10 +893,9 @@ fn hoist_witness_pat(&self, pat: &WitnessPat<'p, 'tcx>) -> print::Pat<'tcx> {
}
}
let suffix: Box<[_]> = subpatterns.collect();
let wild = Pat { ty: pat.ty().inner(), kind: PatKind::Wild };
PatKind::Slice {
prefix: prefix.into_boxed_slice(),
slice: Some(Box::new(wild)),
has_dot_dot: true,
suffix,
}
}

View File

@ -50,7 +50,9 @@ pub(crate) enum PatKind<'tcx> {
Slice {
prefix: Box<[Box<Pat<'tcx>>]>,
slice: Option<Box<Pat<'tcx>>>,
/// True if this slice-like pattern should include a `..` between the
/// prefix and suffix.
has_dot_dot: bool,
suffix: Box<[Box<Pat<'tcx>>]>,
},
@ -68,8 +70,8 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
PatKind::Deref { ref subpattern } => write_ref_like(f, self.ty, subpattern),
PatKind::Constant { value } => write!(f, "{value}"),
PatKind::Range(ref range) => write!(f, "{range}"),
PatKind::Slice { ref prefix, ref slice, ref suffix } => {
write_slice_like(f, prefix, slice, suffix)
PatKind::Slice { ref prefix, has_dot_dot, ref suffix } => {
write_slice_like(f, prefix, has_dot_dot, suffix)
}
}
}
@ -194,7 +196,7 @@ fn write_ref_like<'tcx>(
fn write_slice_like<'tcx>(
f: &mut impl fmt::Write,
prefix: &[Box<Pat<'tcx>>],
slice: &Option<Box<Pat<'tcx>>>,
has_dot_dot: bool,
suffix: &[Box<Pat<'tcx>>],
) -> fmt::Result {
let mut start_or_comma = start_or_comma();
@ -202,13 +204,8 @@ fn write_slice_like<'tcx>(
for p in prefix.iter() {
write!(f, "{}{}", start_or_comma(), p)?;
}
if let Some(ref slice) = *slice {
write!(f, "{}", start_or_comma())?;
match slice.kind {
PatKind::Wild => {}
_ => write!(f, "{slice}")?,
}
write!(f, "..")?;
if has_dot_dot {
write!(f, "{}..", start_or_comma())?;
}
for p in suffix.iter() {
write!(f, "{}{}", start_or_comma(), p)?;