diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index e5fcc68dad23a..2890439c2f816 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -42,7 +42,7 @@ pub(crate) fn check_legal_trait_for_method_call( if tcx.is_lang_item(trait_id, LangItem::Drop) { let sugg = if let Some(receiver) = receiver.filter(|s| !s.is_empty()) { errors::ExplicitDestructorCallSugg::Snippet { - lo: expr_span.shrink_to_lo(), + lo: expr_span.shrink_to_lo().to(receiver.shrink_to_lo()), hi: receiver.shrink_to_hi().to(expr_span.shrink_to_hi()), } } else { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 9a54d651fe73a..1e7c6364bb36f 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -1013,10 +1013,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { debug!(?def_id, ?container, ?container_id); match container { ty::AssocContainer::Trait => { + let arg_span = if let hir::Node::Expr(call_expr) = + self.tcx.parent_hir_node(hir_id) + && let hir::ExprKind::Call(_, args) = call_expr.kind + && let Some(first_arg) = args.first() + { + let mut arg = first_arg; + while let hir::ExprKind::AddrOf(_, _, inner) = arg.kind { + arg = inner; + } + Some(arg.span) + } else { + None + }; + if let Err(e) = callee::check_legal_trait_for_method_call( tcx, path_span, - None, + arg_span, span, container_id, self.body_id.to_def_id(), diff --git a/tests/ui/drop/explicit-drop-call-error.fixed b/tests/ui/drop/explicit-drop-call-error.fixed index f2f0245df96ff..6060d64cebf11 100644 --- a/tests/ui/drop/explicit-drop-call-error.fixed +++ b/tests/ui/drop/explicit-drop-call-error.fixed @@ -11,5 +11,5 @@ impl Drop for Foo { } fn main() { - drop(&mut Foo) //~ ERROR explicit use of destructor method + drop(Foo) //~ ERROR explicit use of destructor method } diff --git a/tests/ui/drop/explicit-drop-call-error.stderr b/tests/ui/drop/explicit-drop-call-error.stderr index 95d5c31ab6d05..159bf8f841aae 100644 --- a/tests/ui/drop/explicit-drop-call-error.stderr +++ b/tests/ui/drop/explicit-drop-call-error.stderr @@ -2,10 +2,13 @@ error[E0040]: explicit use of destructor method --> $DIR/explicit-drop-call-error.rs:14:5 | LL | Drop::drop(&mut Foo) - | ^^^^^^^^^^ - | | - | explicit destructor calls not allowed - | help: consider using `drop` function: `drop` + | ^^^^^^^^^^ explicit destructor calls not allowed + | +help: consider using `drop` function + | +LL - Drop::drop(&mut Foo) +LL + drop(Foo) + | error: aborting due to 1 previous error diff --git a/tests/ui/drop/explicit-drop-call-named-ref.fixed b/tests/ui/drop/explicit-drop-call-named-ref.fixed new file mode 100644 index 0000000000000..68f452d7a0844 --- /dev/null +++ b/tests/ui/drop/explicit-drop-call-named-ref.fixed @@ -0,0 +1,17 @@ +//@ run-rustfix + +struct Foo; + +impl Drop for Foo { + fn drop(&mut self) {} +} + +fn foo(f: Foo) { + drop(f); //~ ERROR explicit use of destructor method + //~| HELP: consider using `drop` function +} + +fn main() { + let f = Foo; + foo(f); +} diff --git a/tests/ui/drop/explicit-drop-call-named-ref.rs b/tests/ui/drop/explicit-drop-call-named-ref.rs new file mode 100644 index 0000000000000..59c05ca01a9e0 --- /dev/null +++ b/tests/ui/drop/explicit-drop-call-named-ref.rs @@ -0,0 +1,17 @@ +//@ run-rustfix + +struct Foo; + +impl Drop for Foo { + fn drop(&mut self) {} +} + +fn foo(f: Foo) { + Drop::drop(&mut f); //~ ERROR explicit use of destructor method + //~| HELP: consider using `drop` function +} + +fn main() { + let f = Foo; + foo(f); +} diff --git a/tests/ui/drop/explicit-drop-call-named-ref.stderr b/tests/ui/drop/explicit-drop-call-named-ref.stderr new file mode 100644 index 0000000000000..f7ad6de895a6f --- /dev/null +++ b/tests/ui/drop/explicit-drop-call-named-ref.stderr @@ -0,0 +1,15 @@ +error[E0040]: explicit use of destructor method + --> $DIR/explicit-drop-call-named-ref.rs:10:5 + | +LL | Drop::drop(&mut f); + | ^^^^^^^^^^ explicit destructor calls not allowed + | +help: consider using `drop` function + | +LL - Drop::drop(&mut f); +LL + drop(f); + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0040`.