Skip to content

Commit 9f22708

Browse files
authored
Rollup merge of #61654 - Electron-libre:use_slice_patterns_in_rustc, r=oli-obk,Centril
use pattern matching for slices destructuring refs #61542 Use slices pattern where it seems to make sense .
2 parents 6933034 + 0a4504d commit 9f22708

File tree

7 files changed

+56
-67
lines changed

7 files changed

+56
-67
lines changed

src/libsyntax/diagnostics/plugin.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt<'_>,
3333
span: Span,
3434
token_tree: &[TokenTree])
3535
-> Box<dyn MacResult+'cx> {
36-
let code = match (token_tree.len(), token_tree.get(0)) {
37-
(1, Some(&TokenTree::Token(Token { kind: token::Ident(code, _), .. }))) => code,
36+
let code = match token_tree {
37+
[
38+
TokenTree::Token(Token { kind: token::Ident(code, _), .. })
39+
] => code,
3840
_ => unreachable!()
3941
};
4042

@@ -66,22 +68,19 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>,
6668
span: Span,
6769
token_tree: &[TokenTree])
6870
-> Box<dyn MacResult+'cx> {
69-
let (code, description) = match (
70-
token_tree.len(),
71-
token_tree.get(0),
72-
token_tree.get(1),
73-
token_tree.get(2)
74-
) {
75-
(1, Some(&TokenTree::Token(Token { kind: token::Ident(code, _), .. })), None, None) => {
76-
(code, None)
71+
let (code, description) = match token_tree {
72+
[
73+
TokenTree::Token(Token { kind: token::Ident(code, _), .. })
74+
] => {
75+
(*code, None)
76+
},
77+
[
78+
TokenTree::Token(Token { kind: token::Ident(code, _), .. }),
79+
TokenTree::Token(Token { kind: token::Comma, .. }),
80+
TokenTree::Token(Token { kind: token::Literal(token::Lit { symbol, .. }), ..})
81+
] => {
82+
(*code, Some(*symbol))
7783
},
78-
(3, Some(&TokenTree::Token(Token { kind: token::Ident(code, _), .. })),
79-
Some(&TokenTree::Token(Token { kind: token::Comma, .. })),
80-
Some(&TokenTree::Token(Token {
81-
kind: token::Literal(token::Lit { symbol, .. }), ..
82-
}))) => {
83-
(code, Some(symbol))
84-
}
8584
_ => unreachable!()
8685
};
8786

src/libsyntax/parse/mod.rs

+27-37
Original file line numberDiff line numberDiff line change
@@ -424,48 +424,38 @@ mod tests {
424424
string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect();
425425
let tts: &[TokenTree] = &tts[..];
426426

427-
match (tts.len(), tts.get(0), tts.get(1), tts.get(2), tts.get(3)) {
428-
(
429-
4,
430-
Some(&TokenTree::Token(Token {
431-
kind: token::Ident(name_macro_rules, false), ..
432-
})),
433-
Some(&TokenTree::Token(Token { kind: token::Not, .. })),
434-
Some(&TokenTree::Token(Token { kind: token::Ident(name_zip, false), .. })),
435-
Some(&TokenTree::Delimited(_, macro_delim, ref macro_tts)),
436-
)
437-
if name_macro_rules == sym::macro_rules && name_zip.as_str() == "zip" => {
427+
match tts {
428+
[
429+
TokenTree::Token(Token { kind: token::Ident(name_macro_rules, false), .. }),
430+
TokenTree::Token(Token { kind: token::Not, .. }),
431+
TokenTree::Token(Token { kind: token::Ident(name_zip, false), .. }),
432+
TokenTree::Delimited(_, macro_delim, macro_tts)
433+
]
434+
if name_macro_rules == &sym::macro_rules && name_zip.as_str() == "zip" => {
438435
let tts = &macro_tts.trees().collect::<Vec<_>>();
439-
match (tts.len(), tts.get(0), tts.get(1), tts.get(2)) {
440-
(
441-
3,
442-
Some(&TokenTree::Delimited(_, first_delim, ref first_tts)),
443-
Some(&TokenTree::Token(Token { kind: token::FatArrow, .. })),
444-
Some(&TokenTree::Delimited(_, second_delim, ref second_tts)),
445-
)
446-
if macro_delim == token::Paren => {
436+
match &tts[..] {
437+
[
438+
TokenTree::Delimited(_, first_delim, first_tts),
439+
TokenTree::Token(Token { kind: token::FatArrow, .. }),
440+
TokenTree::Delimited(_, second_delim, second_tts),
441+
]
442+
if macro_delim == &token::Paren => {
447443
let tts = &first_tts.trees().collect::<Vec<_>>();
448-
match (tts.len(), tts.get(0), tts.get(1)) {
449-
(
450-
2,
451-
Some(&TokenTree::Token(Token { kind: token::Dollar, .. })),
452-
Some(&TokenTree::Token(Token {
453-
kind: token::Ident(name, false), ..
454-
})),
455-
)
456-
if first_delim == token::Paren && name.as_str() == "a" => {},
444+
match &tts[..] {
445+
[
446+
TokenTree::Token(Token { kind: token::Dollar, .. }),
447+
TokenTree::Token(Token { kind: token::Ident(name, false), .. }),
448+
]
449+
if first_delim == &token::Paren && name.as_str() == "a" => {},
457450
_ => panic!("value 3: {:?} {:?}", first_delim, first_tts),
458451
}
459452
let tts = &second_tts.trees().collect::<Vec<_>>();
460-
match (tts.len(), tts.get(0), tts.get(1)) {
461-
(
462-
2,
463-
Some(&TokenTree::Token(Token { kind: token::Dollar, .. })),
464-
Some(&TokenTree::Token(Token {
465-
kind: token::Ident(name, false), ..
466-
})),
467-
)
468-
if second_delim == token::Paren && name.as_str() == "a" => {},
453+
match &tts[..] {
454+
[
455+
TokenTree::Token(Token { kind: token::Dollar, .. }),
456+
TokenTree::Token(Token { kind: token::Ident(name, false), .. }),
457+
]
458+
if second_delim == &token::Paren && name.as_str() == "a" => {},
469459
_ => panic!("value 4: {:?} {:?}", second_delim, second_tts),
470460
}
471461
},

src/libsyntax_ext/deriving/cmp/ord.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<
8282
// }
8383

8484
let new = {
85-
let other_f = match (other_fs.len(), other_fs.get(0)) {
86-
(1, Some(o_f)) => o_f,
85+
let other_f = match other_fs {
86+
[o_f] => o_f,
8787
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`"),
8888
};
8989

src/libsyntax_ext/deriving/cmp/partial_eq.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt<'_>,
2525
-> P<Expr>
2626
{
2727
let op = |cx: &mut ExtCtxt<'_>, span: Span, self_f: P<Expr>, other_fs: &[P<Expr>]| {
28-
let other_f = match (other_fs.len(), other_fs.get(0)) {
29-
(1, Some(o_f)) => o_f,
28+
let other_f = match other_fs {
29+
[o_f] => o_f,
3030
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialEq)`"),
3131
};
3232

src/libsyntax_ext/deriving/cmp/partial_ord.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
143143
// }
144144

145145
let new = {
146-
let other_f = match (other_fs.len(), other_fs.get(0)) {
147-
(1, Some(o_f)) => o_f,
146+
let other_f = match other_fs {
147+
[o_f] => o_f,
148148
_ => {
149149
cx.span_bug(span,
150150
"not exactly 2 arguments in `derive(PartialOrd)`")
@@ -193,8 +193,8 @@ fn cs_op(less: bool,
193193
};
194194

195195
let par_cmp = |cx: &mut ExtCtxt<'_>, span, self_f: P<Expr>, other_fs: &[P<Expr>], default| {
196-
let other_f = match (other_fs.len(), other_fs.get(0)) {
197-
(1, Some(o_f)) => o_f,
196+
let other_f = match other_fs {
197+
[o_f] => o_f,
198198
_ => cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`"),
199199
};
200200

src/libsyntax_ext/deriving/hash.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt<'_>,
5252
}
5353

5454
fn hash_substructure(cx: &mut ExtCtxt<'_>, trait_span: Span, substr: &Substructure<'_>) -> P<Expr> {
55-
let state_expr = match (substr.nonself_args.len(), substr.nonself_args.get(0)) {
56-
(1, Some(o_f)) => o_f,
55+
let state_expr = match &substr.nonself_args {
56+
&[o_f] => o_f,
5757
_ => {
5858
cx.span_bug(trait_span,
5959
"incorrect number of arguments in `derive(Hash)`")

src/libsyntax_ext/trace_macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ pub fn expand_trace_macros(cx: &mut ExtCtxt<'_>,
1616
feature_gate::EXPLAIN_TRACE_MACROS);
1717
}
1818

19-
match (tt.len(), tt.first()) {
20-
(1, Some(TokenTree::Token(token))) if token.is_keyword(kw::True) => {
19+
match tt {
20+
[TokenTree::Token(token)] if token.is_keyword(kw::True) => {
2121
cx.set_trace_macros(true);
2222
}
23-
(1, Some(TokenTree::Token(token))) if token.is_keyword(kw::False) => {
23+
[TokenTree::Token(token)] if token.is_keyword(kw::False) => {
2424
cx.set_trace_macros(false);
2525
}
2626
_ => cx.span_err(sp, "trace_macros! accepts only `true` or `false`"),

0 commit comments

Comments
 (0)