]> git.lizzy.rs Git - rust.git/blob - tests/ui/liveness/liveness-upvars.rs
Auto merge of #100178 - mati865:upgrade-mingw-w64-on-CI, r=nikic
[rust.git] / tests / ui / liveness / liveness-upvars.rs
1 // edition:2018
2 // check-pass
3 #![feature(generators)]
4 #![warn(unused)]
5 #![allow(unreachable_code)]
6
7 pub fn unintentional_copy_one() {
8     let mut last = None;
9     let mut f = move |s| {
10         last = Some(s); //~  WARN value assigned to `last` is never read
11                         //~| WARN unused variable: `last`
12     };
13     f("a");
14     f("b");
15     f("c");
16     dbg!(last.unwrap());
17 }
18
19 pub fn unintentional_copy_two() {
20     let mut sum = 0;
21     (1..10).for_each(move |x| {
22         sum += x; //~ WARN unused variable: `sum`
23     });
24     dbg!(sum);
25 }
26
27 pub fn f() {
28     let mut c = 0;
29
30     // Captured by value, but variable is dead on entry.
31     let _ = move || {
32         c = 1; //~ WARN value captured by `c` is never read
33         println!("{}", c);
34     };
35     let _ = async move {
36         c = 1; //~ WARN value captured by `c` is never read
37         println!("{}", c);
38     };
39
40     // Read and written to, but never actually used.
41     let _ = move || {
42         c += 1; //~ WARN unused variable: `c`
43     };
44     let _ = async move {
45         c += 1; //~  WARN value assigned to `c` is never read
46                 //~| WARN unused variable: `c`
47     };
48
49     let _ = move || {
50         println!("{}", c);
51         // Value is read by closure itself on later invocations.
52         c += 1;
53     };
54     let b = Box::new(42);
55     let _ = move || {
56         println!("{}", c);
57         // Never read because this is FnOnce closure.
58         c += 1; //~  WARN value assigned to `c` is never read
59         drop(b);
60     };
61     let _ = async move {
62         println!("{}", c);
63         // Never read because this is a generator.
64         c += 1; //~  WARN value assigned to `c` is never read
65     };
66 }
67
68 pub fn nested() {
69     let mut d = None;
70     let mut e = None;
71     let _ = || {
72         let _ = || {
73             d = Some("d1"); //~ WARN value assigned to `d` is never read
74             d = Some("d2");
75         };
76         let _ = move || {
77             e = Some("e1"); //~  WARN value assigned to `e` is never read
78                             //~| WARN unused variable: `e`
79             e = Some("e2"); //~  WARN value assigned to `e` is never read
80         };
81     };
82 }
83
84 pub fn g<T: Default>(mut v: T) {
85     let _ = |r| {
86         if r {
87             v = T::default(); //~ WARN value assigned to `v` is never read
88         } else {
89             drop(v);
90         }
91     };
92 }
93
94 pub fn h<T: Copy + Default + std::fmt::Debug>() {
95     let mut z = T::default();
96     let _ = move |b| {
97         loop {
98             if b {
99                 z = T::default(); //~  WARN value assigned to `z` is never read
100                                   //~| WARN unused variable: `z`
101             } else {
102                 return;
103             }
104         }
105         dbg!(z);
106     };
107 }
108
109 async fn yield_now() {
110     todo!();
111 }
112
113 pub fn async_generator() {
114     let mut state: u32 = 0;
115
116     let _ = async {
117         state = 1;
118         yield_now().await;
119         state = 2;
120         yield_now().await;
121         state = 3;
122     };
123
124     let _ = async move {
125         state = 4;  //~  WARN value assigned to `state` is never read
126                     //~| WARN unused variable: `state`
127         yield_now().await;
128         state = 5;  //~ WARN value assigned to `state` is never read
129     };
130 }
131
132 pub fn generator() {
133     let mut s: u32 = 0;
134     let _ = |_| {
135         s = 0;
136         yield ();
137         s = 1; //~ WARN value assigned to `s` is never read
138         yield (s = 2);
139         s = yield (); //~ WARN value assigned to `s` is never read
140         s = 3;
141     };
142 }
143
144 fn main() {}