Improve ManuallyDrop suggestion
This commit is contained in:
parent
f820d9d4e5
commit
f980f813e1
6 changed files with 42 additions and 43 deletions
|
|
@ -371,8 +371,13 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
|
|||
let param_env = tcx.param_env(item_def_id);
|
||||
for field in fields {
|
||||
let field_ty = field.ty(tcx, substs);
|
||||
// We are currently checking the type this field came from, so it must be local.
|
||||
let field_span = tcx.hir().span_if_local(field.did).unwrap();
|
||||
let (field_span, ty_span) =
|
||||
// We are currently checking the type this field came from, so it must be local.
|
||||
if let Node::Field(field) = tcx.hir().get_if_local(field.did).unwrap() {
|
||||
(field.span, field.ty.span)
|
||||
} else {
|
||||
unreachable!("mir field has to correspond to hir field");
|
||||
};
|
||||
if field_ty.needs_drop(tcx, param_env) {
|
||||
struct_span_err!(
|
||||
tcx.sess,
|
||||
|
|
@ -380,7 +385,11 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
|
|||
E0740,
|
||||
"unions may not contain fields that need dropping"
|
||||
)
|
||||
.span_note(field_span, "`std::mem::ManuallyDrop` can be used to wrap the type")
|
||||
.multipart_suggestion_verbose(
|
||||
"wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped",
|
||||
vec![(ty_span, format!("std::mem::ManuallyDrop<{}>", field_ty))],
|
||||
Applicability::MaybeIncorrect,
|
||||
)
|
||||
.emit();
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue