Skip to content

Commit 25ec1bc

Browse files
Rollup merge of rust-lang#156103 - Unique-Usman:ua/fixe0040, r=mejrs
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. Fix: rust-lang#156017
2 parents 1b0d1ab + e5b42e5 commit 25ec1bc

7 files changed

Lines changed: 73 additions & 7 deletions

File tree

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) fn check_legal_trait_for_method_call(
4242
if tcx.is_lang_item(trait_id, LangItem::Drop) {
4343
let sugg = if let Some(receiver) = receiver.filter(|s| !s.is_empty()) {
4444
errors::ExplicitDestructorCallSugg::Snippet {
45-
lo: expr_span.shrink_to_lo(),
45+
lo: expr_span.shrink_to_lo().to(receiver.shrink_to_lo()),
4646
hi: receiver.shrink_to_hi().to(expr_span.shrink_to_hi()),
4747
}
4848
} else {

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,10 +1007,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10071007
debug!(?def_id, ?container, ?container_id);
10081008
match container {
10091009
ty::AssocContainer::Trait => {
1010+
let arg_span = if let hir::Node::Expr(call_expr) =
1011+
self.tcx.parent_hir_node(hir_id)
1012+
&& let hir::ExprKind::Call(_, args) = call_expr.kind
1013+
&& let Some(first_arg) = args.first()
1014+
{
1015+
let mut arg = first_arg;
1016+
while let hir::ExprKind::AddrOf(_, _, inner) = arg.kind {
1017+
arg = inner;
1018+
}
1019+
Some(arg.span)
1020+
} else {
1021+
None
1022+
};
1023+
10101024
if let Err(e) = callee::check_legal_trait_for_method_call(
10111025
tcx,
10121026
path_span,
1013-
None,
1027+
arg_span,
10141028
span,
10151029
container_id,
10161030
self.body_id.to_def_id(),

tests/ui/drop/explicit-drop-call-error.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ impl Drop for Foo {
1111
}
1212

1313
fn main() {
14-
drop(&mut Foo) //~ ERROR explicit use of destructor method
14+
drop(Foo) //~ ERROR explicit use of destructor method
1515
}

tests/ui/drop/explicit-drop-call-error.stderr

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ error[E0040]: explicit use of destructor method
22
--> $DIR/explicit-drop-call-error.rs:14:5
33
|
44
LL | Drop::drop(&mut Foo)
5-
| ^^^^^^^^^^
6-
| |
7-
| explicit destructor calls not allowed
8-
| help: consider using `drop` function: `drop`
5+
| ^^^^^^^^^^ explicit destructor calls not allowed
6+
|
7+
help: consider using `drop` function
8+
|
9+
LL - Drop::drop(&mut Foo)
10+
LL + drop(Foo)
11+
|
912

1013
error: aborting due to 1 previous error
1114

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ run-rustfix
2+
3+
struct Foo;
4+
5+
impl Drop for Foo {
6+
fn drop(&mut self) {}
7+
}
8+
9+
fn foo(f: Foo) {
10+
drop(f); //~ ERROR explicit use of destructor method
11+
//~| HELP: consider using `drop` function
12+
}
13+
14+
fn main() {
15+
let f = Foo;
16+
foo(f);
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ run-rustfix
2+
3+
struct Foo;
4+
5+
impl Drop for Foo {
6+
fn drop(&mut self) {}
7+
}
8+
9+
fn foo(f: Foo) {
10+
Drop::drop(&mut f); //~ ERROR explicit use of destructor method
11+
//~| HELP: consider using `drop` function
12+
}
13+
14+
fn main() {
15+
let f = Foo;
16+
foo(f);
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0040]: explicit use of destructor method
2+
--> $DIR/explicit-drop-call-named-ref.rs:10:5
3+
|
4+
LL | Drop::drop(&mut f);
5+
| ^^^^^^^^^^ explicit destructor calls not allowed
6+
|
7+
help: consider using `drop` function
8+
|
9+
LL - Drop::drop(&mut f);
10+
LL + drop(f);
11+
|
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0040`.

0 commit comments

Comments
 (0)