]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/diagnostics/liveness.rs
Rollup merge of #107264 - ferrocene:pa-private-items, r=Mark-Simulacrum
[rust.git] / tests / ui / closures / 2229_closure_analysis / diagnostics / liveness.rs
1 // edition:2021
2
3 // check-pass
4 #![allow(unreachable_code)]
5 #![warn(unused)]
6 #![allow(dead_code)]
7
8 #[derive(Debug)]
9 struct Point {
10     x: i32,
11     y: i32,
12 }
13
14 pub fn f() {
15     let mut a = 1;
16     let mut c = Point{ x:1, y:0 };
17
18     // Captured by value, but variable is dead on entry.
19     (move || {
20         // This will not trigger a warning for unused variable as
21         // c.x will be treated as a Non-tracked place
22         c.x = 1;
23         println!("{}", c.x);
24         a = 1; //~ WARN value captured by `a` is never read
25         println!("{}", a);
26     })();
27
28     // Read and written to, but never actually used.
29     (move || {
30         // This will not trigger a warning for unused variable as
31         // c.x will be treated as a Non-tracked place
32         c.x += 1;
33         a += 1; //~ WARN unused variable: `a`
34     })();
35
36     (move || {
37         println!("{}", c.x);
38         // Value is read by closure itself on later invocations.
39         // This will not trigger a warning for unused variable as
40         // c.x will be treated as a Non-tracked place
41         c.x += 1;
42         println!("{}", a);
43         a += 1;
44     })();
45     let b = Box::new(42);
46     (move || {
47         println!("{}", c.x);
48         // Never read because this is FnOnce closure.
49         // This will not trigger a warning for unused variable as
50         // c.x will be treated as a Non-tracked place
51         c.x += 1;
52         println!("{}", a);
53         a += 1; //~ WARN value assigned to `a` is never read
54         drop(b);
55     })();
56 }
57
58 #[derive(Debug)]
59 struct MyStruct<'a>  {
60     x: Option<& 'a str>,
61     y: i32,
62 }
63
64 pub fn nested() {
65     let mut a : Option<& str>;
66     a = None;
67     let mut b : Option<& str>;
68     b = None;
69     let mut d = MyStruct{ x: None, y: 1};
70     let mut e = MyStruct{ x: None, y: 1};
71     (|| {
72         (|| {
73             // This will not trigger a warning for unused variable as
74             // d.x will be treated as a Non-tracked place
75             d.x = Some("d1");
76             d.x = Some("d2");
77             a = Some("d1"); //~ WARN value assigned to `a` is never read
78             a = Some("d2");
79         })();
80         (move || {
81             // This will not trigger a warning for unused variable as
82             //e.x will be treated as a Non-tracked place
83             e.x = Some("e1");
84             e.x = Some("e2");
85             b = Some("e1"); //~ WARN value assigned to `b` is never read
86                             //~| WARN unused variable: `b`
87             b = Some("e2"); //~ WARN value assigned to `b` is never read
88         })();
89     })();
90 }
91
92 fn main() {}