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

fn main() {}

fn tuple_types() {
    struct TupleStruct<'a>(&'a Option<i32>);
    let ref_value = &TupleStruct(&Some(42));

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

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

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

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

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

fn plain_tuples() {
    let ref_value = &(&Some(23), &Some(42));

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

    // ok
    let &(_a, _b) = ref_value;
    let (_a, _b) = *ref_value;
    if let &(_a, &Some(_)) = ref_value {}
    if let (_a, &Some(_)) = *ref_value {}
}