summary refs log tree commit diff
path: root/compiler/rustc_passes/src
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2025-01-25 23:27:01 -0500
committerGitHub <noreply@github.com>2025-01-25 23:27:01 -0500
commit64550d1ed8023bea8668888ea8cfa91f4978875f (patch)
tree822f92973f9c6feeed8eb1b7099f35533d8e95de /compiler/rustc_passes/src
parent182ccfa11fe5d7a3a6e9cc0d2100159dcd9963d2 (diff)
parent1dfc437aaf09185c9830b6b5214bc2a089fd297f (diff)
downloadrust-64550d1ed8023bea8668888ea8cfa91f4978875f.tar.gz
rust-64550d1ed8023bea8668888ea8cfa91f4978875f.zip
Rollup merge of #136032 - estebank:issue-136028, r=SparrowLii
Account for mutable borrow in argument suggestion

```
error: value assigned to `object` is never read
  --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:21:5
   |
LL |     object = &mut object2;
   |     ^^^^^^
   |
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
   |
LL ~ fn change_object3(object: &mut Object) {
LL |
LL |     let object2 = Object;
LL ~     *object = object2;
   |
```
instead of
```
error: value assigned to `object` is never read
  --> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:21:5
   |
LL |     object = &mut object2;
   |     ^^^^^^
   |
help: you might have meant to mutate the pointed at value being passed in, instead of changing the reference in the local binding
   |
LL ~ fn change_object3(object: &mut mut Object) {
LL |
LL |     let object2 = Object;
LL ~     *object = object2;
   |
```

Fix #136028.
Diffstat (limited to 'compiler/rustc_passes/src')
-rw-r--r--compiler/rustc_passes/src/errors.rs2
-rw-r--r--compiler/rustc_passes/src/liveness.rs16
2 files changed, 11 insertions, 7 deletions
diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs
index 196a0a46962..0aa50ad19ff 100644
--- a/compiler/rustc_passes/src/errors.rs
+++ b/compiler/rustc_passes/src/errors.rs
@@ -1791,7 +1791,7 @@ pub(crate) struct UnusedAssign {
 pub(crate) struct UnusedAssignSuggestion {
     pub pre: &'static str,
     #[suggestion_part(code = "{pre}mut ")]
-    pub ty_span: Span,
+    pub ty_span: Option<Span>,
     #[suggestion_part(code = "")]
     pub ty_ref_span: Span,
     #[suggestion_part(code = "*")]
diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs
index 426899a4d5c..73da8855e10 100644
--- a/compiler/rustc_passes/src/liveness.rs
+++ b/compiler/rustc_passes/src/liveness.rs
@@ -1620,24 +1620,28 @@ impl<'tcx> Liveness<'_, 'tcx> {
             && let item = self.ir.tcx.hir_owner_node(item_id)
             && let Some(fn_decl) = item.fn_decl()
             && let hir::PatKind::Binding(hir::BindingMode::MUT, _hir_id, ident, _) = pat.kind
-            && let Some((ty_span, pre)) = fn_decl
+            && let Some((lt, mut_ty)) = fn_decl
                 .inputs
                 .iter()
                 .filter_map(|ty| {
                     if ty.span == *ty_span
                         && let hir::TyKind::Ref(lt, mut_ty) = ty.kind
                     {
-                        // `&'name Ty` -> `&'name mut Ty` or `&Ty` -> `&mut Ty`
-                        Some((
-                            mut_ty.ty.span.shrink_to_lo(),
-                            if lt.ident.span.lo() == lt.ident.span.hi() { "" } else { " " },
-                        ))
+                        Some((lt, mut_ty))
                     } else {
                         None
                     }
                 })
                 .next()
         {
+            let ty_span = if mut_ty.mutbl.is_mut() {
+                // Leave `&'name mut Ty` and `&mut Ty` as they are (#136028).
+                None
+            } else {
+                // `&'name Ty` -> `&'name mut Ty` or `&Ty` -> `&mut Ty`
+                Some(mut_ty.ty.span.shrink_to_lo())
+            };
+            let pre = if lt.ident.span.lo() == lt.ident.span.hi() { "" } else { " " };
             Some(errors::UnusedAssignSuggestion {
                 ty_span,
                 pre,