Skip to content

Commit 32ea6a1

Browse files
authored
Rollup merge of #71525 - ldm0:intosug, r=Mark-Simulacrum
`prefix` should not be mutable. Change the process from for loop to find, which makes the `prefix` able to be immutable.
2 parents 23ffeea + 079817d commit 32ea6a1

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
@@ -708,24 +708,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
708708
// For now, don't suggest casting with `as`.
709709
let can_cast = false;
710710

711-
let mut prefix = String::new();
712-
if let Some(hir::Node::Expr(hir::Expr {
713-
kind: hir::ExprKind::Struct(_, fields, _), ..
711+
let prefix = if let Some(hir::Node::Expr(hir::Expr {
712+
kind: hir::ExprKind::Struct(_, fields, _),
713+
..
714714
})) = self.tcx.hir().find(self.tcx.hir().get_parent_node(expr.hir_id))
715715
{
716716
// `expr` is a literal field for a struct, only suggest if appropriate
717-
for field in *fields {
718-
if field.expr.hir_id == expr.hir_id && field.is_shorthand {
719-
// This is a field literal
720-
prefix = format!("{}: ", field.ident);
721-
break;
722-
}
723-
}
724-
if &prefix == "" {
717+
match (*fields)
718+
.iter()
719+
.find(|field| field.expr.hir_id == expr.hir_id && field.is_shorthand)
720+
{
721+
// This is a field literal
722+
Some(field) => format!("{}: ", field.ident),
725723
// Likely a field was meant, but this field wasn't found. Do not suggest anything.
726-
return false;
724+
None => return false,
727725
}
728-
}
726+
} else {
727+
String::new()
728+
};
729729
if let hir::ExprKind::Call(path, args) = &expr.kind {
730730
if let (hir::ExprKind::Path(hir::QPath::TypeRelative(base_ty, path_segment)), 1) =
731731
(&path.kind, args.len())
@@ -817,7 +817,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
817817

818818
let suggest_to_change_suffix_or_into =
819819
|err: &mut DiagnosticBuilder<'_>, is_fallible: bool| {
820-
let into_sugg = into_suggestion.clone();
821820
err.span_suggestion(
822821
expr.span,
823822
if literal_is_ty_suffixed(expr) {
@@ -832,7 +831,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
832831
} else if is_fallible {
833832
try_into_suggestion
834833
} else {
835-
into_sugg
834+
into_suggestion.clone()
836835
},
837836
Applicability::MachineApplicable,
838837
);

0 commit comments

Comments
 (0)