]> git.lizzy.rs Git - rust.git/blob - tests/ui/drop_forget_copy.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / drop_forget_copy.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::drop_copy, clippy::forget_copy)]
11 #![allow(clippy::toplevel_ref_arg, clippy::drop_ref, clippy::forget_ref, unused_mut)]
12
13 use std::mem::{drop, forget};
14 use std::vec::Vec;
15
16 #[derive(Copy, Clone)]
17 struct SomeStruct {}
18
19 struct AnotherStruct {
20     x: u8,
21     y: u8,
22     z: Vec<u8>,
23 }
24
25 impl Clone for AnotherStruct {
26     fn clone(&self) -> AnotherStruct {
27         AnotherStruct {
28             x: self.x,
29             y: self.y,
30             z: self.z.clone(),
31         }
32     }
33 }
34
35 fn main() {
36     let s1 = SomeStruct {};
37     let s2 = s1;
38     let s3 = &s1;
39     let mut s4 = s1;
40     let ref s5 = s1;
41
42     drop(s1);
43     drop(s2);
44     drop(s3);
45     drop(s4);
46     drop(s5);
47
48     forget(s1);
49     forget(s2);
50     forget(s3);
51     forget(s4);
52     forget(s5);
53
54     let a1 = AnotherStruct {
55         x: 255,
56         y: 0,
57         z: vec![1, 2, 3],
58     };
59     let a2 = &a1;
60     let mut a3 = a1.clone();
61     let ref a4 = a1;
62     let a5 = a1.clone();
63
64     drop(a2);
65     drop(a3);
66     drop(a4);
67     drop(a5);
68
69     forget(a2);
70     let a3 = &a1;
71     forget(a3);
72     forget(a4);
73     let a5 = a1.clone();
74     forget(a5);
75 }