Skip to content
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

Suggest removing &mut x when x: &mut T and &mut T is expected type #68697

Closed
Centril opened this issue Jan 31, 2020 · 6 comments · Fixed by #87453
Closed

Suggest removing &mut x when x: &mut T and &mut T is expected type #68697

Centril opened this issue Jan 31, 2020 · 6 comments · Fixed by #87453
Assignees
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Centril
Copy link
Contributor

Centril commented Jan 31, 2020

An example:

struct A;

fn bar(x: &mut A) {}

fn foo(x: &mut A) {
    bar(&mut x);
}

results in:

error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
 --> src/lib.rs:6:9
  |
5 | fn foo(x: &mut A) {
  |        - help: consider changing this to be mutable: `mut x`
6 |     bar(&mut x);
  |         ^^^^^^ cannot borrow as mutable

but we could say:

error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
 --> src/lib.rs:6:9
  |
5 | fn foo(x: &mut A) {
6 |     bar(&mut x);
  |         ^^^^^^ cannot borrow as mutable
  |         ---- help: remove the unnecessary `&mut` here
@Centril Centril added A-typesystem Area: The type system A-diagnostics Area: Messages for errors, warnings, and lints A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Jan 31, 2020
@LeSeulArtichaut
Copy link
Contributor

I’d be interested in working on this. I’ll probably take a look in the next few days.

@LeSeulArtichaut
Copy link
Contributor

@rustbot claim

@rustbot rustbot self-assigned this Feb 1, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Feb 2, 2020
@LeSeulArtichaut
Copy link
Contributor

Looks like this is a regression, see 3303e68

@LeSeulArtichaut
Copy link
Contributor

@rustbot release-assignment

@rustbot rustbot removed their assignment Feb 11, 2020
@ibraheemdev
Copy link
Member

I think this issue was fixed:

error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
 --> src/lib.rs:6:9
  |
6 |     bar(&mut x);
  |         ^^^^^^
  |         |
  |         cannot borrow as mutable
  |         try removing `&mut` here

@ibraheemdev
Copy link
Member

Actually, that should be a help message. @rustbot claim

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jul 28, 2021
Suggest removing unnecessary &mut as help message

Closes rust-lang#68697
@bors bors closed this as completed in 954137e Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants