Skip to content

Commit 84cfe04

Browse files
committed
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>
1 parent 0469a92 commit 84cfe04

8 files changed

Lines changed: 74 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
@@ -1013,10 +1013,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10131013
debug!(?def_id, ?container, ?container_id);
10141014
match container {
10151015
ty::AssocContainer::Trait => {
1016+
let arg_span = if let hir::Node::Expr(call_expr) =
1017+
self.tcx.parent_hir_node(hir_id)
1018+
&& let hir::ExprKind::Call(_, args) = call_expr.kind
1019+
&& let Some(first_arg) = args.first()
1020+
{
1021+
let mut arg = first_arg;
1022+
while let hir::ExprKind::AddrOf(_, _, inner) = arg.kind {
1023+
arg = inner;
1024+
}
1025+
Some(arg.span)
1026+
} else {
1027+
None
1028+
};
1029+
10161030
if let Err(e) = callee::check_legal_trait_for_method_call(
10171031
tcx,
10181032
path_span,
1019-
None,
1033+
arg_span,
10201034
span,
10211035
container_id,
10221036
self.body_id.to_def_id(),

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ use dangling::*;
8888
use default_could_be_derived::DefaultCouldBeDerived;
8989
use deref_into_dyn_supertrait::*;
9090
use disallowed_pass_by_ref::*;
91+
pub use drop_forget_useless::DROPPING_REFERENCES;
9192
use drop_forget_useless::*;
9293
use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums;
9394
use for_loops_over_fallibles::*;

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)