Skip to content

Fixed more &x ->&mut x suggestions#157032

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
nullie:borrowck-deref-pattern-mut-suggestion-fix
May 30, 2026
Merged

Fixed more &x ->&mut x suggestions#157032
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
nullie:borrowck-deref-pattern-mut-suggestion-fix

Conversation

@nullie
Copy link
Copy Markdown
Contributor

@nullie nullie commented May 27, 2026

This PR fixes additional issues related to #148467, i.e. compiler still suggests incorrect fixes for adding mut to deref patterns (needing additional parens) in other bindings besides for loop

fn let_deref(num_ref: &u32) -> u32 {
    let &num = num_ref;

    num *= 2; //~ ERROR cannot assign twice to immutable variable `num`

    num
}

fn deref_inside_pattern(option_num_ref: Option<&u32>) {
    if let Some(&num) = option_num_ref {
        num *= 2; //~ ERROR cannot assign twice to immutable variable `num`

        println!("{num}");
    }
}
...
help: consider making this binding mutable
  |
2 |     let &mut num = num_ref;
  |          +++
...
help: consider making this binding mutable
   |
10 |     if let Some(&mut num) = option_num_ref {
   |                  +++

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 27, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 27, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

Copy link
Copy Markdown
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you merge the three commits? There is no need for them to be separate, they logically belong together. r=me once that is done.

(For next time: a nice way to do error message PRs is to add/change tests in a first commit, and then make the code changes in a second commit. That way the improvements in the diagnostics are easy to see in the second commit.)

View changes since this review

@nullie nullie force-pushed the borrowck-deref-pattern-mut-suggestion-fix branch from a0f000a to a5b0416 Compare May 29, 2026 12:51
@nullie nullie force-pushed the borrowck-deref-pattern-mut-suggestion-fix branch from a5b0416 to 9bef677 Compare May 29, 2026 13:34
@nullie
Copy link
Copy Markdown
Contributor Author

nullie commented May 29, 2026

r? @nnethercote regrouped into two commits as you suggested

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 29, 2026

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

Copy link
Copy Markdown
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Splitting it that way is really helpful, and makes reviewing much easier.

View changes since this review

@nnethercote
Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 29, 2026

📌 Commit 9bef677 has been approved by nnethercote

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 1. This pull request will be tested once the tree is reopened.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 29, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request May 30, 2026
…suggestion-fix, r=nnethercote

Fixed more &x ->&mut x suggestions

This PR fixes additional issues related to rust-lang#148467, i.e. compiler still suggests incorrect fixes for adding mut to deref patterns (needing additional parens) in other bindings besides for loop

```rust
fn let_deref(num_ref: &u32) -> u32 {
    let &num = num_ref;

    num *= 2; //~ ERROR cannot assign twice to immutable variable `num`

    num
}

fn deref_inside_pattern(option_num_ref: Option<&u32>) {
    if let Some(&num) = option_num_ref {
        num *= 2; //~ ERROR cannot assign twice to immutable variable `num`

        println!("{num}");
    }
}
```

```
...
help: consider making this binding mutable
  |
2 |     let &mut num = num_ref;
  |          +++
...
help: consider making this binding mutable
   |
10 |     if let Some(&mut num) = option_num_ref {
   |                  +++
```
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request May 30, 2026
…suggestion-fix, r=nnethercote

Fixed more &x ->&mut x suggestions

This PR fixes additional issues related to rust-lang#148467, i.e. compiler still suggests incorrect fixes for adding mut to deref patterns (needing additional parens) in other bindings besides for loop

```rust
fn let_deref(num_ref: &u32) -> u32 {
    let &num = num_ref;

    num *= 2; //~ ERROR cannot assign twice to immutable variable `num`

    num
}

fn deref_inside_pattern(option_num_ref: Option<&u32>) {
    if let Some(&num) = option_num_ref {
        num *= 2; //~ ERROR cannot assign twice to immutable variable `num`

        println!("{num}");
    }
}
```

```
...
help: consider making this binding mutable
  |
2 |     let &mut num = num_ref;
  |          +++
...
help: consider making this binding mutable
   |
10 |     if let Some(&mut num) = option_num_ref {
   |                  +++
```
rust-bors Bot pushed a commit that referenced this pull request May 30, 2026
…uwer

Rollup of 12 pull requests

Successful merges:

 - #154591 (Remove `will_cache_on_disk_for_key_fn`)
 - #156672 (Misc improvements to coroutine transform code)
 - #157027 (HIR ty lowering: Move some things into submodules)
 - #157051 (Allow two object files for a single CGU in CompiledModule)
 - #157100 (Some more per owner things)
 - #153497 (Use `trait_object_dummy_self` more & heavily fix+update related docs)
 - #155638 (Fix tupled closure signature in `AsyncFn` arg mismatch diagnostic)
 - #156826 (style: Clarify nullary call and `()` no-break rule applies past max width)
 - #157004 (Remove unused functions in `value_analysis.rs`)
 - #157032 (Fixed more &x ->&mut x suggestions)
 - #157033 (Note irrefutable while let in loop type errors)
 - #157139 (compiler: `ops::RangeInclusive` → `range::RangeInclusive`)

Failed merges:

 - #156875 (Correct and document semantics of `yield` terminator)
@rust-bors rust-bors Bot merged commit 6717454 into rust-lang:main May 30, 2026
12 checks passed
@rustbot rustbot added this to the 1.98.0 milestone May 30, 2026
rust-timer added a commit that referenced this pull request May 30, 2026
Rollup merge of #157032 - nullie:borrowck-deref-pattern-mut-suggestion-fix, r=nnethercote

Fixed more &x ->&mut x suggestions

This PR fixes additional issues related to #148467, i.e. compiler still suggests incorrect fixes for adding mut to deref patterns (needing additional parens) in other bindings besides for loop

```rust
fn let_deref(num_ref: &u32) -> u32 {
    let &num = num_ref;

    num *= 2; //~ ERROR cannot assign twice to immutable variable `num`

    num
}

fn deref_inside_pattern(option_num_ref: Option<&u32>) {
    if let Some(&num) = option_num_ref {
        num *= 2; //~ ERROR cannot assign twice to immutable variable `num`

        println!("{num}");
    }
}
```

```
...
help: consider making this binding mutable
  |
2 |     let &mut num = num_ref;
  |          +++
...
help: consider making this binding mutable
   |
10 |     if let Some(&mut num) = option_num_ref {
   |                  +++
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants