1
Fork 0

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.
This commit is contained in:
Jacob Pratt 2025-01-25 23:27:01 -05:00 committed by GitHub
commit 64550d1ed8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 114 additions and 57 deletions

View file

@ -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 = "*")]

View file

@ -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,