Skip to content

Commit 7f7f679

Browse files
committed
Do not recomment cloning explicit &mut expressions
1 parent 5a7caa3 commit 7f7f679

File tree

3 files changed

+5
-24
lines changed

3 files changed

+5
-24
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10331033
while let hir::ExprKind::AddrOf(.., inner) | hir::ExprKind::Unary(hir::UnOp::Deref, inner) =
10341034
&inner_expr.kind
10351035
{
1036+
if let hir::ExprKind::AddrOf(_, hir::Mutability::Mut, _) = inner_expr.kind {
1037+
// We assume that `&mut` refs are desired for their side-effects, so cloning the
1038+
// value wouldn't do what the user wanted.
1039+
return;
1040+
}
10361041
inner_expr = inner;
10371042
}
10381043
if inner_expr.span.lo() != expr.span.lo() {

tests/ui/borrowck/borrowck-overloaded-index-move-index.stderr

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ LL | println!("{}", f[s]);
1111
...
1212
LL | use_mut(rs);
1313
| -- borrow later used here
14-
|
15-
help: consider cloning the value if the performance cost is acceptable
16-
|
17-
LL - let rs = &mut s;
18-
LL + let rs = s.clone();
19-
|
2014

2115
error[E0505]: cannot move out of `s` because it is borrowed
2216
--> $DIR/borrowck-overloaded-index-move-index.rs:53:7
@@ -31,12 +25,6 @@ LL | f[s] = 10;
3125
...
3226
LL | use_mut(rs);
3327
| -- borrow later used here
34-
|
35-
help: consider cloning the value if the performance cost is acceptable
36-
|
37-
LL - let rs = &mut s;
38-
LL + let rs = s.clone();
39-
|
4028

4129
error[E0382]: use of moved value: `s`
4230
--> $DIR/borrowck-overloaded-index-move-index.rs:53:7

tests/ui/nll/polonius/polonius-smoke-test.stderr

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ LL | let z = x;
2727
| ^ move out of `x` occurs here
2828
LL | y
2929
| - returning this value requires that `*x` is borrowed for `'1`
30-
|
31-
help: consider cloning the value if the performance cost is acceptable
32-
|
33-
LL - let y = &mut *x;
34-
LL + let y = x.clone();
35-
|
3630

3731
error[E0505]: cannot move out of `s` because it is borrowed
3832
--> $DIR/polonius-smoke-test.rs:42:5
@@ -46,12 +40,6 @@ LL | s;
4640
| ^ move out of `s` occurs here
4741
LL | tmp;
4842
| --- borrow later used here
49-
|
50-
help: consider cloning the value if the performance cost is acceptable
51-
|
52-
LL - let r = &mut *s;
53-
LL + let r = s.clone();
54-
|
5543

5644
error: aborting due to 4 previous errors
5745

0 commit comments

Comments
 (0)