Skip to content

Commit 348997a

Browse files
Rollup merge of #81147 - estebank:drop-suggestion, r=varkor
Fix structured suggestion for explicit `drop` call
2 parents 6af6c40 + 70a43e0 commit 348997a

15 files changed

+107
-27
lines changed

compiler/rustc_typeck/src/check/callee.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ pub fn check_legal_trait_for_method_call(
2525
tcx: TyCtxt<'_>,
2626
span: Span,
2727
receiver: Option<Span>,
28+
expr_span: Span,
2829
trait_id: DefId,
2930
) {
3031
if tcx.lang_items().drop_trait() == Some(trait_id) {
3132
let mut err = struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
3233
err.span_label(span, "explicit destructor calls not allowed");
3334

34-
let snippet = receiver
35+
let (sp, suggestion) = receiver
3536
.and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
36-
.unwrap_or_default();
37-
38-
let suggestion =
39-
if snippet.is_empty() { "drop".to_string() } else { format!("drop({})", snippet) };
37+
.filter(|snippet| !snippet.is_empty())
38+
.map(|snippet| (expr_span, format!("drop({})", snippet)))
39+
.unwrap_or_else(|| (span, "drop".to_string()));
4040

4141
err.span_suggestion(
42-
span,
43-
&format!("consider using `drop` function: `{}`", suggestion),
44-
String::new(),
45-
Applicability::Unspecified,
42+
sp,
43+
"consider using `drop` function",
44+
suggestion,
45+
Applicability::MaybeIncorrect,
4646
);
4747

4848
err.emit();

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11631163
debug!("instantiate_value_path: def_id={:?} container={:?}", def_id, container);
11641164
match container {
11651165
ty::TraitContainer(trait_did) => {
1166-
callee::check_legal_trait_for_method_call(tcx, span, None, trait_did)
1166+
callee::check_legal_trait_for_method_call(tcx, span, None, span, trait_did)
11671167
}
11681168
ty::ImplContainer(impl_def_id) => {
11691169
if segments.len() == 1 {

compiler/rustc_typeck/src/check/method/confirm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
508508
self.tcx,
509509
self.span,
510510
Some(self.self_expr.span),
511+
self.call_expr.span,
511512
trait_def_id,
512513
),
513514
ty::ImplContainer(..) => {}

src/test/ui/error-codes/E0040.fixed

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run-rustfix
2+
struct Foo {
3+
x: i32,
4+
}
5+
6+
impl Drop for Foo {
7+
fn drop(&mut self) {
8+
println!("kaboom");
9+
}
10+
}
11+
12+
fn main() {
13+
let mut x = Foo { x: -7 };
14+
x.x = 0;
15+
println!("{}", x.x);
16+
drop(x);
17+
//~^ ERROR E0040
18+
}

src/test/ui/error-codes/E0040.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
struct Foo {
23
x: i32,
34
}
@@ -10,6 +11,8 @@ impl Drop for Foo {
1011

1112
fn main() {
1213
let mut x = Foo { x: -7 };
14+
x.x = 0;
15+
println!("{}", x.x);
1316
x.drop();
1417
//~^ ERROR E0040
1518
}

src/test/ui/error-codes/E0040.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0040]: explicit use of destructor method
2-
--> $DIR/E0040.rs:13:7
2+
--> $DIR/E0040.rs:16:7
33
|
44
LL | x.drop();
5-
| ^^^^
6-
| |
7-
| explicit destructor calls not allowed
8-
| help: consider using `drop` function: `drop(x)`
5+
| --^^^^--
6+
| | |
7+
| | explicit destructor calls not allowed
8+
| help: consider using `drop` function: `drop(x)`
99

1010
error: aborting due to previous error
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-rustfix
2+
struct Foo {
3+
x: isize
4+
}
5+
6+
impl Drop for Foo {
7+
fn drop(&mut self) {
8+
println!("kaboom");
9+
}
10+
}
11+
12+
fn main() {
13+
let x = Foo { x: 3 };
14+
println!("{}", x.x);
15+
drop(x); //~ ERROR explicit use of destructor method
16+
}

src/test/ui/explicit/explicit-call-to-dtor.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
struct Foo {
23
x: isize
34
}
@@ -10,5 +11,6 @@ impl Drop for Foo {
1011

1112
fn main() {
1213
let x = Foo { x: 3 };
14+
println!("{}", x.x);
1315
x.drop(); //~ ERROR explicit use of destructor method
1416
}

src/test/ui/explicit/explicit-call-to-dtor.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0040]: explicit use of destructor method
2-
--> $DIR/explicit-call-to-dtor.rs:13:7
2+
--> $DIR/explicit-call-to-dtor.rs:15:7
33
|
44
LL | x.drop();
5-
| ^^^^
6-
| |
7-
| explicit destructor calls not allowed
8-
| help: consider using `drop` function: `drop(x)`
5+
| --^^^^--
6+
| | |
7+
| | explicit destructor calls not allowed
8+
| help: consider using `drop` function: `drop(x)`
99

1010
error: aborting due to previous error
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// run-rustfix
2+
struct Foo {
3+
x: isize
4+
}
5+
6+
#[allow(drop_bounds)]
7+
trait Bar: Drop {
8+
fn blah(&self);
9+
}
10+
11+
impl Drop for Foo {
12+
fn drop(&mut self) {
13+
println!("kaboom");
14+
}
15+
}
16+
17+
impl Bar for Foo {
18+
fn blah(&self) {
19+
drop(self); //~ ERROR explicit use of destructor method
20+
}
21+
}
22+
23+
fn main() {
24+
let x = Foo { x: 3 };
25+
println!("{}", x.x);
26+
}

src/test/ui/explicit/explicit-call-to-supertrait-dtor.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
// run-rustfix
12
struct Foo {
23
x: isize
34
}
45

5-
trait Bar : Drop {
6+
#[allow(drop_bounds)]
7+
trait Bar: Drop {
68
fn blah(&self);
79
}
810

@@ -20,4 +22,5 @@ impl Bar for Foo {
2022

2123
fn main() {
2224
let x = Foo { x: 3 };
25+
println!("{}", x.x);
2326
}

src/test/ui/explicit/explicit-call-to-supertrait-dtor.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0040]: explicit use of destructor method
2-
--> $DIR/explicit-call-to-supertrait-dtor.rs:17:14
2+
--> $DIR/explicit-call-to-supertrait-dtor.rs:19:14
33
|
44
LL | self.drop();
5-
| ^^^^
6-
| |
7-
| explicit destructor calls not allowed
8-
| help: consider using `drop` function: `drop(self)`
5+
| -----^^^^--
6+
| | |
7+
| | explicit destructor calls not allowed
8+
| help: consider using `drop` function: `drop(self)`
99

1010
error: aborting due to previous error
1111

src/test/ui/illegal-ufcs-drop.fixed

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
struct Foo;
3+
4+
impl Drop for Foo {
5+
fn drop(&mut self) {}
6+
}
7+
8+
fn main() {
9+
drop(&mut Foo) //~ ERROR explicit use of destructor method
10+
}

src/test/ui/illegal-ufcs-drop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
struct Foo;
23

34
impl Drop for Foo {

src/test/ui/illegal-ufcs-drop.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0040]: explicit use of destructor method
2-
--> $DIR/illegal-ufcs-drop.rs:8:5
2+
--> $DIR/illegal-ufcs-drop.rs:9:5
33
|
44
LL | Drop::drop(&mut Foo)
55
| ^^^^^^^^^^

0 commit comments

Comments
 (0)