Fix E0040 suggestion for explicit Drop::drop UFCS calls

`Drop::drop(&mut f)` now correctly suggests `drop(f)` instead of
the bare `drop` with no argument.

Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
This commit is contained in:
Usman Akinyemi
2026-05-03 14:45:07 +05:30
parent 0469a92a76
commit e5b42e5084
7 changed files with 73 additions and 7 deletions

View File

@@ -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 {

View File

@@ -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(),

View File

@@ -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
}

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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`.