mirror of https://github.com/rust-lang/rust.git
Refactor wrap suggestion code (just a bit)
This commit is contained in:
parent
506c98f291
commit
260a840543
|
@ -400,37 +400,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
|
let suggestions_for = |variant: &_, ctor, field_name| {
|
||||||
Some(ident) => format!("{ident}: "),
|
let prefix = match self.maybe_get_struct_pattern_shorthand_field(expr) {
|
||||||
None => String::new(),
|
Some(ident) => format!("{ident}: "),
|
||||||
};
|
None => String::new(),
|
||||||
|
};
|
||||||
|
|
||||||
fn brackets_for(
|
let (open, close) = match ctor {
|
||||||
ctor: hir::def::CtorKind,
|
|
||||||
field_name: Symbol,
|
|
||||||
) -> (String, &'static str) {
|
|
||||||
match ctor {
|
|
||||||
hir::def::CtorKind::Fn => ("(".to_owned(), ")"),
|
hir::def::CtorKind::Fn => ("(".to_owned(), ")"),
|
||||||
hir::def::CtorKind::Fictive => (format!(" {{ {field_name}: "), " }"),
|
hir::def::CtorKind::Fictive => (format!(" {{ {field_name}: "), " }"),
|
||||||
|
|
||||||
|
// unit variants don't have fields
|
||||||
hir::def::CtorKind::Const => unreachable!(),
|
hir::def::CtorKind::Const => unreachable!(),
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
vec![
|
||||||
|
(expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")),
|
||||||
|
(expr.span.shrink_to_hi(), close.to_owned()),
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
match &compatible_variants[..] {
|
match &compatible_variants[..] {
|
||||||
[] => { /* No variants to format */ }
|
[] => { /* No variants to format */ }
|
||||||
[(variant, ctor_kind, field_name, note)] => {
|
[(variant, ctor_kind, field_name, note)] => {
|
||||||
let (open, close) = brackets_for(*ctor_kind, *field_name);
|
|
||||||
|
|
||||||
// Just a single matching variant.
|
// Just a single matching variant.
|
||||||
err.multipart_suggestion_verbose(
|
err.multipart_suggestion_verbose(
|
||||||
&format!(
|
&format!(
|
||||||
"try wrapping the expression in `{variant}`{note}",
|
"try wrapping the expression in `{variant}`{note}",
|
||||||
note = note.as_deref().unwrap_or("")
|
note = note.as_deref().unwrap_or("")
|
||||||
),
|
),
|
||||||
vec![
|
suggestions_for(&**variant, *ctor_kind, *field_name),
|
||||||
(expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")),
|
|
||||||
(expr.span.shrink_to_hi(), close.to_owned()),
|
|
||||||
],
|
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -443,12 +442,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
),
|
),
|
||||||
compatible_variants.into_iter().map(
|
compatible_variants.into_iter().map(
|
||||||
|(variant, ctor_kind, field_name, _)| {
|
|(variant, ctor_kind, field_name, _)| {
|
||||||
let (open, close) = brackets_for(ctor_kind, field_name);
|
suggestions_for(&variant, ctor_kind, field_name)
|
||||||
|
|
||||||
vec![
|
|
||||||
(expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")),
|
|
||||||
(expr.span.shrink_to_hi(), close.to_owned()),
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
|
|
Loading…
Reference in New Issue