Skip to content

Diagnostic: suggest removing &mut instead of making the function argument mutable #76793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Alvenix opened this issue Sep 16, 2020 · 4 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Alvenix
Copy link

Alvenix commented Sep 16, 2020

For the following code:

fn main() {
    let mut borrowed = 843;
    example(&mut borrowed);
}

fn example(borrowed: &mut usize) {
    example2(&mut borrowed);
}

fn example2(_: &mut usize) {}

The compiler provide the following error message:

 --> src/main.rs:7:14
  |
6 | fn example(borrowed: &mut usize) {
  |            -------- help: consider changing this to be mutable: `mut borrowed`
7 |     example2(&mut borrowed);
  |              ^^^^^^^^^^^^^ cannot borrow as mutable

Although the suggestion actually work. (Because of type coercion). I think it would be a little nicer to suggest removing &mut when calling the function.

My friend who is new to Rust got a similar suggestion (and misunderstood it) which made him confused.

@tesuji

This comment has been minimized.

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 17, 2020
@tesuji
Copy link
Contributor

tesuji commented Sep 23, 2020

@rustbot claim

@tesuji
Copy link
Contributor

tesuji commented Sep 23, 2020

Duplicate of #75871.

@tesuji tesuji removed their assignment Sep 23, 2020
@jyn514
Copy link
Member

jyn514 commented Sep 23, 2020

Closing as duplicate.

@jyn514 jyn514 closed this as completed Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants