mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-03 12:13:43 +00:00
2df4f7dd8c
When encountering a move conflict, on an expression that is `!Copy` passed as an argument to an `fn` that is `impl AsRef`, suggest borrowing the expression. ``` error[E0382]: use of moved value: `bar` --> f204.rs:14:15 | 12 | let bar = Bar; | --- move occurs because `bar` has type `Bar`, which does not implement the `Copy` trait 13 | foo(bar); | --- value moved here 14 | let baa = bar; | ^^^ value used here after move | help: borrow the value to avoid moving it | 13 | foo(&bar); | + ``` Fix #41708
38 lines
769 B
Rust
38 lines
769 B
Rust
//@ run-rustfix
|
|
#![allow(unused_mut)]
|
|
use std::borrow::{Borrow, BorrowMut};
|
|
use std::convert::{AsMut, AsRef};
|
|
struct Bar;
|
|
|
|
impl AsRef<Bar> for Bar {
|
|
fn as_ref(&self) -> &Bar {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl AsMut<Bar> for Bar {
|
|
fn as_mut(&mut self) -> &mut Bar {
|
|
self
|
|
}
|
|
}
|
|
|
|
fn foo<T: AsRef<Bar>>(_: T) {}
|
|
fn qux<T: AsMut<Bar>>(_: T) {}
|
|
fn bat<T: Borrow<T>>(_: T) {}
|
|
fn baz<T: BorrowMut<T>>(_: T) {}
|
|
|
|
pub fn main() {
|
|
let bar = Bar;
|
|
foo(bar);
|
|
let _baa = bar; //~ ERROR use of moved value
|
|
let mut bar = Bar;
|
|
qux(bar);
|
|
let _baa = bar; //~ ERROR use of moved value
|
|
let bar = Bar;
|
|
bat(bar);
|
|
let _baa = bar; //~ ERROR use of moved value
|
|
let mut bar = Bar;
|
|
baz(bar);
|
|
let _baa = bar; //~ ERROR use of moved value
|
|
}
|