Skip to content

Commit 5fb099d

Browse files
use pattern matching for slices destructuring
1 parent c1c60d2 commit 5fb099d

File tree

7 files changed

+45
-59
lines changed

7 files changed

+45
-59
lines changed

src/libsyntax/diagnostics/plugin.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ 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+
&[TokenTree::Token(Token { kind: token::Ident(code, _), .. })] => code,
3838
_ => unreachable!()
3939
};
4040

@@ -66,22 +66,15 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>,
6666
span: Span,
6767
token_tree: &[TokenTree])
6868
-> 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) => {
69+
let (code, description) = match token_tree {
70+
&[TokenTree::Token(Token { kind: token::Ident(code, _), .. })] => {
7671
(code, None)
7772
},
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-
}))) => {
73+
&[TokenTree::Token(Token { kind: token::Ident(code, _), .. }),
74+
TokenTree::Token(Token { kind: token::Comma, .. }),
75+
TokenTree::Token(Token { kind: token::Literal(token::Lit { symbol, .. }), ..})] => {
8376
(code, Some(symbol))
84-
}
77+
},
8578
_ => unreachable!()
8679
};
8780

src/libsyntax/parse/mod.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -424,47 +424,40 @@ 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-
)
427+
match tts {
428+
&[TokenTree::Token(Token {kind: token::Ident(name_macro_rules, false), ..}),
429+
TokenTree::Token(Token { kind: token::Not, .. }),
430+
TokenTree::Token(Token { kind: token::Ident(name_zip, false), .. }),
431+
TokenTree::Delimited(_, macro_delim, ref macro_tts)
432+
]
437433
if name_macro_rules == sym::macro_rules && name_zip.as_str() == "zip" => {
438434
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-
)
435+
match tts {
436+
&[
437+
TokenTree::Delimited(_, first_delim, ref first_tts),
438+
TokenTree::Token(Token { kind: token::FatArrow, .. }),
439+
TokenTree::Delimited(_, second_delim, ref second_tts),
440+
]
446441
if macro_delim == token::Paren => {
447442
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 {
443+
match tts {
444+
&[
445+
TokenTree::Token(Token { kind: token::Dollar, .. }),
446+
TokenTree::Token(Token {
453447
kind: token::Ident(name, false), ..
454-
})),
455-
)
448+
}),
449+
]
456450
if first_delim == token::Paren && name.as_str() == "a" => {},
457451
_ => panic!("value 3: {:?} {:?}", first_delim, first_tts),
458452
}
459453
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 {
454+
match tts {
455+
&[
456+
TokenTree::Token(Token { kind: token::Dollar, .. }),
457+
TokenTree::Token(Token {
465458
kind: token::Ident(name, false), ..
466-
})),
467-
)
459+
}),
460+
]
468461
if second_delim == token::Paren && name.as_str() == "a" => {},
469462
_ => panic!("value 4: {:?} {:?}", second_delim, second_tts),
470463
}

src/libsyntax_ext/deriving/cmp/ord.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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)