Skip to content

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

Closed

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
    on Jan 31, 2020
  2. LeSeulArtichaut commented on Jan 31, 2020

    @LeSeulArtichaut
    Contributor

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

  3. LeSeulArtichaut commented on Feb 1, 2020

    @LeSeulArtichaut
    Contributor

    @rustbot claim

  4. self-assigned this
    on Feb 1, 2020
  5. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Feb 2, 2020
  6. LeSeulArtichaut commented on Feb 2, 2020

    @LeSeulArtichaut
    Contributor

    Looks like this is a regression, see 3303e68

  7. LeSeulArtichaut commented on Feb 11, 2020

    @LeSeulArtichaut
    Contributor

    @rustbot release-assignment

  8. removed their assignment
    on Feb 11, 2020
  9. ibraheemdev commented on Jul 25, 2021

    @ibraheemdev
    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
  10. ibraheemdev commented on Jul 25, 2021

    @ibraheemdev
    Member

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

  11. added 2 commits that reference this issue on Jul 28, 2021
    e5677a6
    954137e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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