Skip to content

Better suggested fix available for passing &mut (&mut val) #45392

Closed
@FauxFaux

Description

@FauxFaux

Given code like:

fn foo(a: &mut Bar) {}

fn baz(b: &mut Bar) {
    foo(&mut b)
}

rustc correctly points out that you "cannot borrow immutable argument b as mutable", and offers a fix of:

fn baz(b: &mut Bar) {
       - consider changing this to `mut b`

A better fix, in this case (and many others where I make this error), is to simply drop the &mut:

fn baz(b: &mut Bar) {
    foo(b)
}

I hit this error quite frequently, and remember finding the addition and removal of &mut until the compiler shut up about syntactic noise that I didn't care about (etc. etc.) pretty confusing; especially around Write.


Full code as a playground: https://play.rust-lang.org/?gist=9b69414d92287359bd6f1b91a53b254a&version=stable

If anyone can think of a better way to phrase the subject of this issue, please do change it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions