Skip to content

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

Closed
@Centril

Description

@Centril

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

Metadata

Metadata

Assignees

Labels

A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsA-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions