Skip to content

Commit cf0b6b9

Browse files
committed
Account for dereference expressions
1 parent b8bd1d0 commit cf0b6b9

17 files changed

+208
-140
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
410410
fn add_move_hints(&self, error: GroupedMoveError<'tcx>, err: &mut Diagnostic, span: Span) {
411411
match error {
412412
GroupedMoveError::MovesFromPlace { mut binds_to, move_from, .. } => {
413-
err.span_suggestion_verbose(
414-
span.shrink_to_lo(),
415-
"consider borrowing here",
416-
"&".to_string(),
417-
Applicability::Unspecified,
418-
);
419-
413+
self.add_borrow_suggestions(err, span);
420414
if binds_to.is_empty() {
421415
let place_ty = move_from.ty(self.body, self.infcx.tcx).ty;
422416
let place_desc = match self.describe_place(move_from.as_ref()) {
@@ -459,6 +453,27 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
459453
}
460454
}
461455

456+
fn add_borrow_suggestions(&self, err: &mut Diagnostic, span: Span) {
457+
match self.infcx.tcx.sess.source_map().span_to_snippet(span) {
458+
Ok(snippet) if snippet.starts_with('*') => {
459+
err.span_suggestion_verbose(
460+
span.with_hi(span.lo() + BytePos(1)),
461+
"consider removing the dereference here",
462+
String::new(),
463+
Applicability::MaybeIncorrect,
464+
);
465+
}
466+
_ => {
467+
err.span_suggestion_verbose(
468+
span.shrink_to_lo(),
469+
"consider borrowing here",
470+
"&".to_string(),
471+
Applicability::MaybeIncorrect,
472+
);
473+
}
474+
}
475+
}
476+
462477
fn add_move_error_suggestions(&self, err: &mut Diagnostic, binds_to: &[Local]) {
463478
let mut suggestions: Vec<(Span, String, String)> = Vec::new();
464479
for local in binds_to {

src/test/ui/borrowck/access-mode-in-closures.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ LL | match *s { S(v) => v }
77
| data moved here
88
| move occurs because `v` has type `Vec<isize>`, which does not implement the `Copy` trait
99
|
10-
help: consider borrowing here
10+
help: consider removing the dereference here
11+
|
12+
LL - match *s { S(v) => v }
13+
LL + match s { S(v) => v }
1114
|
12-
LL | match &*s { S(v) => v }
13-
| +
1415

1516
error: aborting due to previous error
1617

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-rustfix
2+
fn main() {
3+
4+
let x: Option<Box<_>> = Some(Box::new(1));
5+
6+
match x {
7+
Some(ref y) => {
8+
let _b = y; //~ ERROR cannot move out
9+
}
10+
_ => {}
11+
}
12+
}

src/test/ui/borrowck/borrowck-issue-2657-2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
fn main() {
23

34
let x: Option<Box<_>> = Some(Box::new(1));

src/test/ui/borrowck/borrowck-issue-2657-2.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
error[E0507]: cannot move out of `*y` which is behind a shared reference
2-
--> $DIR/borrowck-issue-2657-2.rs:7:18
2+
--> $DIR/borrowck-issue-2657-2.rs:8:18
33
|
44
LL | let _b = *y;
55
| ^^ move occurs because `*y` has type `Box<i32>`, which does not implement the `Copy` trait
66
|
7-
help: consider borrowing here
7+
help: consider removing the dereference here
8+
|
9+
LL - let _b = *y;
10+
LL + let _b = y;
811
|
9-
LL | let _b = &*y;
10-
| +
1112

1213
error: aborting due to previous error
1314

src/test/ui/borrowck/borrowck-move-error-with-note.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enum Foo {
1010

1111
fn blah() {
1212
let f = &Foo::Foo1(Box::new(1), Box::new(2));
13-
match &*f { //~ ERROR cannot move out of
13+
match f { //~ ERROR cannot move out of
1414
Foo::Foo1(num1,
1515
num2) => (),
1616
Foo::Foo2(num) => (),

src/test/ui/borrowck/borrowck-move-error-with-note.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ LL | Foo::Foo2(num) => (),
1111
| --- ...and here
1212
|
1313
= note: move occurs because these variables have types that don't implement the `Copy` trait
14-
help: consider borrowing here
14+
help: consider removing the dereference here
15+
|
16+
LL - match *f {
17+
LL + match f {
1518
|
16-
LL | match &*f {
17-
| +
1819

1920
error[E0509]: cannot move out of type `S`, which implements the `Drop` trait
2021
--> $DIR/borrowck-move-error-with-note.rs:30:11

src/test/ui/borrowck/borrowck-move-from-unsafe-ptr.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ error[E0507]: cannot move out of `*x` which is behind a raw pointer
44
LL | let y = *x;
55
| ^^ move occurs because `*x` has type `Box<isize>`, which does not implement the `Copy` trait
66
|
7-
help: consider borrowing here
7+
help: consider removing the dereference here
8+
|
9+
LL - let y = *x;
10+
LL + let y = x;
811
|
9-
LL | let y = &*x;
10-
| +
1112

1213
error: aborting due to previous error
1314

src/test/ui/borrowck/borrowck-move-out-of-overloaded-deref.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ error[E0507]: cannot move out of an `Rc`
44
LL | let _x = *Rc::new("hi".to_string());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
66
|
7-
help: consider borrowing here
7+
help: consider removing the dereference here
8+
|
9+
LL - let _x = *Rc::new("hi".to_string());
10+
LL + let _x = Rc::new("hi".to_string());
811
|
9-
LL | let _x = &*Rc::new("hi".to_string());
10-
| +
1112

1213
error: aborting due to previous error
1314

src/test/ui/borrowck/issue-20801.stderr

+16-12
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,47 @@ error[E0507]: cannot move out of a mutable reference
44
LL | let a = unsafe { *mut_ref() };
55
| ^^^^^^^^^^ move occurs because value has type `T`, which does not implement the `Copy` trait
66
|
7-
help: consider borrowing here
7+
help: consider removing the dereference here
8+
|
9+
LL - let a = unsafe { *mut_ref() };
10+
LL + let a = unsafe { mut_ref() };
811
|
9-
LL | let a = unsafe { &*mut_ref() };
10-
| +
1112

1213
error[E0507]: cannot move out of a shared reference
1314
--> $DIR/issue-20801.rs:29:22
1415
|
1516
LL | let b = unsafe { *imm_ref() };
1617
| ^^^^^^^^^^ move occurs because value has type `T`, which does not implement the `Copy` trait
1718
|
18-
help: consider borrowing here
19+
help: consider removing the dereference here
20+
|
21+
LL - let b = unsafe { *imm_ref() };
22+
LL + let b = unsafe { imm_ref() };
1923
|
20-
LL | let b = unsafe { &*imm_ref() };
21-
| +
2224

2325
error[E0507]: cannot move out of a raw pointer
2426
--> $DIR/issue-20801.rs:32:22
2527
|
2628
LL | let c = unsafe { *mut_ptr() };
2729
| ^^^^^^^^^^ move occurs because value has type `T`, which does not implement the `Copy` trait
2830
|
29-
help: consider borrowing here
31+
help: consider removing the dereference here
32+
|
33+
LL - let c = unsafe { *mut_ptr() };
34+
LL + let c = unsafe { mut_ptr() };
3035
|
31-
LL | let c = unsafe { &*mut_ptr() };
32-
| +
3336

3437
error[E0507]: cannot move out of a raw pointer
3538
--> $DIR/issue-20801.rs:35:22
3639
|
3740
LL | let d = unsafe { *const_ptr() };
3841
| ^^^^^^^^^^^^ move occurs because value has type `T`, which does not implement the `Copy` trait
3942
|
40-
help: consider borrowing here
43+
help: consider removing the dereference here
44+
|
45+
LL - let d = unsafe { *const_ptr() };
46+
LL + let d = unsafe { const_ptr() };
4147
|
42-
LL | let d = unsafe { &*const_ptr() };
43-
| +
4448

4549
error: aborting due to 4 previous errors
4650

src/test/ui/borrowck/issue-54597-reject-move-out-of-borrow-via-pat.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ error[E0507]: cannot move out of `*array` which is behind a shared reference
44
LL | *array
55
| ^^^^^^ move occurs because `*array` has type `Vec<Value>`, which does not implement the `Copy` trait
66
|
7-
help: consider borrowing here
7+
help: consider removing the dereference here
8+
|
9+
LL - *array
10+
LL + array
811
|
9-
LL | &*array
10-
| +
1112

1213
error: aborting due to previous error
1314

src/test/ui/moves/move-out-of-array-ref.stderr

+16-12
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ LL | let [_, e, _, _] = *a;
77
| data moved here
88
| move occurs because `e` has type `D`, which does not implement the `Copy` trait
99
|
10-
help: consider borrowing here
10+
help: consider removing the dereference here
11+
|
12+
LL - let [_, e, _, _] = *a;
13+
LL + let [_, e, _, _] = a;
1114
|
12-
LL | let [_, e, _, _] = &*a;
13-
| +
1415

1516
error[E0508]: cannot move out of type `[D; 4]`, a non-copy array
1617
--> $DIR/move-out-of-array-ref.rs:13:27
@@ -21,10 +22,11 @@ LL | let [_, s @ .. , _] = *a;
2122
| data moved here
2223
| move occurs because `s` has type `[D; 2]`, which does not implement the `Copy` trait
2324
|
24-
help: consider borrowing here
25+
help: consider removing the dereference here
26+
|
27+
LL - let [_, s @ .. , _] = *a;
28+
LL + let [_, s @ .. , _] = a;
2529
|
26-
LL | let [_, s @ .. , _] = &*a;
27-
| +
2830

2931
error[E0508]: cannot move out of type `[D; 4]`, a non-copy array
3032
--> $DIR/move-out-of-array-ref.rs:18:24
@@ -35,10 +37,11 @@ LL | let [_, e, _, _] = *a;
3537
| data moved here
3638
| move occurs because `e` has type `D`, which does not implement the `Copy` trait
3739
|
38-
help: consider borrowing here
40+
help: consider removing the dereference here
41+
|
42+
LL - let [_, e, _, _] = *a;
43+
LL + let [_, e, _, _] = a;
3944
|
40-
LL | let [_, e, _, _] = &*a;
41-
| +
4245

4346
error[E0508]: cannot move out of type `[D; 4]`, a non-copy array
4447
--> $DIR/move-out-of-array-ref.rs:23:27
@@ -49,10 +52,11 @@ LL | let [_, s @ .. , _] = *a;
4952
| data moved here
5053
| move occurs because `s` has type `[D; 2]`, which does not implement the `Copy` trait
5154
|
52-
help: consider borrowing here
55+
help: consider removing the dereference here
56+
|
57+
LL - let [_, s @ .. , _] = *a;
58+
LL + let [_, s @ .. , _] = a;
5359
|
54-
LL | let [_, s @ .. , _] = &*a;
55-
| +
5660

5761
error: aborting due to 4 previous errors
5862

src/test/ui/nll/cannot-move-block-spans.stderr

+24-18
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,35 @@ error[E0507]: cannot move out of `*r` which is behind a shared reference
44
LL | let x = { *r };
55
| ^^ move occurs because `*r` has type `String`, which does not implement the `Copy` trait
66
|
7-
help: consider borrowing here
7+
help: consider removing the dereference here
8+
|
9+
LL - let x = { *r };
10+
LL + let x = { r };
811
|
9-
LL | let x = { &*r };
10-
| +
1112

1213
error[E0507]: cannot move out of `*r` which is behind a shared reference
1314
--> $DIR/cannot-move-block-spans.rs:6:22
1415
|
1516
LL | let y = unsafe { *r };
1617
| ^^ move occurs because `*r` has type `String`, which does not implement the `Copy` trait
1718
|
18-
help: consider borrowing here
19+
help: consider removing the dereference here
20+
|
21+
LL - let y = unsafe { *r };
22+
LL + let y = unsafe { r };
1923
|
20-
LL | let y = unsafe { &*r };
21-
| +
2224

2325
error[E0507]: cannot move out of `*r` which is behind a shared reference
2426
--> $DIR/cannot-move-block-spans.rs:7:26
2527
|
2628
LL | let z = loop { break *r; };
2729
| ^^ move occurs because `*r` has type `String`, which does not implement the `Copy` trait
2830
|
29-
help: consider borrowing here
31+
help: consider removing the dereference here
32+
|
33+
LL - let z = loop { break *r; };
34+
LL + let z = loop { break r; };
3035
|
31-
LL | let z = loop { break &*r; };
32-
| +
3336

3437
error[E0508]: cannot move out of type `[String; 2]`, a non-copy array
3538
--> $DIR/cannot-move-block-spans.rs:11:15
@@ -79,32 +82,35 @@ error[E0507]: cannot move out of `*r` which is behind a shared reference
7982
LL | let x = { let mut u = 0; u += 1; *r };
8083
| ^^ move occurs because `*r` has type `String`, which does not implement the `Copy` trait
8184
|
82-
help: consider borrowing here
85+
help: consider removing the dereference here
86+
|
87+
LL - let x = { let mut u = 0; u += 1; *r };
88+
LL + let x = { let mut u = 0; u += 1; r };
8389
|
84-
LL | let x = { let mut u = 0; u += 1; &*r };
85-
| +
8690

8791
error[E0507]: cannot move out of `*r` which is behind a shared reference
8892
--> $DIR/cannot-move-block-spans.rs:18:45
8993
|
9094
LL | let y = unsafe { let mut u = 0; u += 1; *r };
9195
| ^^ move occurs because `*r` has type `String`, which does not implement the `Copy` trait
9296
|
93-
help: consider borrowing here
97+
help: consider removing the dereference here
98+
|
99+
LL - let y = unsafe { let mut u = 0; u += 1; *r };
100+
LL + let y = unsafe { let mut u = 0; u += 1; r };
94101
|
95-
LL | let y = unsafe { let mut u = 0; u += 1; &*r };
96-
| +
97102

98103
error[E0507]: cannot move out of `*r` which is behind a shared reference
99104
--> $DIR/cannot-move-block-spans.rs:19:49
100105
|
101106
LL | let z = loop { let mut u = 0; u += 1; break *r; u += 2; };
102107
| ^^ move occurs because `*r` has type `String`, which does not implement the `Copy` trait
103108
|
104-
help: consider borrowing here
109+
help: consider removing the dereference here
110+
|
111+
LL - let z = loop { let mut u = 0; u += 1; break *r; u += 2; };
112+
LL + let z = loop { let mut u = 0; u += 1; break r; u += 2; };
105113
|
106-
LL | let z = loop { let mut u = 0; u += 1; break &*r; u += 2; };
107-
| +
108114

109115
error: aborting due to 9 previous errors
110116

0 commit comments

Comments
 (0)