]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/match-guards-always-borrow.rs
Rollup merge of #105216 - GuillaumeGomez:rm-unused-gui-test, r=notriddle
[rust.git] / src / test / ui / nll / match-guards-always-borrow.rs
1 // Here is arielb1's basic example from rust-lang/rust#27282
2 // that AST borrowck is flummoxed by:
3
4 fn should_reject_destructive_mutate_in_guard() {
5     match Some(&4) {
6         None => {},
7         ref mut foo if {
8             (|| { let bar = foo; bar.take() })();
9             //~^ ERROR cannot move out of `foo` in pattern guard [E0507]
10             false } => { },
11         Some(s) => std::process::exit(*s),
12     }
13 }
14
15 // Here below is a case that needs to keep working: we only use the
16 // binding via immutable-borrow in the guard, and we mutate in the arm
17 // body.
18 fn allow_mutate_in_arm_body() {
19     match Some(&4) {
20         None => {},
21         ref mut foo if foo.is_some() && false => { foo.take(); () }
22         Some(s) => std::process::exit(*s),
23     }
24 }
25
26 // Here below is a case that needs to keep working: we only use the
27 // binding via immutable-borrow in the guard, and we move into the arm
28 // body.
29 fn allow_move_into_arm_body() {
30     match Some(&4) {
31         None => {},
32         mut foo if foo.is_some() && false => { foo.take(); () }
33         Some(s) => std::process::exit(*s),
34     }
35 }
36
37 fn main() {
38     should_reject_destructive_mutate_in_guard();
39     allow_mutate_in_arm_body();
40     allow_move_into_arm_body();
41 }