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