Skip to content

Commit 34b5118

Browse files
Suggest using std::mem::drop function instead of explicit destructor call
1 parent 672b272 commit 34b5118

File tree

7 files changed

+45
-12
lines changed

7 files changed

+45
-12
lines changed

src/librustc_typeck/check/callee.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,29 @@ use rustc_target::spec::abi;
2121
/// Checks that it is legal to call methods of the trait corresponding
2222
/// to `trait_id` (this only cares about the trait, not the specific
2323
/// method that is called).
24-
pub fn check_legal_trait_for_method_call(tcx: TyCtxt<'_>, span: Span, trait_id: DefId) {
24+
pub fn check_legal_trait_for_method_call(
25+
tcx: TyCtxt<'_>,
26+
span: Span,
27+
receiver: Option<Span>,
28+
trait_id: DefId,
29+
) {
2530
if tcx.lang_items().drop_trait() == Some(trait_id) {
26-
struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method")
27-
.span_label(span, "explicit destructor calls not allowed")
28-
.emit();
31+
let mut err = struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
32+
err.span_label(span, "explicit destructor calls not allowed");
33+
34+
let snippet = receiver
35+
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
36+
.unwrap_or_default();
37+
38+
let (suggestion, applicability) = if snippet.is_empty() {
39+
(snippet, Applicability::Unspecified)
40+
} else {
41+
(format!("drop({})", snippet), Applicability::MachineApplicable)
42+
};
43+
44+
err.span_suggestion(span, "consider using `drop` function", suggestion, applicability);
45+
46+
err.emit();
2947
}
3048
}
3149

src/librustc_typeck/check/method/confirm.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,12 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
597597
fn enforce_illegal_method_limitations(&self, pick: &probe::Pick<'_>) {
598598
// Disallow calls to the method `drop` defined in the `Drop` trait.
599599
match pick.item.container {
600-
ty::TraitContainer(trait_def_id) => {
601-
callee::check_legal_trait_for_method_call(self.tcx, self.span, trait_def_id)
602-
}
600+
ty::TraitContainer(trait_def_id) => callee::check_legal_trait_for_method_call(
601+
self.tcx,
602+
self.span,
603+
Some(self.self_expr.span),
604+
trait_def_id,
605+
),
603606
ty::ImplContainer(..) => {}
604607
}
605608
}

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5438,7 +5438,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
54385438
debug!("instantiate_value_path: def_id={:?} container={:?}", def_id, container);
54395439
match container {
54405440
ty::TraitContainer(trait_did) => {
5441-
callee::check_legal_trait_for_method_call(tcx, span, trait_did)
5441+
callee::check_legal_trait_for_method_call(tcx, span, None, trait_did)
54425442
}
54435443
ty::ImplContainer(impl_def_id) => {
54445444
if segments.len() == 1 {

src/test/ui/error-codes/E0040.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
22
--> $DIR/E0040.rs:13:7
33
|
44
LL | x.drop();
5-
| ^^^^ explicit destructor calls not allowed
5+
| ^^^^
6+
| |
7+
| explicit destructor calls not allowed
8+
| help: consider using `drop` function: `drop(x)`
69

710
error: aborting due to previous error
811

src/test/ui/explicit/explicit-call-to-dtor.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
22
--> $DIR/explicit-call-to-dtor.rs:13:7
33
|
44
LL | x.drop();
5-
| ^^^^ explicit destructor calls not allowed
5+
| ^^^^
6+
| |
7+
| explicit destructor calls not allowed
8+
| help: consider using `drop` function: `drop(x)`
69

710
error: aborting due to previous error
811

src/test/ui/explicit/explicit-call-to-supertrait-dtor.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
22
--> $DIR/explicit-call-to-supertrait-dtor.rs:17:14
33
|
44
LL | self.drop();
5-
| ^^^^ explicit destructor calls not allowed
5+
| ^^^^
6+
| |
7+
| explicit destructor calls not allowed
8+
| help: consider using `drop` function: `drop(self)`
69

710
error: aborting due to previous error
811

src/test/ui/illegal-ufcs-drop.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0040]: explicit use of destructor method
22
--> $DIR/illegal-ufcs-drop.rs:8:5
33
|
44
LL | Drop::drop(&mut Foo)
5-
| ^^^^^^^^^^ explicit destructor calls not allowed
5+
| ^^^^^^^^^^
6+
| |
7+
| explicit destructor calls not allowed
8+
| help: consider using `drop` function
69

710
error: aborting due to previous error
811

0 commit comments

Comments
 (0)