Skip to content

Commit 700c095

Browse files
committed
rustc_builtin_macros: remove ref patterns
... and other pattern matching improvements
1 parent 244990a commit 700c095

16 files changed

+176
-196
lines changed

Diff for: compiler/rustc_builtin_macros/src/alloc_error_handler.rs

+14-23
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,23 @@ pub fn expand(
1717
check_builtin_macro_attribute(ecx, meta_item, sym::alloc_error_handler);
1818

1919
let orig_item = item.clone();
20-
let not_function = || {
21-
ecx.sess
22-
.parse_sess
23-
.span_diagnostic
24-
.span_err(item.span(), "alloc_error_handler must be a function");
25-
vec![orig_item.clone()]
26-
};
2720

2821
// Allow using `#[alloc_error_handler]` on an item statement
2922
// FIXME - if we get deref patterns, use them to reduce duplication here
30-
let (item, is_stmt, sig_span) = match &item {
31-
Annotatable::Item(item) => match item.kind {
32-
ItemKind::Fn(ref fn_kind) => (item, false, ecx.with_def_site_ctxt(fn_kind.sig.span)),
33-
_ => return not_function(),
34-
},
35-
Annotatable::Stmt(stmt) => match &stmt.kind {
36-
StmtKind::Item(item_) => match item_.kind {
37-
ItemKind::Fn(ref fn_kind) => {
38-
(item_, true, ecx.with_def_site_ctxt(fn_kind.sig.span))
39-
}
40-
_ => return not_function(),
41-
},
42-
_ => return not_function(),
43-
},
44-
_ => return not_function(),
45-
};
23+
let (item, is_stmt, sig_span) =
24+
if let Annotatable::Item(item) = &item
25+
&& let ItemKind::Fn(fn_kind) = &item.kind
26+
{
27+
(item, false, ecx.with_def_site_ctxt(fn_kind.sig.span))
28+
} else if let Annotatable::Stmt(stmt) = &item
29+
&& let StmtKind::Item(item) = &stmt.kind
30+
&& let ItemKind::Fn(fn_kind) = &item.kind
31+
{
32+
(item, true, ecx.with_def_site_ctxt(fn_kind.sig.span))
33+
} else {
34+
ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "alloc_error_handler must be a function");
35+
return vec![orig_item.clone()];
36+
};
4637

4738
// Generate a bunch of new items using the AllocFnFactory
4839
let span = ecx.with_def_site_ctxt(item.span);

