Suggest using std::mem::drop function instead of explicit destructor call
This commit is contained in:
parent
672b272077
commit
34b51187ce
7 changed files with 45 additions and 12 deletions
|
|
@ -21,11 +21,29 @@ use rustc_target::spec::abi;
|
|||
/// Checks that it is legal to call methods of the trait corresponding
|
||||
/// to `trait_id` (this only cares about the trait, not the specific
|
||||
/// method that is called).
|
||||
pub fn check_legal_trait_for_method_call(tcx: TyCtxt<'_>, span: Span, trait_id: DefId) {
|
||||
pub fn check_legal_trait_for_method_call(
|
||||
tcx: TyCtxt<'_>,
|
||||
span: Span,
|
||||
receiver: Option<Span>,
|
||||
trait_id: DefId,
|
||||
) {
|
||||
if tcx.lang_items().drop_trait() == Some(trait_id) {
|
||||
struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method")
|
||||
.span_label(span, "explicit destructor calls not allowed")
|
||||
.emit();
|
||||
let mut err = struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
|
||||
err.span_label(span, "explicit destructor calls not allowed");
|
||||
|
||||
let snippet = receiver
|
||||
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
|
||||
.unwrap_or_default();
|
||||
|
||||
let (suggestion, applicability) = if snippet.is_empty() {
|
||||
(snippet, Applicability::Unspecified)
|
||||
} else {
|
||||
(format!("drop({})", snippet), Applicability::MachineApplicable)
|
||||
};
|
||||
|
||||
err.span_suggestion(span, "consider using `drop` function", suggestion, applicability);
|
||||
|
||||
err.emit();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -597,9 +597,12 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
|
|||
fn enforce_illegal_method_limitations(&self, pick: &probe::Pick<'_>) {
|
||||
// Disallow calls to the method `drop` defined in the `Drop` trait.
|
||||
match pick.item.container {
|
||||
ty::TraitContainer(trait_def_id) => {
|
||||
callee::check_legal_trait_for_method_call(self.tcx, self.span, trait_def_id)
|
||||
}
|
||||
ty::TraitContainer(trait_def_id) => callee::check_legal_trait_for_method_call(
|
||||
self.tcx,
|
||||
self.span,
|
||||
Some(self.self_expr.span),
|
||||
trait_def_id,
|
||||
),
|
||||
ty::ImplContainer(..) => {}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5438,7 +5438,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
debug!("instantiate_value_path: def_id={:?} container={:?}", def_id, container);
|
||||
match container {
|
||||
ty::TraitContainer(trait_did) => {
|
||||
callee::check_legal_trait_for_method_call(tcx, span, trait_did)
|
||||
callee::check_legal_trait_for_method_call(tcx, span, None, trait_did)
|
||||
}
|
||||
ty::ImplContainer(impl_def_id) => {
|
||||
if segments.len() == 1 {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
|
|||
--> $DIR/E0040.rs:13:7
|
||||
|
|
||||
LL | x.drop();
|
||||
| ^^^^ explicit destructor calls not allowed
|
||||
| ^^^^
|
||||
| |
|
||||
| explicit destructor calls not allowed
|
||||
| help: consider using `drop` function: `drop(x)`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
|
|||
--> $DIR/explicit-call-to-dtor.rs:13:7
|
||||
|
|
||||
LL | x.drop();
|
||||
| ^^^^ explicit destructor calls not allowed
|
||||
| ^^^^
|
||||
| |
|
||||
| explicit destructor calls not allowed
|
||||
| help: consider using `drop` function: `drop(x)`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
|
|||
--> $DIR/explicit-call-to-supertrait-dtor.rs:17:14
|
||||
|
|
||||
LL | self.drop();
|
||||
| ^^^^ explicit destructor calls not allowed
|
||||
| ^^^^
|
||||
| |
|
||||
| explicit destructor calls not allowed
|
||||
| help: consider using `drop` function: `drop(self)`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
|
|||
--> $DIR/illegal-ufcs-drop.rs:8:5
|
||||
|
|
||||
LL | Drop::drop(&mut Foo)
|
||||
| ^^^^^^^^^^ explicit destructor calls not allowed
|
||||
| ^^^^^^^^^^
|
||||
| |
|
||||
| explicit destructor calls not allowed
|
||||
| help: consider using `drop` function
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue