Skip to content

Suggest &foo when passing foo to a function expecting AsRef<T>... #41708

Closed
@oli-obk

Description

@oli-obk

... and trying to use foo afterwards, which triggers

error[E0382]: use of moved value: foo

Example:

struct Bar;

impl AsRef<Bar> for Bar {
    fn as_ref(&self) -> &Bar {
        self
    }
}

fn foo<T: AsRef<Bar>>(_: T) {}

pub fn main() {
    let bar = Bar;
    foo(bar);
    let baa = bar;
}

currently yields

rustc 1.17.0 (56124baa9 2017-04-24)
error[E0382]: use of moved value: `bar`
  --> <anon>:15:9
   |
14 |     foo(bar);
   |         --- value moved here
15 |     let baa = bar;
   |         ^^^ value used here after move
   |
   = note: move occurs because `bar` has type `Bar`, which does not implement the `Copy` trait

error: aborting due to previous error

I suggest that it should also contain

help: if you meant to borrow `bar` use `foo(&bar)`?

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`C-enhancementCategory: An issue proposing an enhancement or a PR with one.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