]> git.lizzy.rs Git - rust.git/blob - src/test/ui/unboxed-closures/unboxed-closures-counter-not-moved.rs
Rollup merge of #86828 - lambinoo:67441-const-fn-copied-take-replace, r=joshtriplett
[rust.git] / src / test / ui / unboxed-closures / unboxed-closures-counter-not-moved.rs
1 // run-pass
2 // Test that we mutate a counter on the stack only when we expect to.
3
4 fn call<F>(f: F) where F : FnOnce() {
5     f();
6 }
7
8 fn main() {
9     let y = vec![format!("Hello"), format!("World")];
10     let mut counter = 22_u32;
11
12     call(|| {
13         // Move `y`, but do not move `counter`, even though it is read
14         // by value (note that it is also mutated).
15         for item in y { //~ WARN unused variable: `item`
16             let v = counter;
17             counter += v;
18         }
19     });
20     assert_eq!(counter, 88);
21
22     call(move || {
23         // this mutates a moved copy, and hence doesn't affect original
24         counter += 1; //~  WARN value assigned to `counter` is never read
25                       //~| WARN unused variable: `counter`
26     });
27     assert_eq!(counter, 88);
28 }