Skip to content

Commit 07857f7

Browse files
committed
review comments
1 parent a51ca02 commit 07857f7

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

src/librustc_typeck/check/demand.rs

+44-39
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,26 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
249249
None
250250
}
251251

252+
fn is_hir_id_from_struct_pattern_shorthand_field(&self, hir_id: hir::HirId) -> bool {
253+
let parent_id = self.tcx.hir().get_parent_node_by_hir_id(expr.hir_id);
254+
let mut is_struct_pat_shorthand_field = false;
255+
if let Some(parent) = self.tcx.hir().find_by_hir_id(parent_id) {
256+
// Account for fields
257+
if let Node::Expr(hir::Expr {
258+
node: hir::ExprKind::Struct(_, fields, ..), ..
259+
}) = parent {
260+
if let Ok(src) = cm.span_to_snippet(sp) {
261+
for field in fields {
262+
if field.ident.as_str() == src.as_str() && field.is_shorthand {
263+
is_struct_pat_shorthand_field = true;
264+
break;
265+
}
266+
}
267+
}
268+
}
269+
};
270+
}
271+
252272
/// This function is used to determine potential "simple" improvements or users' errors and
253273
/// provide them useful help. For example:
254274
///
@@ -277,23 +297,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
277297
return None;
278298
}
279299

280-
let parent_id = self.tcx.hir().get_parent_node_by_hir_id(expr.hir_id);
281-
let mut is_struct_pat_shorthand_field = false;
282-
if let Some(parent) = self.tcx.hir().find_by_hir_id(parent_id) {
283-
// Account for fields
284-
if let Node::Expr(hir::Expr {
285-
node: hir::ExprKind::Struct(_, fields, ..), ..
286-
}) = parent {
287-
if let Ok(src) = cm.span_to_snippet(sp) {
288-
for field in fields {
289-
if field.ident.as_str() == src.as_str() && field.is_shorthand {
290-
is_struct_pat_shorthand_field = true;
291-
break;
292-
}
293-
}
294-
}
295-
}
296-
};
300+
let mut is_struct_pat_shorthand_field =
301+
self.is_hir_id_from_struct_pattern_shorthand_field(expr.hir_id);
297302

298303
match (&expected.sty, &checked_ty.sty) {
299304
(&ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.sty, &check.sty) {
@@ -333,12 +338,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
333338
// bar(&x); // error, expected &mut
334339
// ```
335340
let ref_ty = match mutability {
336-
hir::Mutability::MutMutable => self.tcx.mk_mut_ref(
337-
self.tcx.mk_region(ty::ReStatic),
338-
checked_ty),
339-
hir::Mutability::MutImmutable => self.tcx.mk_imm_ref(
340-
self.tcx.mk_region(ty::ReStatic),
341-
checked_ty),
341+
hir::Mutability::MutMutable => {
342+
self.tcx.mk_mut_ref(self.tcx.mk_region(ty::ReStatic), checked_ty)
343+
}
344+
hir::Mutability::MutImmutable => {
345+
self.tcx.mk_imm_ref(self.tcx.mk_region(ty::ReStatic), checked_ty)
346+
}
342347
};
343348
if self.can_coerce(ref_ty, expected) {
344349
if let Ok(src) = cm.span_to_snippet(sp) {
@@ -359,22 +364,22 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
359364
if let Some(sugg) = self.can_use_as_ref(expr) {
360365
return Some(sugg);
361366
}
362-
return Some(match (mutability, is_struct_pat_shorthand_field) {
363-
(hir::Mutability::MutMutable, false) => {
364-
(sp, "consider mutably borrowing here",
365-
format!("&mut {}", sugg_expr))
366-
}
367-
(hir::Mutability::MutImmutable, false) => {
368-
(sp, "consider borrowing here", format!("&{}", sugg_expr))
369-
}
370-
(hir::Mutability::MutMutable, true) => {
371-
(sp, "consider mutably borrowing here",
372-
format!("{}: &mut {}", sugg_expr, sugg_expr))
373-
}
374-
(hir::Mutability::MutImmutable, true) => {
375-
(sp, "consider borrowing here",
376-
format!("{}: &{}", sugg_expr, sugg_expr))
377-
}
367+
let field_name = if is_struct_pat_shorthand_field {
368+
format!("{}: ", sugg_expr)
369+
} else {
370+
String::new()
371+
};
372+
return Some(match mutability {
373+
hir::Mutability::MutMutable => (
374+
sp,
375+
"consider mutably borrowing here",
376+
format!("{}&mut {}", field_name, sugg_expr),
377+
),
378+
hir::Mutability::MutImmutable => (
379+
sp,
380+
"consider borrowing here",
381+
format!("{}&{}", field_name, sugg_expr),
382+
),
378383
});
379384
}
380385
}

0 commit comments

Comments
 (0)