Diff for: compiler/rustc_builtin_macros/src/assert/context.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,19 @@ impl<'cx, 'a> Context<'cx, 'a> {
191191
///
192192
/// See [Self::manage_initial_capture] and [Self::manage_try_capture]
193193
fn manage_cond_expr(&mut self, expr: &mut P<Expr>) {
194-
match (*expr).kind {
195-
ExprKind::AddrOf(_, mutability, ref mut local_expr) => {
194+
match &mut expr.kind {
195+
ExprKind::AddrOf(_, mutability, local_expr) => {
196196
self.with_is_consumed_management(
197197
matches!(mutability, Mutability::Mut),
198198
|this| this.manage_cond_expr(local_expr)
199199
);
200200
}
201-
ExprKind::Array(ref mut local_exprs) => {
201+
ExprKind::Array(local_exprs) => {
202202
for local_expr in local_exprs {
203203
self.manage_cond_expr(local_expr);
204204
}
205205
}
206-
ExprKind::Binary(ref op, ref mut lhs, ref mut rhs) => {
206+
ExprKind::Binary(op, lhs, rhs) => {
207207
self.with_is_consumed_management(
208208
matches!(
209209
op.node,
@@ -226,56 +226,56 @@ impl<'cx, 'a> Context<'cx, 'a> {
226226
}
227227
);
228228
}
229-
ExprKind::Call(_, ref mut local_exprs) => {
229+
ExprKind::Call(_, local_exprs) => {
230230
for local_expr in local_exprs {
231231
self.manage_cond_expr(local_expr);
232232
}
233233
}
234-
ExprKind::Cast(ref mut local_expr, _) => {
234+
ExprKind::Cast(local_expr, _) => {
235235
self.manage_cond_expr(local_expr);
236236
}
237-
ExprKind::Index(ref mut prefix, ref mut suffix) => {
237+
ExprKind::Index(prefix, suffix) => {
238238
self.manage_cond_expr(prefix);
239239
self.manage_cond_expr(suffix);
240240
}
241-
ExprKind::MethodCall(ref mut call) => {
242-
for arg in call.args.iter_mut() {
241+
ExprKind::MethodCall(call) => {
242+
for arg in &mut call.args {
243243
self.manage_cond_expr(arg);
244244
}
245245
}
246-
ExprKind::Path(_, Path { ref segments, .. }) if let &[ref path_segment] = &segments[..] => {
246+
ExprKind::Path(_, Path { segments, .. }) if let [path_segment] = &segments[..] => {
247247
let path_ident = path_segment.ident;
248248
self.manage_initial_capture(expr, path_ident);
249249
}
250-
ExprKind::Paren(ref mut local_expr) => {
250+
ExprKind::Paren(local_expr) => {
251251
self.manage_cond_expr(local_expr);
252252
}
253-
ExprKind::Range(ref mut prefix, ref mut suffix, _) => {
254-
if let Some(ref mut elem) = prefix {
253+
ExprKind::Range(prefix, suffix, _) => {
254+
if let Some(elem) = prefix {
255255
self.manage_cond_expr(elem);
256256
}
257-
if let Some(ref mut elem) = suffix {
257+
if let Some(elem) = suffix {
258258
self.manage_cond_expr(elem);
259259
}
260260
}
261-
ExprKind::Repeat(ref mut local_expr, ref mut elem) => {
261+
ExprKind::Repeat(local_expr, elem) => {
262262
self.manage_cond_expr(local_expr);
263263
self.manage_cond_expr(&mut elem.value);
264264
}
265-
ExprKind::Struct(ref mut elem) => {
265+
ExprKind::Struct(elem) => {
266266
for field in &mut elem.fields {
267267
self.manage_cond_expr(&mut field.expr);
268268
}
269-
if let StructRest::Base(ref mut local_expr) = elem.rest {
269+
if let StructRest::Base(local_expr) = &mut elem.rest {
270270
self.manage_cond_expr(local_expr);
271271
}
272272
}
273-
ExprKind::Tup(ref mut local_exprs) => {
273+
ExprKind::Tup(local_exprs) => {
274274
for local_expr in local_exprs {
275275
self.manage_cond_expr(local_expr);
276276
}
277277
}
278-
ExprKind::Unary(un_op, ref mut local_expr) => {
278+
ExprKind::Unary(un_op, local_expr) => {
279279
self.with_is_consumed_management(
280280
matches!(un_op, UnOp::Neg | UnOp::Not),
281281
|this| this.manage_cond_expr(local_expr)

Diff for: compiler/rustc_builtin_macros/src/concat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn expand_concat(
2020
for e in es {
2121
match e.kind {
2222
ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
23-
Ok(ast::LitKind::Str(ref s, _) | ast::LitKind::Float(ref s, _)) => {
23+
Ok(ast::LitKind::Str(s, _) | ast::LitKind::Float(s, _)) => {
2424
accumulator.push_str(s.as_str());
2525
}
2626
Ok(ast::LitKind::Char(c)) => {

Diff for: compiler/rustc_builtin_macros/src/concat_bytes.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ pub fn expand_concat_bytes(
144144
let mut missing_literals = vec![];
145145
let mut has_errors = false;
146146
for e in es {
147-
match e.kind {
148-
ast::ExprKind::Array(ref exprs) => {
147+
match &e.kind {
148+
ast::ExprKind::Array(exprs) => {
149149
for expr in exprs {
150150
if let Some(elem) =
151151
handle_array_element(cx, &mut has_errors, &mut missing_literals, expr)
@@ -154,7 +154,7 @@ pub fn expand_concat_bytes(
154154
}
155155
}
156156
}
157-
ast::ExprKind::Repeat(ref expr, ref count) => {
157+
ast::ExprKind::Repeat(expr, count) => {
158158
if let ast::ExprKind::Lit(token_lit) = count.value.kind
159159
&& let Ok(ast::LitKind::Int(count_val, _)) =
160160
ast::LitKind::from_token_lit(token_lit)
@@ -170,7 +170,7 @@ pub fn expand_concat_bytes(
170170
cx.span_err(count.value.span, "repeat count is not a positive number");
171171
}
172172
}
173-
ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
173+
&ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
174174
Ok(ast::LitKind::Byte(val)) => {
175175
accumulator.push(val);
176176
}
@@ -184,7 +184,7 @@ pub fn expand_concat_bytes(
184184
has_errors = true;
185185
}
186186
},
187-
ast::ExprKind::IncludedBytes(ref bytes) => {
187+
ast::ExprKind::IncludedBytes(bytes) => {
188188
accumulator.extend_from_slice(bytes);
189189
}
190190
ast::ExprKind::Err => {

Diff for: compiler/rustc_builtin_macros/src/deriving/clone.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ pub fn expand_deriving_clone(
3232
let bounds;
3333
let substructure;
3434
let is_simple;
35-
match *item {
36-
Annotatable::Item(ref annitem) => match annitem.kind {
37-
ItemKind::Struct(_, Generics { ref params, .. })
38-
| ItemKind::Enum(_, Generics { ref params, .. }) => {
35+
match item {
36+
Annotatable::Item(annitem) => match &annitem.kind {
37+
ItemKind::Struct(_, Generics { params, .. })
38+
| ItemKind::Enum(_, Generics { params, .. }) => {
3939
let container_id = cx.current_expansion.id.expn_data().parent.expect_local();
4040
let has_derive_copy = cx.resolver.has_derive_copy(container_id);
4141
if has_derive_copy
@@ -166,13 +166,13 @@ fn cs_clone(
166166
};
167167

168168
let vdata;
169-
match *substr.fields {
170-
Struct(vdata_, ref af) => {
169+
match substr.fields {
170+
Struct(vdata_, af) => {
171171
ctor_path = cx.path(trait_span, vec![substr.type_ident]);
172172
all_fields = af;
173-
vdata = vdata_;
173+
vdata = *vdata_;
174174
}
175-
EnumMatching(.., variant, ref af) => {
175+
EnumMatching(.., variant, af) => {
176176
ctor_path = cx.path(trait_span, vec![substr.type_ident, variant.ident]);
177177
all_fields = af;
178178
vdata = &variant.data;

Diff for: compiler/rustc_builtin_macros/src/deriving/decodable.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ fn decodable_substructure(
7878
let blkarg = Ident::new(sym::_d, trait_span);
7979
let blkdecoder = cx.expr_ident(trait_span, blkarg);
8080

81-
let expr = match *substr.fields {
82-
StaticStruct(_, ref summary) => {
83-
let nfields = match *summary {
84-
Unnamed(ref fields, _) => fields.len(),
85-
Named(ref fields) => fields.len(),
81+
let expr = match substr.fields {
82+
StaticStruct(_, summary) => {
83+
let nfields = match summary {
84+
Unnamed(fields, _) => fields.len(),
85+
Named(fields) => fields.len(),
8686
};
8787
let fn_read_struct_field_path: Vec<_> =
8888
cx.def_site_path(&[sym::rustc_serialize, sym::Decoder, sym::read_struct_field]);
@@ -119,7 +119,7 @@ fn decodable_substructure(
119119
],
120120
)
121121
}
122-
StaticEnum(_, ref fields) => {
122+
StaticEnum(_, fields) => {
123123
let variant = Ident::new(sym::i, trait_span);
124124

125125
let mut arms = Vec::with_capacity(fields.len() + 1);
@@ -194,10 +194,10 @@ fn decode_static_fields<F>(
194194
where
195195
F: FnMut(&mut ExtCtxt<'_>, Span, Symbol, usize) -> P<Expr>,
196196
{
197-
match *fields {
198-
Unnamed(ref fields, is_tuple) => {
197+
match fields {
198+
Unnamed(fields, is_tuple) => {
199199
let path_expr = cx.expr_path(outer_pat_path);
200-
if !is_tuple {
200+
if !*is_tuple {
201201
path_expr
202202
} else {
203203
let fields = fields
@@ -209,7 +209,7 @@ where
209209
cx.expr_call(trait_span, path_expr, fields)
210210
}
211211
}
212-
Named(ref fields) => {
212+
Named(fields) => {
213213
// use the field's span to get nicer error messages.
214214
let fields = fields
215215
.iter()

Diff for: compiler/rustc_builtin_macros/src/deriving/default.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,12 @@ fn default_struct_substructure(
6262
let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());
6363

6464
let expr = match summary {
65-
Unnamed(ref fields, is_tuple) => {
66-
if !is_tuple {
67-
cx.expr_ident(trait_span, substr.type_ident)
68-
} else {
69-
let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
70-
cx.expr_call_ident(trait_span, substr.type_ident, exprs)
71-
}
65+
Unnamed(_, false) => cx.expr_ident(trait_span, substr.type_ident),
66+
Unnamed(fields, true) => {
67+
let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
68+
cx.expr_call_ident(trait_span, substr.type_ident, exprs)
7269
}
73-
Named(ref fields) => {
70+
Named(fields) => {
7471
let default_fields = fields
7572
.iter()
7673
.map(|&(ident, span)| cx.field_imm(span, ident, default_call(span)))

Diff for: compiler/rustc_builtin_macros/src/deriving/encodable.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ fn encodable_substructure(
164164
],
165165
));
166166

167-
match *substr.fields {
168-
Struct(_, ref fields) => {
167+
match substr.fields {
168+
Struct(_, fields) => {
169169
let fn_emit_struct_field_path =
170170
cx.def_site_path(&[sym::rustc_serialize, sym::Encoder, sym::emit_struct_field]);
171171
let mut stmts = Vec::new();
@@ -224,7 +224,7 @@ fn encodable_substructure(
224224
BlockOrExpr::new_expr(expr)
225225
}
226226

227-
EnumMatching(idx, _, variant, ref fields) => {
227+
EnumMatching(idx, _, variant, fields) => {
228228
// We're not generating an AST that the borrow checker is expecting,
229229
// so we need to generate a unique local variable to take the
230230
// mutable loan out on, otherwise we get conflicts which don't
@@ -274,7 +274,7 @@ fn encodable_substructure(
274274
vec![
275275
blkencoder,
276276
name,
277-
cx.expr_usize(trait_span, idx),
277+
cx.expr_usize(trait_span, *idx),
278278
cx.expr_usize(trait_span, fields.len()),
279279
blk,
280280
],

0 commit comments

Comments
 (0)