Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/drop/explicit-drop-call-error.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 7 additions & 4 deletions tests/ui/drop/explicit-drop-call-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions tests/ui/drop/explicit-drop-call-named-ref.fixed
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 17 additions & 0 deletions tests/ui/drop/explicit-drop-call-named-ref.rs
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 15 additions & 0 deletions tests/ui/drop/explicit-drop-call-named-ref.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading