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

Lines changed: 22 additions & 7 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 16 additions & 12 deletions
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

0 commit comments

Comments
 (0)