diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2024-03-13 16:35:58 +0000 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2024-04-11 16:41:41 +0000 |
| commit | 7f7f6792f161d8b7f383ca18a1d820d2ca811bc4 (patch) | |
| tree | 7960c531ba7bcec4352d4cba1c6c6d125201a1f1 | |
| parent | 5a7caa3174f8174db817228d8c2a02aa4913095c (diff) | |
| download | rust-7f7f6792f161d8b7f383ca18a1d820d2ca811bc4.tar.gz rust-7f7f6792f161d8b7f383ca18a1d820d2ca811bc4.zip | |
Do not recomment cloning explicit `&mut` expressions
3 files changed, 5 insertions, 24 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index c385fb583bb..7c4623e6ea7 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -1033,6 +1033,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { while let hir::ExprKind::AddrOf(.., inner) | hir::ExprKind::Unary(hir::UnOp::Deref, inner) = &inner_expr.kind { + if let hir::ExprKind::AddrOf(_, hir::Mutability::Mut, _) = inner_expr.kind { + // We assume that `&mut` refs are desired for their side-effects, so cloning the + // value wouldn't do what the user wanted. + return; + } inner_expr = inner; } if inner_expr.span.lo() != expr.span.lo() { diff --git a/tests/ui/borrowck/borrowck-overloaded-index-move-index.stderr b/tests/ui/borrowck/borrowck-overloaded-index-move-index.stderr index 3386d2074a0..7f8cc74a715 100644 --- a/tests/ui/borrowck/borrowck-overloaded-index-move-index.stderr +++ b/tests/ui/borrowck/borrowck-overloaded-index-move-index.stderr @@ -11,12 +11,6 @@ LL | println!("{}", f[s]); ... LL | use_mut(rs); | -- borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let rs = &mut s; -LL + let rs = s.clone(); - | error[E0505]: cannot move out of `s` because it is borrowed --> $DIR/borrowck-overloaded-index-move-index.rs:53:7 @@ -31,12 +25,6 @@ LL | f[s] = 10; ... LL | use_mut(rs); | -- borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let rs = &mut s; -LL + let rs = s.clone(); - | error[E0382]: use of moved value: `s` --> $DIR/borrowck-overloaded-index-move-index.rs:53:7 diff --git a/tests/ui/nll/polonius/polonius-smoke-test.stderr b/tests/ui/nll/polonius/polonius-smoke-test.stderr index 77722beab98..534813b2d9f 100644 --- a/tests/ui/nll/polonius/polonius-smoke-test.stderr +++ b/tests/ui/nll/polonius/polonius-smoke-test.stderr @@ -27,12 +27,6 @@ LL | let z = x; | ^ move out of `x` occurs here LL | y | - returning this value requires that `*x` is borrowed for `'1` - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let y = &mut *x; -LL + let y = x.clone(); - | error[E0505]: cannot move out of `s` because it is borrowed --> $DIR/polonius-smoke-test.rs:42:5 @@ -46,12 +40,6 @@ LL | s; | ^ move out of `s` occurs here LL | tmp; | --- borrow later used here - | -help: consider cloning the value if the performance cost is acceptable - | -LL - let r = &mut *s; -LL + let r = s.clone(); - | error: aborting due to 4 previous errors |
