Skip to content

Commit 079817d

Browse files
committed
Make prefix immutable
1 parent 0612568 commit 079817d

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/librustc_typeck/check/demand.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -614,24 +614,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
614614
// For now, don't suggest casting with `as`.
615615
let can_cast = false;
616616

617-
let mut prefix = String::new();
618-
if let Some(hir::Node::Expr(hir::Expr {
619-
kind: hir::ExprKind::Struct(_, fields, _), ..
617+
let prefix = if let Some(hir::Node::Expr(hir::Expr {
618+
kind: hir::ExprKind::Struct(_, fields, _),
619+
..
620620
})) = self.tcx.hir().find(self.tcx.hir().get_parent_node(expr.hir_id))
621621
{
622622
// `expr` is a literal field for a struct, only suggest if appropriate
623-
for field in *fields {
624-
if field.expr.hir_id == expr.hir_id && field.is_shorthand {
625-
// This is a field literal
626-
prefix = format!("{}: ", field.ident);
627-
break;
628-
}
629-
}
630-
if &prefix == "" {
623+
match (*fields)
624+
.iter()
625+
.find(|field| field.expr.hir_id == expr.hir_id && field.is_shorthand)
626+
{
627+
// This is a field literal
628+
Some(field) => format!("{}: ", field.ident),
631629
// Likely a field was meant, but this field wasn't found. Do not suggest anything.
632-
return false;
630+
None => return false,
633631
}
634-
}
632+
} else {
633+
String::new()
634+
};
635635
if let hir::ExprKind::Call(path, args) = &expr.kind {
636636
if let (hir::ExprKind::Path(hir::QPath::TypeRelative(base_ty, path_segment)), 1) =
637637
(&path.kind, args.len())
@@ -723,7 +723,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
723723

724724
let suggest_to_change_suffix_or_into =
725725
|err: &mut DiagnosticBuilder<'_>, is_fallible: bool| {
726-
let into_sugg = into_suggestion.clone();
727726
err.span_suggestion(
728727
expr.span,
729728
if literal_is_ty_suffixed(expr) {
@@ -738,7 +737,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
738737
} else if is_fallible {
739738
try_into_suggestion
740739
} else {
741-
into_sugg
740+
into_suggestion.clone()
742741
},
743742
Applicability::MachineApplicable,
744743
);

0 commit comments

Comments
 (0)