]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/drop_forget_copy.rs
Rollup merge of #102764 - compiler-errors:issue-102762, r=jackh726
[rust.git] / src / tools / clippy / tests / ui / drop_forget_copy.rs
1 #![warn(clippy::drop_copy, clippy::forget_copy)]
2 #![allow(clippy::toplevel_ref_arg, clippy::drop_ref, clippy::forget_ref, unused_mut)]
3
4 use std::mem::{drop, forget};
5 use std::vec::Vec;
6
7 #[derive(Copy, Clone)]
8 struct SomeStruct;
9
10 struct AnotherStruct {
11     x: u8,
12     y: u8,
13     z: Vec<u8>,
14 }
15
16 impl Clone for AnotherStruct {
17     fn clone(&self) -> AnotherStruct {
18         AnotherStruct {
19             x: self.x,
20             y: self.y,
21             z: self.z.clone(),
22         }
23     }
24 }
25
26 fn main() {
27     let s1 = SomeStruct {};
28     let s2 = s1;
29     let s3 = &s1;
30     let mut s4 = s1;
31     let ref s5 = s1;
32
33     drop(s1);
34     drop(s2);
35     drop(s3);
36     drop(s4);
37     drop(s5);
38
39     forget(s1);
40     forget(s2);
41     forget(s3);
42     forget(s4);
43     forget(s5);
44
45     let a1 = AnotherStruct {
46         x: 255,
47         y: 0,
48         z: vec![1, 2, 3],
49     };
50     let a2 = &a1;
51     let mut a3 = a1.clone();
52     let ref a4 = a1;
53     let a5 = a1.clone();
54
55     drop(a2);
56     drop(a3);
57     drop(a4);
58     drop(a5);
59
60     forget(a2);
61     let a3 = &a1;
62     forget(a3);
63     forget(a4);
64     let a5 = a1.clone();
65     forget(a5);
66 }
67
68 #[allow(unused)]
69 #[allow(clippy::unit_cmp)]
70 fn issue9482(x: u8) {
71     fn println_and<T>(t: T) -> T {
72         println!("foo");
73         t
74     }
75
76     match x {
77         0 => drop(println_and(12)), // Don't lint (copy type), we only care about side-effects
78         1 => drop(println_and(String::new())), // Don't lint (no copy type), we only care about side-effects
79         2 => {
80             drop(println_and(13)); // Lint, even if we only care about the side-effect, it's already in a block
81         },
82         3 if drop(println_and(14)) == () => (), // Lint, idiomatic use is only in body of `Arm`
83         4 => drop(2),                           // Lint, not a fn/method call
84         _ => (),
85     }
86 }