Skip to content

Commit 70cf3f4

Browse files
committed
Put #[diagnostic::on_move] on File
1 parent d12e1e1 commit 70cf3f4

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

library/std/src/fs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ use crate::{error, fmt};
132132
/// [`read`]: File::read
133133
#[stable(feature = "rust1", since = "1.0.0")]
134134
#[cfg_attr(not(test), rustc_diagnostic_item = "File")]
135+
#[diagnostic::on_move(note = "you can use `File::try_clone` to duplicate a `File` instance")]
135136
pub struct File {
136137
inner: fs_imp::File,
137138
}

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
#![feature(const_trait_impl)]
279279
#![feature(decl_macro)]
280280
#![feature(deprecated_suggestion)]
281+
#![feature(diagnostic_on_move)]
281282
#![feature(doc_cfg)]
282283
#![feature(doc_masked)]
283284
#![feature(doc_notable_trait)]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ dont-require-annotations: NOTE
2+
3+
use std::fs::File;
4+
use std::sync::Arc;
5+
use std::rc::Rc;
6+
7+
fn main(){
8+
let file = File::open("foo.txt").unwrap();
9+
(file, file);
10+
//~^ ERROR use of moved value: `file`
11+
//~| NOTE you can use `File::try_clone` to duplicate a `File` instance
12+
13+
let arc = Arc::new(42);
14+
//~^ NOTE this move could be avoided by cloning the original `Arc`, which is inexpensive
15+
(arc, arc);
16+
//~^ ERROR the type `Arc` does not implement `Copy`
17+
//~| NOTE consider using `Arc::clone`
18+
19+
20+
let rc = Rc::new(12);
21+
//~^ NOTE this move could be avoided by cloning the original `Rc`, which is inexpensive
22+
(rc, rc);
23+
//~^ ERROR the type `Rc` does not implement `Copy`
24+
//~| NOTE consider using `Rc::clone`
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0382]: use of moved value: `file`
2+
--> $DIR/std_impls.rs:9:12
3+
|
4+
LL | let file = File::open("foo.txt").unwrap();
5+
| ---- move occurs because `file` has type `File`, which does not implement the `Copy` trait
6+
LL | (file, file);
7+
| ---- ^^^^ value used here after move
8+
| |
9+
| value moved here
10+
|
11+
= note: you can use `File::try_clone` to duplicate a `File` instance
12+
13+
error[E0382]: the type `Arc` does not implement `Copy`
14+
--> $DIR/std_impls.rs:15:11
15+
|
16+
LL | let arc = Arc::new(42);
17+
| --- this move could be avoided by cloning the original `Arc`, which is inexpensive
18+
LL |
19+
LL | (arc, arc);
20+
| --- ^^^ value used here after move
21+
| |
22+
| value moved here
23+
|
24+
= note: consider using `Arc::clone`
25+
help: clone the value to increment its reference count
26+
|
27+
LL | (arc.clone(), arc);
28+
| ++++++++
29+
30+
error[E0382]: the type `Rc` does not implement `Copy`
31+
--> $DIR/std_impls.rs:22:10
32+
|
33+
LL | let rc = Rc::new(12);
34+
| -- this move could be avoided by cloning the original `Rc`, which is inexpensive
35+
LL |
36+
LL | (rc, rc);
37+
| -- ^^ value used here after move
38+
| |
39+
| value moved here
40+
|
41+
= note: consider using `Rc::clone`
42+
help: clone the value to increment its reference count
43+
|
44+
LL | (rc.clone(), rc);
45+
| ++++++++
46+
47+
error: aborting due to 3 previous errors
48+
49+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)