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 .as_ref() on E0382 when possible #84165

Closed
estebank opened this issue Apr 13, 2021 · 0 comments · Fixed by #84353
Closed

Suggest .as_ref() on E0382 when possible #84165

estebank opened this issue Apr 13, 2021 · 0 comments · Fixed by #84353
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

Given the following code:

struct Struct;
fn bar(s: &Struct) -> Struct {
    Struct
}

fn main() {
    let foo = Some(Struct);
    let _x: Option<Struct> = foo
        .map(|s| bar(&s));
    let _y = foo;
}

The current output is:

error[E0382]: use of moved value: `foo`
   --> src/main.rs:10:14
    |
7   |     let foo = Some(Struct);
    |         --- move occurs because `foo` has type `Option<Struct>`, which does not implement the `Copy` trait
8   |     let _x: Option<Struct> = foo
9   |         .map(|s| bar(&s));
    |          ---------------- `foo` moved due to this method call
10  |     let _y = foo;
    |              ^^^ value used here after move

We should suggest calling .as_ref() before the .map(...), as we do in some type errors.

error[E0308]: mismatched types
  --> src/main.rs:10:22
   |
10 |         .map(|s| bar(s));
   |          ---         ^ expected `&Struct`, found struct `Struct`
   |          |
   |          help: consider using `as_ref` instead: `as_ref().map`

This has been seen to cause trouble in the wild https://news.ycombinator.com/item?id=26794551

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. A-lifetimes Area: lifetime related labels Apr 13, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 20, 2021
Suggest `.as_ref()` on borrow error involving `Option`/`Result`

When encountering a E0382 borrow error involving an `Option` or `Result`
provide a suggestion to use `.as_ref()` on the prior move location to
avoid the move.

Fix rust-lang#84165.
@bors bors closed this as completed in 2763a05 Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant