Skip to content

Commit e98e19e

Browse files
committed
Replace an unnecessary slice pattern with has_dot_dot: bool
1 parent 4cd8005 commit e98e19e

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

compiler/rustc_pattern_analysis/src/rustc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
872872
match slice.kind {
873873
SliceKind::FixedLen(_) => PatKind::Slice {
874874
prefix: subpatterns.collect(),
875-
slice: None,
875+
has_dot_dot: false,
876876
suffix: Box::new([]),
877877
},
878878
SliceKind::VarLen(prefix, _) => {
@@ -893,10 +893,9 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
893893
}
894894
}
895895
let suffix: Box<[_]> = subpatterns.collect();
896-
let wild = Pat { ty: pat.ty().inner(), kind: PatKind::Wild };
897896
PatKind::Slice {
898897
prefix: prefix.into_boxed_slice(),
899-
slice: Some(Box::new(wild)),
898+
has_dot_dot: true,
900899
suffix,
901900
}
902901
}

compiler/rustc_pattern_analysis/src/rustc/print.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ pub(crate) enum PatKind<'tcx> {
5050

5151
Slice {
5252
prefix: Box<[Box<Pat<'tcx>>]>,
53-
slice: Option<Box<Pat<'tcx>>>,
53+
/// True if this slice-like pattern should include a `..` between the
54+
/// prefix and suffix.
55+
has_dot_dot: bool,
5456
suffix: Box<[Box<Pat<'tcx>>]>,
5557
},
5658

@@ -68,8 +70,8 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
6870
PatKind::Deref { ref subpattern } => write_ref_like(f, self.ty, subpattern),
6971
PatKind::Constant { value } => write!(f, "{value}"),
7072
PatKind::Range(ref range) => write!(f, "{range}"),
71-
PatKind::Slice { ref prefix, ref slice, ref suffix } => {
72-
write_slice_like(f, prefix, slice, suffix)
73+
PatKind::Slice { ref prefix, has_dot_dot, ref suffix } => {
74+
write_slice_like(f, prefix, has_dot_dot, suffix)
7375
}
7476
}
7577
}
@@ -194,21 +196,16 @@ fn write_ref_like<'tcx>(
194196
fn write_slice_like<'tcx>(
195197
f: &mut impl fmt::Write,
196198
prefix: &[Box<Pat<'tcx>>],
197-
slice: &Option<Box<Pat<'tcx>>>,
199+
has_dot_dot: bool,
198200
suffix: &[Box<Pat<'tcx>>],
199201
) -> fmt::Result {
200202
let mut start_or_comma = start_or_comma();
201203
write!(f, "[")?;
202204
for p in prefix.iter() {
203205
write!(f, "{}{}", start_or_comma(), p)?;
204206
}
205-
if let Some(ref slice) = *slice {
206-
write!(f, "{}", start_or_comma())?;
207-
match slice.kind {
208-
PatKind::Wild => {}
209-
_ => write!(f, "{slice}")?,
210-
}
211-
write!(f, "..")?;
207+
if has_dot_dot {
208+
write!(f, "{}..", start_or_comma())?;
212209
}
213210
for p in suffix.iter() {
214211
write!(f, "{}{}", start_or_comma(), p)?;

0 commit comments

Comments
 (0)