summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/infallible_destructuring_match.rs
blob: f2768245bbc417d8fa89944f38f0f3a3b0cd4459 (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
// run-rustfix
#![feature(exhaustive_patterns, never_type)]
#![allow(dead_code, unreachable_code, unused_variables)]
#![allow(clippy::let_and_return)]

enum SingleVariantEnum {
    Variant(i32),
}

struct TupleStruct(i32);

struct NonCopy;
struct TupleStructWithNonCopy(NonCopy);

enum EmptyEnum {}

macro_rules! match_enum {
    ($param:expr) => {
        let data = match $param {
            SingleVariantEnum::Variant(i) => i,
        };
    };
}

fn infallible_destructuring_match_enum() {
    let wrapper = SingleVariantEnum::Variant(0);

    // This should lint!
    let data = match wrapper {
        SingleVariantEnum::Variant(i) => i,
    };

    // This shouldn't (inside macro)
    match_enum!(wrapper);

    // This shouldn't!
    let data = match wrapper {
        SingleVariantEnum::Variant(_) => -1,
    };

    // Neither should this!
    let data = match wrapper {
        SingleVariantEnum::Variant(i) => -1,
    };

    let SingleVariantEnum::Variant(data) = wrapper;
}

macro_rules! match_struct {
    ($param:expr) => {
        let data = match $param {
            TupleStruct(i) => i,
        };
    };
}

fn infallible_destructuring_match_struct() {
    let wrapper = TupleStruct(0);

    // This should lint!
    let data = match wrapper {
        TupleStruct(i) => i,
    };

    // This shouldn't (inside macro)
    match_struct!(wrapper);

    // This shouldn't!
    let data = match wrapper {
        TupleStruct(_) => -1,
    };

    // Neither should this!
    let data = match wrapper {
        TupleStruct(i) => -1,
    };

    let TupleStruct(data) = wrapper;
}

fn infallible_destructuring_match_struct_with_noncopy() {
    let wrapper = TupleStructWithNonCopy(NonCopy);

    // This should lint! (keeping `ref` in the suggestion)
    let data = match wrapper {
        TupleStructWithNonCopy(ref n) => n,
    };

    let TupleStructWithNonCopy(ref data) = wrapper;
}

macro_rules! match_never_enum {
    ($param:expr) => {
        let data = match $param {
            Ok(i) => i,
        };
    };
}

fn never_enum() {
    let wrapper: Result<i32, !> = Ok(23);

    // This should lint!
    let data = match wrapper {
        Ok(i) => i,
    };

    // This shouldn't (inside macro)
    match_never_enum!(wrapper);

    // This shouldn't!
    let data = match wrapper {
        Ok(_) => -1,
    };

    // Neither should this!
    let data = match wrapper {
        Ok(i) => -1,
    };

    let Ok(data) = wrapper;
}

impl EmptyEnum {
    fn match_on(&self) -> ! {
        // The lint shouldn't pick this up, as `let` won't work here!
        let data = match *self {};
        data
    }
}

fn main() {}