summaryrefslogtreecommitdiffstats
path: root/tests/ui/nll/match-guards-always-borrow.rs
blob: ff63cc092734a0211af6825597610f4bd921c42c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#![feature(if_let_guard)]

// Here is arielb1's basic example from rust-lang/rust#27282
// that AST borrowck is flummoxed by:

fn should_reject_destructive_mutate_in_guard() {
    match Some(&4) {
        None => {},
        ref mut foo if {
            (|| { let bar = foo; bar.take() })();
            //~^ ERROR cannot move out of `foo` in pattern guard [E0507]
            false } => { },
        Some(s) => std::process::exit(*s),
    }

    match Some(&4) {
        None => {},
        ref mut foo if let Some(()) = {
            (|| { let bar = foo; bar.take() })();
            //~^ ERROR cannot move out of `foo` in pattern guard [E0507]
            None } => { },
        Some(s) => std::process::exit(*s),
    }
}

// Here below is a case that needs to keep working: we only use the
// binding via immutable-borrow in the guard, and we mutate in the arm
// body.
fn allow_mutate_in_arm_body() {
    match Some(&4) {
        None => {},
        ref mut foo if foo.is_some() => { foo.take(); () }
        Some(s) => std::process::exit(*s),
    }

    match Some(&4) {
        None => {},
        ref mut foo if let Some(_) = foo => { foo.take(); () }
        Some(s) => std::process::exit(*s),
    }
}

// Here below is a case that needs to keep working: we only use the
// binding via immutable-borrow in the guard, and we move into the arm
// body.
fn allow_move_into_arm_body() {
    match Some(&4) {
        None => {},
        mut foo if foo.is_some() => { foo.unwrap(); () }
        Some(s) => std::process::exit(*s),
    }

    match Some(&4) {
        None => {},
        mut foo if let Some(_) = foo => { foo.unwrap(); () }
        Some(s) => std::process::exit(*s),
    }
}

fn main() {
    should_reject_destructive_mutate_in_guard();
    allow_mutate_in_arm_body();
    allow_move_into_arm_body();
}