forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide suggestions for some moved value errors
When encountering an used moved value where the previous move happened in a `match` or `if let` pattern, suggest using `ref`. Fix rust-lang#63988. When encountering a `&mut` value that is used in multiple iterations of a loop, suggest reborrowing it with `&mut *`. Fix rust-lang#62112.
- Loading branch information
Showing
17 changed files
with
315 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Issue #63988 | ||
#[derive(Debug)] | ||
struct S; | ||
fn foo(_: Option<S>) {} | ||
|
||
enum E { | ||
V { | ||
s: S, | ||
} | ||
} | ||
fn bar(_: E) {} | ||
|
||
fn main() { | ||
let s = Some(S); | ||
if let Some(mut x) = s { | ||
x = S; | ||
} | ||
foo(s); //~ ERROR use of moved value: `s` | ||
let mut e = E::V { s: S }; | ||
let E::V { s: mut x } = e; | ||
x = S; | ||
bar(e); //~ ERROR use of moved value: `e` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
error[E0382]: use of moved value: `s` | ||
--> $DIR/move-in-pattern-mut.rs:18:9 | ||
| | ||
LL | if let Some(mut x) = s { | ||
| ----- value moved here | ||
... | ||
LL | foo(s); | ||
| ^ value used here after partial move | ||
| | ||
= note: move occurs because value has type `S`, which does not implement the `Copy` trait | ||
help: borrow this field in the pattern to avoid moving `s.0` | ||
| | ||
LL | if let Some(ref mut x) = s { | ||
| ^^^ | ||
|
||
error[E0382]: use of moved value: `e` | ||
--> $DIR/move-in-pattern-mut.rs:22:9 | ||
| | ||
LL | let E::V { s: mut x } = e; | ||
| ----- value moved here | ||
LL | x = S; | ||
LL | bar(e); | ||
| ^ value used here after partial move | ||
| | ||
= note: move occurs because value has type `S`, which does not implement the `Copy` trait | ||
help: borrow this field in the pattern to avoid moving `e.s` | ||
| | ||
LL | let E::V { s: ref mut x } = e; | ||
| ^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// run-rustfix | ||
// Issue #63988 | ||
#[derive(Debug)] | ||
struct S; | ||
fn foo(_: Option<S>) {} | ||
|
||
enum E { | ||
V { | ||
s: S, | ||
} | ||
} | ||
fn bar(_: E) {} | ||
|
||
fn main() { | ||
let s = Some(S); | ||
if let Some(ref x) = s { | ||
let _ = x; | ||
} | ||
foo(s); //~ ERROR use of moved value: `s` | ||
let e = E::V { s: S }; | ||
let E::V { s: ref x } = e; | ||
let _ = x; | ||
bar(e); //~ ERROR use of moved value: `e` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// run-rustfix | ||
// Issue #63988 | ||
#[derive(Debug)] | ||
struct S; | ||
fn foo(_: Option<S>) {} | ||
|
||
enum E { | ||
V { | ||
s: S, | ||
} | ||
} | ||
fn bar(_: E) {} | ||
|
||
fn main() { | ||
let s = Some(S); | ||
if let Some(x) = s { | ||
let _ = x; | ||
} | ||
foo(s); //~ ERROR use of moved value: `s` | ||
let e = E::V { s: S }; | ||
let E::V { s: x } = e; | ||
let _ = x; | ||
bar(e); //~ ERROR use of moved value: `e` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
error[E0382]: use of moved value: `s` | ||
--> $DIR/move-in-pattern.rs:19:9 | ||
| | ||
LL | if let Some(x) = s { | ||
| - value moved here | ||
... | ||
LL | foo(s); | ||
| ^ value used here after partial move | ||
| | ||
= note: move occurs because value has type `S`, which does not implement the `Copy` trait | ||
help: borrow this field in the pattern to avoid moving `s.0` | ||
| | ||
LL | if let Some(ref x) = s { | ||
| ^^^ | ||
|
||
error[E0382]: use of moved value: `e` | ||
--> $DIR/move-in-pattern.rs:23:9 | ||
| | ||
LL | let E::V { s: x } = e; | ||
| - value moved here | ||
LL | let _ = x; | ||
LL | bar(e); | ||
| ^ value used here after partial move | ||
| | ||
= note: move occurs because value has type `S`, which does not implement the `Copy` trait | ||
help: borrow this field in the pattern to avoid moving `e.s` | ||
| | ||
LL | let E::V { s: ref x } = e; | ||
| ^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// run-rustfix | ||
#![allow(dead_code)] | ||
|
||
struct Events<R>(R); | ||
|
||
struct Other; | ||
|
||
pub trait Trait<T> { | ||
fn handle(value: T) -> Self; | ||
} | ||
|
||
// Blanket impl. (If you comment this out, compiler figures out that it | ||
// is passing an `&mut` to a method that must be expecting an `&mut`, | ||
// and injects an auto-reborrow.) | ||
impl<T, U> Trait<U> for T where T: From<U> { | ||
fn handle(_: U) -> Self { unimplemented!() } | ||
} | ||
|
||
impl<'a, R> Trait<&'a mut Events<R>> for Other { | ||
fn handle(_: &'a mut Events<R>) -> Self { unimplemented!() } | ||
} | ||
|
||
fn this_compiles<'a, R>(value: &'a mut Events<R>) { | ||
for _ in 0..3 { | ||
Other::handle(&mut *value); | ||
} | ||
} | ||
|
||
fn this_does_not<'a, R>(value: &'a mut Events<R>) { | ||
for _ in 0..3 { | ||
Other::handle(&mut *value); //~ ERROR use of moved value: `value` | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// run-rustfix | ||
#![allow(dead_code)] | ||
|
||
struct Events<R>(R); | ||
|
||
struct Other; | ||
|
||
pub trait Trait<T> { | ||
fn handle(value: T) -> Self; | ||
} | ||
|
||
// Blanket impl. (If you comment this out, compiler figures out that it | ||
// is passing an `&mut` to a method that must be expecting an `&mut`, | ||
// and injects an auto-reborrow.) | ||
impl<T, U> Trait<U> for T where T: From<U> { | ||
fn handle(_: U) -> Self { unimplemented!() } | ||
} | ||
|
||
impl<'a, R> Trait<&'a mut Events<R>> for Other { | ||
fn handle(_: &'a mut Events<R>) -> Self { unimplemented!() } | ||
} | ||
|
||
fn this_compiles<'a, R>(value: &'a mut Events<R>) { | ||
for _ in 0..3 { | ||
Other::handle(&mut *value); | ||
} | ||
} | ||
|
||
fn this_does_not<'a, R>(value: &'a mut Events<R>) { | ||
for _ in 0..3 { | ||
Other::handle(value); //~ ERROR use of moved value: `value` | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error[E0382]: use of moved value: `value` | ||
--> $DIR/mut-borrow-in-loop-2.rs:31:23 | ||
| | ||
LL | fn this_does_not<'a, R>(value: &'a mut Events<R>) { | ||
| ----- move occurs because `value` has type `&mut Events<R>`, which does not implement the `Copy` trait | ||
LL | for _ in 0..3 { | ||
LL | Other::handle(value); | ||
| ^^^^^ value moved here, in previous iteration of loop | ||
| | ||
help: consider creating a fresh reborrow of `value` here | ||
| | ||
LL | Other::handle(&mut *value); | ||
| ^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.