summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/pattern_type_mismatch/syntax.rs
blob: e89917c41e8c1a72a50451d1b628a0dfaef4fe78 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#![allow(clippy::all)]
#![warn(clippy::pattern_type_mismatch)]

fn main() {}

fn syntax_match() {
    let ref_value = &Some(&Some(42));

    // not ok
    match ref_value {
        Some(_) => (),
        None => (),
    }

    // ok
    match ref_value {
        &Some(_) => (),
        &None => (),
    }
    match *ref_value {
        Some(_) => (),
        None => (),
    }
}

fn syntax_if_let() {
    let ref_value = &Some(42);

    // not ok
    if let Some(_) = ref_value {}

    // ok
    if let &Some(_) = ref_value {}
    if let Some(_) = *ref_value {}
}

fn syntax_while_let() {
    let ref_value = &Some(42);

    // not ok
    while let Some(_) = ref_value {
        break;
    }

    // ok
    while let &Some(_) = ref_value {
        break;
    }
    while let Some(_) = *ref_value {
        break;
    }
}

fn syntax_for() {
    let ref_value = &Some(23);
    let slice = &[(2, 3), (4, 2)];

    // not ok
    for (_a, _b) in slice.iter() {}

    // ok
    for &(_a, _b) in slice.iter() {}
}

fn syntax_let() {
    let ref_value = &(2, 3);

    // not ok
    let (_n, _m) = ref_value;

    // ok
    let &(_n, _m) = ref_value;
    let (_n, _m) = *ref_value;
}

fn syntax_fn() {
    // not ok
    fn foo((_a, _b): &(i32, i32)) {}

    // ok
    fn foo_ok_1(&(_a, _b): &(i32, i32)) {}
}

fn syntax_closure() {
    fn foo<F>(f: F)
    where
        F: FnOnce(&(i32, i32)),
    {
    }

    // not ok
    foo(|(_a, _b)| ());

    // ok
    foo(|&(_a, _b)| ());
}

fn macro_with_expression() {
    macro_rules! matching_macro {
        ($e:expr) => {
            $e
        };
    }
    let value = &Some(23);

    // not ok
    matching_macro!(match value {
        Some(_) => (),
        _ => (),
    });

    // ok
    matching_macro!(match value {
        &Some(_) => (),
        _ => (),
    });
    matching_macro!(match *value {
        Some(_) => (),
        _ => (),
    });
}

fn macro_expansion() {
    macro_rules! matching_macro {
        ($e:expr) => {
            // not ok
            match $e {
                Some(_) => (),
                _ => (),
            }

            // ok
            match $e {
                &Some(_) => (),
                _ => (),
            }
            match *$e {
                Some(_) => (),
                _ => (),
            }
        };
    }

    let value = &Some(23);
    matching_macro!(value);
}