Skip to content

Commit b6070a0

Browse files
committed
Use more slice patterns inside the compiler
1 parent 60d1465 commit b6070a0

File tree

41 files changed

+191
-224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+191
-224
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ impl Pat {
585585
}
586586
// A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
587587
// when `P` can be reparsed as a type `T`.
588-
PatKind::Slice(pats) if pats.len() == 1 => pats[0].to_ty().map(TyKind::Slice)?,
588+
PatKind::Slice(pats) if let [pat] = &**pats => pat.to_ty().map(TyKind::Slice)?,
589589
// A tuple pattern `(P0, .., Pn)` can be reparsed as `(T0, .., Tn)`
590590
// assuming `T0` to `Tn` are all syntactically valid as types.
591591
PatKind::Tuple(pats) => {
@@ -1187,8 +1187,8 @@ impl Expr {
11871187
/// Does not ensure that the path resolves to a const param, the caller should check this.
11881188
pub fn is_potential_trivial_const_arg(&self) -> bool {
11891189
let this = if let ExprKind::Block(block, None) = &self.kind
1190-
&& block.stmts.len() == 1
1191-
&& let StmtKind::Expr(expr) = &block.stmts[0].kind
1190+
&& let [stmt] = &*block.stmts
1191+
&& let StmtKind::Expr(expr) = &stmt.kind
11921192
{
11931193
expr
11941194
} else {
@@ -1248,7 +1248,7 @@ impl Expr {
12481248
expr.to_ty().map(|ty| TyKind::Array(ty, expr_len.clone()))?
12491249
}
12501250

1251-
ExprKind::Array(exprs) if exprs.len() == 1 => exprs[0].to_ty().map(TyKind::Slice)?,
1251+
ExprKind::Array(exprs) if let [expr] = &**exprs => expr.to_ty().map(TyKind::Slice)?,
12521252

12531253
ExprKind::Tup(exprs) => {
12541254
let tys = exprs.iter().map(|expr| expr.to_ty()).collect::<Option<ThinVec<_>>>()?;

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
275275
// FIXME(fn_delegation): Alternatives for target expression lowering:
276276
// https://github.com/rust-lang/rfcs/pull/3530#issuecomment-2197170600.
277277
fn lower_target_expr(&mut self, block: &Block) -> hir::Expr<'hir> {
278-
if block.stmts.len() == 1
279-
&& let StmtKind::Expr(expr) = &block.stmts[0].kind
278+
if let [stmt] = &*block.stmts
279+
&& let StmtKind::Expr(expr) = &stmt.kind
280280
{
281281
return self.lower_expr_mut(expr);
282282
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
502502
if !self.is_beginning_of_line() {
503503
self.word(" ");
504504
}
505-
if cmnt.lines.len() == 1 {
506-
self.word(cmnt.lines[0].clone());
505+
if let [line] = &*cmnt.lines {
506+
self.word(line.clone());
507507
self.hardbreak()
508508
} else {
509509
self.visual_align();

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,8 +783,8 @@ impl<'a> State<'a> {
783783
}
784784
if items.is_empty() {
785785
self.word("{}");
786-
} else if items.len() == 1 {
787-
self.print_use_tree(&items[0].0);
786+
} else if let [(item, _)] = &**items {
787+
self.print_use_tree(item);
788788
} else {
789789
self.cbox(INDENT_UNIT);
790790
self.word("{");

compiler/rustc_attr/src/builtin.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,12 @@ pub fn eval_condition(
665665
res & eval_condition(mi.meta_item().unwrap(), sess, features, eval)
666666
}),
667667
sym::not => {
668-
if mis.len() != 1 {
668+
let [mi] = &**mis else {
669669
dcx.emit_err(session_diagnostics::ExpectedOneCfgPattern { span: cfg.span });
670670
return false;
671-
}
671+
};
672672

673-
!eval_condition(mis[0].meta_item().unwrap(), sess, features, eval)
673+
!eval_condition(mi.meta_item().unwrap(), sess, features, eval)
674674
}
675675
sym::target => {
676676
if let Some(features) = features
@@ -1051,10 +1051,10 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10511051
MetaItemKind::List(nested_items) => {
10521052
if meta_item.has_name(sym::align) {
10531053
recognised = true;
1054-
if nested_items.len() == 1 {
1054+
if let [nested_item] = &**nested_items {
10551055
sess.dcx().emit_err(
10561056
session_diagnostics::IncorrectReprFormatExpectInteger {
1057-
span: nested_items[0].span(),
1057+
span: nested_item.span(),
10581058
},
10591059
);
10601060
} else {
@@ -1066,10 +1066,10 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10661066
}
10671067
} else if meta_item.has_name(sym::packed) {
10681068
recognised = true;
1069-
if nested_items.len() == 1 {
1069+
if let [nested_item] = &**nested_items {
10701070
sess.dcx().emit_err(
10711071
session_diagnostics::IncorrectReprFormatPackedExpectInteger {
1072-
span: nested_items[0].span(),
1072+
span: nested_item.span(),
10731073
},
10741074
);
10751075
} else {

compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ impl OutlivesSuggestionBuilder {
206206

207207
// If there is exactly one suggestable constraints, then just suggest it. Otherwise, emit a
208208
// list of diagnostics.
209-
let mut diag = if suggested.len() == 1 {
210-
mbcx.dcx().struct_help(match suggested.last().unwrap() {
209+
let mut diag = if let [constraint] = &*suggested {
210+
mbcx.dcx().struct_help(match constraint {
211211
SuggestedConstraint::Outlives(a, bs) => {
212212
let bs: SmallVec<[String; 2]> = bs.iter().map(|r| r.to_string()).collect();
213213
format!("add bound `{a}: {}`", bs.join(" + "))

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,10 +745,9 @@ fn expand_preparsed_asm(
745745
unused_operands.push((args.operands[idx].1, msg));
746746
}
747747
}
748-
match unused_operands.len() {
749-
0 => {}
750-
1 => {
751-
let (sp, msg) = unused_operands.into_iter().next().unwrap();
748+
match *unused_operands.as_slice() {
749+
[] => {}
750+
[(sp, msg)] => {
752751
ecx.dcx()
753752
.struct_span_err(sp, msg)
754753
.with_span_label(sp, msg)

compiler/rustc_builtin_macros/src/deriving/default.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,20 @@ fn validate_default_attribute(
181181
attr::filter_by_name(&default_variant.attrs, kw::Default).collect();
182182

183183
let attr = match attrs.as_slice() {
184-
[attr] => attr,
185184
[] => cx.dcx().bug(
186185
"this method must only be called with a variant that has a `#[default]` attribute",
187186
),
188-
[first, rest @ ..] => {
187+
[attr] => attr,
188+
[first, first_rest, rest @ ..] => {
189189
let sugg = errors::MultipleDefaultAttrsSugg {
190190
spans: rest.iter().map(|attr| attr.span).collect(),
191191
};
192192
let guar = cx.dcx().emit_err(errors::MultipleDefaultAttrs {
193193
span: default_variant.ident.span,
194194
first: first.span,
195-
first_rest: rest[0].span,
195+
first_rest: first_rest.span,
196196
rest: rest.iter().map(|attr| attr.span).collect::<Vec<_>>().into(),
197-
only_one: rest.len() == 1,
197+
only_one: rest.is_empty(),
198198
sugg,
199199
});
200200

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ impl BlockOrExpr {
378378
None => cx.expr_block(cx.block(span, ThinVec::new())),
379379
Some(expr) => expr,
380380
}
381-
} else if self.0.len() == 1
382-
&& let ast::StmtKind::Expr(expr) = &self.0[0].kind
381+
} else if let [stmt] = &*self.0
382+
&& let ast::StmtKind::Expr(expr) = &stmt.kind
383383
&& self.1.is_none()
384384
{
385385
// There's only a single statement expression. Pull it out.
@@ -1273,15 +1273,15 @@ impl<'a> MethodDef<'a> {
12731273
}
12741274
FieldlessVariantsStrategy::Default => (),
12751275
}
1276-
} else if variants.len() == 1 {
1276+
} else if let [variant] = &**variants {
12771277
// If there is a single variant, we don't need an operation on
12781278
// the discriminant(s). Just use the most degenerate result.
12791279
return self.call_substructure_method(
12801280
cx,
12811281
trait_,
12821282
type_ident,
12831283
nonselflike_args,
1284-
&EnumMatching(0, &variants[0], Vec::new()),
1284+
&EnumMatching(0, variant, Vec::new()),
12851285
);
12861286
}
12871287
}

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ fn make_format_args(
180180
Ok((mut err, suggested)) => {
181181
if !suggested {
182182
if let ExprKind::Block(block, None) = &efmt.kind
183-
&& block.stmts.len() == 1
184-
&& let StmtKind::Expr(expr) = &block.stmts[0].kind
183+
&& let [stmt] = &*block.stmts
184+
&& let StmtKind::Expr(expr) = &stmt.kind
185185
&& let ExprKind::Path(None, path) = &expr.kind
186186
&& path.is_potential_trivial_const_arg()
187187
{
@@ -196,8 +196,8 @@ fn make_format_args(
196196
} else {
197197
let sugg_fmt = match args.explicit_args().len() {
198198
0 => "{}".to_string(),
199-
_ => {
200-
format!("{}{{}}", "{} ".repeat(args.explicit_args().len()))
199+
count => {
200+
format!("{}{{}}", "{} ".repeat(count))
201201
}
202202
};
203203
err.span_suggestion(

0 commit comments

Comments
 (0)