Skip to content

Error message improvement: Manual reborrowing with &mut *variable #81059

@mstange

Description

@mstange

In this testcase, I wanted to reborrow outer, but accidentally wrote &mut outer instead of &mut *outer. However, the error's suggestion was try removing `&mut` here which wouldn't have worked.
It would be better to have the suggestion try adding `*` here in this particular scenario.

fn test(outer: &mut Option<i32>) { match (&mut outer, 23) { (Some(inner), _) => { *inner = 17; }, _ => { *outer = Some(2); }, } }
 Compiling playground v0.0.1 (/playground) error[E0596]: cannot borrow `outer` as mutable, as it is not declared as mutable --> src/lib.rs:2:12 | 2 | match (&mut outer, 23) { | ^^^^^^^^^^ | | | cannot borrow as mutable | try removing `&mut` here error: aborting due to previous error 

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.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