mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-06 20:28:33 +00:00
Fix structured suggestion for explicit drop
call
This commit is contained in:
parent
2e46cb38f1
commit
70a43e07f6
@ -25,24 +25,24 @@ pub fn check_legal_trait_for_method_call(
|
|||||||
tcx: TyCtxt<'_>,
|
tcx: TyCtxt<'_>,
|
||||||
span: Span,
|
span: Span,
|
||||||
receiver: Option<Span>,
|
receiver: Option<Span>,
|
||||||
|
expr_span: Span,
|
||||||
trait_id: DefId,
|
trait_id: DefId,
|
||||||
) {
|
) {
|
||||||
if tcx.lang_items().drop_trait() == Some(trait_id) {
|
if tcx.lang_items().drop_trait() == Some(trait_id) {
|
||||||
let mut err = struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
|
let mut err = struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
|
||||||
err.span_label(span, "explicit destructor calls not allowed");
|
err.span_label(span, "explicit destructor calls not allowed");
|
||||||
|
|
||||||
let snippet = receiver
|
let (sp, suggestion) = receiver
|
||||||
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
|
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
|
||||||
.unwrap_or_default();
|
.filter(|snippet| !snippet.is_empty())
|
||||||
|
.map(|snippet| (expr_span, format!("drop({})", snippet)))
|
||||||
let suggestion =
|
.unwrap_or_else(|| (span, "drop".to_string()));
|
||||||
if snippet.is_empty() { "drop".to_string() } else { format!("drop({})", snippet) };
|
|
||||||
|
|
||||||
err.span_suggestion(
|
err.span_suggestion(
|
||||||
span,
|
sp,
|
||||||
&format!("consider using `drop` function: `{}`", suggestion),
|
"consider using `drop` function",
|
||||||
String::new(),
|
suggestion,
|
||||||
Applicability::Unspecified,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
|
|
||||||
err.emit();
|
err.emit();
|
||||||
|
@ -1163,7 +1163,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
debug!("instantiate_value_path: def_id={:?} container={:?}", def_id, container);
|
debug!("instantiate_value_path: def_id={:?} container={:?}", def_id, container);
|
||||||
match container {
|
match container {
|
||||||
ty::TraitContainer(trait_did) => {
|
ty::TraitContainer(trait_did) => {
|
||||||
callee::check_legal_trait_for_method_call(tcx, span, None, trait_did)
|
callee::check_legal_trait_for_method_call(tcx, span, None, span, trait_did)
|
||||||
}
|
}
|
||||||
ty::ImplContainer(impl_def_id) => {
|
ty::ImplContainer(impl_def_id) => {
|
||||||
if segments.len() == 1 {
|
if segments.len() == 1 {
|
||||||
|
@ -507,6 +507,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
|
|||||||
self.tcx,
|
self.tcx,
|
||||||
self.span,
|
self.span,
|
||||||
Some(self.self_expr.span),
|
Some(self.self_expr.span),
|
||||||
|
self.call_expr.span,
|
||||||
trait_def_id,
|
trait_def_id,
|
||||||
),
|
),
|
||||||
ty::ImplContainer(..) => {}
|
ty::ImplContainer(..) => {}
|
||||||
|
18
src/test/ui/error-codes/E0040.fixed
Normal file
18
src/test/ui/error-codes/E0040.fixed
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
// run-rustfix
|
||||||
|
struct Foo {
|
||||||
|
x: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Foo {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
println!("kaboom");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut x = Foo { x: -7 };
|
||||||
|
x.x = 0;
|
||||||
|
println!("{}", x.x);
|
||||||
|
drop(x);
|
||||||
|
//~^ ERROR E0040
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
// run-rustfix
|
||||||
struct Foo {
|
struct Foo {
|
||||||
x: i32,
|
x: i32,
|
||||||
}
|
}
|
||||||
@ -10,6 +11,8 @@ impl Drop for Foo {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut x = Foo { x: -7 };
|
let mut x = Foo { x: -7 };
|
||||||
|
x.x = 0;
|
||||||
|
println!("{}", x.x);
|
||||||
x.drop();
|
x.drop();
|
||||||
//~^ ERROR E0040
|
//~^ ERROR E0040
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
error[E0040]: explicit use of destructor method
|
error[E0040]: explicit use of destructor method
|
||||||
--> $DIR/E0040.rs:13:7
|
--> $DIR/E0040.rs:16:7
|
||||||
|
|
|
|
||||||
LL | x.drop();
|
LL | x.drop();
|
||||||
| ^^^^
|
| --^^^^--
|
||||||
| |
|
| | |
|
||||||
| explicit destructor calls not allowed
|
| | explicit destructor calls not allowed
|
||||||
| help: consider using `drop` function: `drop(x)`
|
| help: consider using `drop` function: `drop(x)`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
16
src/test/ui/explicit/explicit-call-to-dtor.fixed
Normal file
16
src/test/ui/explicit/explicit-call-to-dtor.fixed
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// run-rustfix
|
||||||
|
struct Foo {
|
||||||
|
x: isize
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Foo {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
println!("kaboom");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = Foo { x: 3 };
|
||||||
|
println!("{}", x.x);
|
||||||
|
drop(x); //~ ERROR explicit use of destructor method
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
// run-rustfix
|
||||||
struct Foo {
|
struct Foo {
|
||||||
x: isize
|
x: isize
|
||||||
}
|
}
|
||||||
@ -10,5 +11,6 @@ impl Drop for Foo {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = Foo { x: 3 };
|
let x = Foo { x: 3 };
|
||||||
|
println!("{}", x.x);
|
||||||
x.drop(); //~ ERROR explicit use of destructor method
|
x.drop(); //~ ERROR explicit use of destructor method
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
error[E0040]: explicit use of destructor method
|
error[E0040]: explicit use of destructor method
|
||||||
--> $DIR/explicit-call-to-dtor.rs:13:7
|
--> $DIR/explicit-call-to-dtor.rs:15:7
|
||||||
|
|
|
|
||||||
LL | x.drop();
|
LL | x.drop();
|
||||||
| ^^^^
|
| --^^^^--
|
||||||
| |
|
| | |
|
||||||
| explicit destructor calls not allowed
|
| | explicit destructor calls not allowed
|
||||||
| help: consider using `drop` function: `drop(x)`
|
| help: consider using `drop` function: `drop(x)`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
26
src/test/ui/explicit/explicit-call-to-supertrait-dtor.fixed
Normal file
26
src/test/ui/explicit/explicit-call-to-supertrait-dtor.fixed
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// run-rustfix
|
||||||
|
struct Foo {
|
||||||
|
x: isize
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(drop_bounds)]
|
||||||
|
trait Bar: Drop {
|
||||||
|
fn blah(&self);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Foo {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
println!("kaboom");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bar for Foo {
|
||||||
|
fn blah(&self) {
|
||||||
|
drop(self); //~ ERROR explicit use of destructor method
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = Foo { x: 3 };
|
||||||
|
println!("{}", x.x);
|
||||||
|
}
|
@ -1,7 +1,9 @@
|
|||||||
|
// run-rustfix
|
||||||
struct Foo {
|
struct Foo {
|
||||||
x: isize
|
x: isize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(drop_bounds)]
|
||||||
trait Bar: Drop {
|
trait Bar: Drop {
|
||||||
fn blah(&self);
|
fn blah(&self);
|
||||||
}
|
}
|
||||||
@ -20,4 +22,5 @@ impl Bar for Foo {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = Foo { x: 3 };
|
let x = Foo { x: 3 };
|
||||||
|
println!("{}", x.x);
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
error[E0040]: explicit use of destructor method
|
error[E0040]: explicit use of destructor method
|
||||||
--> $DIR/explicit-call-to-supertrait-dtor.rs:17:14
|
--> $DIR/explicit-call-to-supertrait-dtor.rs:19:14
|
||||||
|
|
|
|
||||||
LL | self.drop();
|
LL | self.drop();
|
||||||
| ^^^^
|
| -----^^^^--
|
||||||
| |
|
| | |
|
||||||
| explicit destructor calls not allowed
|
| | explicit destructor calls not allowed
|
||||||
| help: consider using `drop` function: `drop(self)`
|
| help: consider using `drop` function: `drop(self)`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
10
src/test/ui/illegal-ufcs-drop.fixed
Normal file
10
src/test/ui/illegal-ufcs-drop.fixed
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
// run-rustfix
|
||||||
|
struct Foo;
|
||||||
|
|
||||||
|
impl Drop for Foo {
|
||||||
|
fn drop(&mut self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
drop(&mut Foo) //~ ERROR explicit use of destructor method
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
// run-rustfix
|
||||||
struct Foo;
|
struct Foo;
|
||||||
|
|
||||||
impl Drop for Foo {
|
impl Drop for Foo {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
error[E0040]: explicit use of destructor method
|
error[E0040]: explicit use of destructor method
|
||||||
--> $DIR/illegal-ufcs-drop.rs:8:5
|
--> $DIR/illegal-ufcs-drop.rs:9:5
|
||||||
|
|
|
|
||||||
LL | Drop::drop(&mut Foo)
|
LL | Drop::drop(&mut Foo)
|
||||||
| ^^^^^^^^^^
|
| ^^^^^^^^^^
|
||||||
|
Loading…
Reference in New Issue
Block a user