summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/pattern_type_mismatch/pattern_structs.rs
blob: d9b22693f297f8ca6a96745ad15404e34220df0a (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
#![allow(clippy::all)]
#![warn(clippy::pattern_type_mismatch)]

fn main() {}

fn struct_types() {
    struct Struct<'a> {
        ref_inner: &'a Option<i32>,
    }
    let ref_value = &Struct { ref_inner: &Some(42) };

    // not ok
    let Struct { .. } = ref_value;
    //~^ ERROR: type of pattern does not match the expression type
    if let &Struct { ref_inner: Some(_) } = ref_value {}
    //~^ ERROR: type of pattern does not match the expression type
    if let Struct { ref_inner: Some(_) } = *ref_value {}
    //~^ ERROR: type of pattern does not match the expression type

    // ok
    let &Struct { .. } = ref_value;
    let Struct { .. } = *ref_value;
    if let &Struct { ref_inner: &Some(_) } = ref_value {}
    if let Struct { ref_inner: &Some(_) } = *ref_value {}
}

fn struct_enum_variants() {
    enum StructEnum<'a> {
        Empty,
        Var { inner_ref: &'a Option<i32> },
    }
    let ref_value = &StructEnum::Var { inner_ref: &Some(42) };

    // not ok
    if let StructEnum::Var { .. } = ref_value {}
    //~^ ERROR: type of pattern does not match the expression type
    if let StructEnum::Var { inner_ref: Some(_) } = ref_value {}
    //~^ ERROR: type of pattern does not match the expression type
    if let &StructEnum::Var { inner_ref: Some(_) } = ref_value {}
    //~^ ERROR: type of pattern does not match the expression type
    if let StructEnum::Var { inner_ref: Some(_) } = *ref_value {}
    //~^ ERROR: type of pattern does not match the expression type
    if let StructEnum::Empty = ref_value {}
    //~^ ERROR: type of pattern does not match the expression type

    // ok
    if let &StructEnum::Var { .. } = ref_value {}
    if let StructEnum::Var { .. } = *ref_value {}
    if let &StructEnum::Var { inner_ref: &Some(_) } = ref_value {}
    if let StructEnum::Var { inner_ref: &Some(_) } = *ref_value {}
    if let &StructEnum::Empty = ref_value {}
    if let StructEnum::Empty = *ref_value {}
}