mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-02 17:44:28 +00:00
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:
@@ -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 {
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
17
tests/ui/drop/explicit-drop-call-named-ref.fixed
Normal file
17
tests/ui/drop/explicit-drop-call-named-ref.fixed
Normal 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);
|
||||
}
|
||||
17
tests/ui/drop/explicit-drop-call-named-ref.rs
Normal file
17
tests/ui/drop/explicit-drop-call-named-ref.rs
Normal 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);
|
||||
}
|
||||
15
tests/ui/drop/explicit-drop-call-named-ref.stderr
Normal file
15
tests/ui/drop/explicit-drop-call-named-ref.stderr
Normal 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`.
|
||||
Reference in New Issue
Block a user