summaryrefslogtreecommitdiffstats
path: root/tests/ui/nll/enum-drop-access.rs
blob: 5ef0c3fe73dbf8e46376aec4e594b5d1e127aa17 (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
enum DropOption<T> {
    Some(T),
    None,
}

impl<T> Drop for DropOption<T> {
    fn drop(&mut self) {}
}

// Dropping opt could access the value behind the reference,
fn drop_enum(opt: DropOption<&mut i32>) -> Option<&mut i32> {
    match opt {
        DropOption::Some(&mut ref mut r) => { //~ ERROR
            Some(r)
        },
        DropOption::None => None,
    }
}

fn optional_drop_enum(opt: Option<DropOption<&mut i32>>) -> Option<&mut i32> {
    match opt {
        Some(DropOption::Some(&mut ref mut r)) => { //~ ERROR
            Some(r)
        },
        Some(DropOption::None) | None => None,
    }
}

// Ok, dropping opt doesn't access the reference
fn optional_tuple(opt: Option<(&mut i32, String)>) -> Option<&mut i32> {
    match opt {
        Some((&mut ref mut r, _)) => {
            Some(r)
        },
        None => None,
    }
}

// Ok, dropping res doesn't access the Ok case.
fn different_variants(res: Result<&mut i32, String>) -> Option<&mut i32> {
    match res {
        Ok(&mut ref mut r) => {
            Some(r)
        },
        Err(_) => None,
    }
}

fn main() {}