Skip to content

Commit 260a840

Browse files
committed
Refactor wrap suggestion code (just a bit)
1 parent 506c98f commit 260a840

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

compiler/rustc_typeck/src/check/demand.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -400,37 +400,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
400400
})
401401
.collect();
402402

403-
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
404-
Some(ident) => format!("{ident}: "),
405-
None => String::new(),
406-
};
403+
let suggestions_for = |variant: &_, ctor, field_name| {
404+
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
405+
Some(ident) => format!("{ident}: "),
406+
None => String::new(),
407+
};
407408

408-
fn brackets_for(
409-
ctor: hir::def::CtorKind,
410-
field_name: Symbol,
411-
) -> (String, &'static str) {
412-
match ctor {
409+
let (open, close) = match ctor {
413410
hir::def::CtorKind::Fn => ("(".to_owned(), ")"),
414411
hir::def::CtorKind::Fictive => (format!(" {{ {field_name}: "), " }"),
412+
413+
// unit variants don't have fields
415414
hir::def::CtorKind::Const => unreachable!(),
416-
}
417-
}
415+
};
416+
417+
vec![
418+
(expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")),
419+
(expr.span.shrink_to_hi(), close.to_owned()),
420+
]
421+
};
418422

419423
match &compatible_variants[..] {
420424
[] => { /* No variants to format */ }
421425
[(variant, ctor_kind, field_name, note)] => {
422-
let (open, close) = brackets_for(*ctor_kind, *field_name);
423-
424426
// Just a single matching variant.
425427
err.multipart_suggestion_verbose(
426428
&format!(
427429
"try wrapping the expression in `{variant}`{note}",
428430
note = note.as_deref().unwrap_or("")
429431
),
430-
vec![
431-
(expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")),
432-
(expr.span.shrink_to_hi(), close.to_owned()),
433-
],
432+
suggestions_for(&**variant, *ctor_kind, *field_name),
434433
Applicability::MaybeIncorrect,
435434
);
436435
}
@@ -443,12 +442,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
443442
),
444443
compatible_variants.into_iter().map(
445444
|(variant, ctor_kind, field_name, _)| {
446-
let (open, close) = brackets_for(ctor_kind, field_name);
447-
448-
vec![
449-
(expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")),
450-
(expr.span.shrink_to_hi(), close.to_owned()),
451-
]
445+
suggestions_for(&variant, ctor_kind, field_name)
452446
},
453447
),
454448
Applicability::MaybeIncorrect,

0 commit comments

Comments
 (0)