summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/collapsible_match2.rs
blob: c8fb0a39e954cdeabf431e973c2cf19f830778b5 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#![warn(clippy::collapsible_match)]
#![allow(
    clippy::needless_return,
    clippy::no_effect,
    clippy::single_match,
    clippy::needless_borrow
)]

fn lint_cases(opt_opt: Option<Option<u32>>, res_opt: Result<Option<u32>, String>) {
    // if guards on outer match
    {
        match res_opt {
            Ok(val) if make() => match val {
                Some(n) => foo(n),
                _ => return,
            },
            _ => return,
        }
        match res_opt {
            Ok(val) => match val {
                Some(n) => foo(n),
                _ => return,
            },
            _ if make() => return,
            _ => return,
        }
    }

    // macro
    {
        macro_rules! mac {
            ($outer:expr => $pat:pat, $e:expr => $inner_pat:pat, $then:expr) => {
                match $outer {
                    $pat => match $e {
                        $inner_pat => $then,
                        _ => return,
                    },
                    _ => return,
                }
            };
        }
        // Lint this since the patterns are not defined by the macro.
        // Allows the lint to work on if_chain! for example.
        // Fixing the lint requires knowledge of the specific macro, but we optimistically assume that
        // there is still a better way to write this.
        mac!(res_opt => Ok(val), val => Some(n), foo(n));
    }

    // deref reference value
    match Some(&[1]) {
        Some(s) => match *s {
            [n] => foo(n),
            _ => (),
        },
        _ => (),
    }

    // ref pattern and deref
    match Some(&[1]) {
        Some(ref s) => match s {
            [n] => foo(n),
            _ => (),
        },
        _ => (),
    }
}

fn no_lint() {
    // deref inner value (cannot pattern match with Vec)
    match Some(vec![1]) {
        Some(s) => match *s {
            [n] => foo(n),
            _ => (),
        },
        _ => (),
    }
}

fn make<T>() -> T {
    unimplemented!()
}

fn foo<T, U>(t: T) -> U {
    unimplemented!()
}

fn main() {}