Migrate borrow of moved value diagnostic
This commit is contained in:
parent
eeef05b318
commit
3a9a06311d
3 changed files with 29 additions and 17 deletions
|
@ -562,3 +562,19 @@ pub struct IrrefutableLetPatternsLetElse {
|
|||
pub struct IrrefutableLetPatternsWhileLet {
|
||||
pub count: usize,
|
||||
}
|
||||
|
||||
#[derive(SessionDiagnostic)]
|
||||
#[diag(mir_build::borrow_of_moved_value)]
|
||||
pub struct BorrowOfMovedValue<'tcx> {
|
||||
#[primary_span]
|
||||
pub span: Span,
|
||||
#[label]
|
||||
#[label(mir_build::occurs_because_label)]
|
||||
pub binding_span: Span,
|
||||
#[label(mir_build::value_borrowed_label)]
|
||||
pub conflicts_ref: Vec<Span>,
|
||||
pub name: Ident,
|
||||
pub ty: Ty<'tcx>,
|
||||
#[suggestion(code = "ref ", applicability = "machine-applicable")]
|
||||
pub suggest_borrowing: Option<Span>,
|
||||
}
|
||||
|
|
|
@ -962,24 +962,14 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa
|
|||
}
|
||||
});
|
||||
if !conflicts_ref.is_empty() {
|
||||
let occurs_because = format!(
|
||||
"move occurs because `{}` has type `{}` which does not implement the `Copy` trait",
|
||||
sess.emit_err(BorrowOfMovedValue {
|
||||
span: pat.span,
|
||||
binding_span,
|
||||
conflicts_ref,
|
||||
name,
|
||||
typeck_results.node_type(pat.hir_id),
|
||||
);
|
||||
let mut err = sess.struct_span_err(pat.span, "borrow of moved value");
|
||||
err.span_label(binding_span, format!("value moved into `{}` here", name))
|
||||
.span_label(binding_span, occurs_because)
|
||||
.span_labels(conflicts_ref, "value borrowed here after move");
|
||||
if pat.span.contains(binding_span) {
|
||||
err.span_suggestion_verbose(
|
||||
binding_span.shrink_to_lo(),
|
||||
"borrow this binding in the pattern to avoid moving the value",
|
||||
"ref ".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
err.emit();
|
||||
ty: typeck_results.node_type(pat.hir_id),
|
||||
suggest_borrowing: pat.span.contains(binding_span).then(|| binding_span.shrink_to_lo()),
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue