aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/diagnostics/pattern.rs
blob: 044506d66989e68438c037fd889062323e4419fa (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
147
148
149
150
#![deny(elided_lifetimes_in_paths)]
#![allow(unused)] // todo remove

mod deconstruct_pat;
pub mod usefulness;

#[cfg(test)]
mod tests {
    use crate::diagnostics::tests::check_diagnostics;

    use super::*;

    #[test]
    fn unit() {
        check_diagnostics(
            r#"
fn main() {
    match () { () => {} }
    match () {  _ => {} }
    match () {          }
        //^^ Missing match arm
}
"#,
        );
    }

    #[test]
    fn tuple_of_units() {
        check_diagnostics(
            r#"
fn main() {
    match ((), ()) { ((), ()) => {} }
    match ((), ()) {  ((), _) => {} }
    match ((), ()) {   (_, _) => {} }
    match ((), ()) {        _ => {} }
    match ((), ()) {                }
        //^^^^^^^^ Missing match arm
}
"#,
        );
    }

    #[test]
    fn tuple_with_ellipsis() {
        // TODO: test non-exhaustive match with ellipsis in the middle
        // of a pattern, check reported witness
        check_diagnostics(
            r#"
struct A; struct B;
fn main(v: (A, (), B)) {
    match v { (A, ..)    => {} }
    match v { (.., B)    => {} }
    match v { (A, .., B) => {} }
    match v { (..)       => {} }
    match v {                  }
        //^ Missing match arm
}
"#,
        );
    }

    #[test]
    fn strukt() {
        check_diagnostics(
            r#"
struct A; struct B;
struct S { a: A, b: B}
fn main(v: S) {
    match v { S { a, b }       => {} }
    match v { S { a: _, b: _ } => {} }
    match v { S { .. }         => {} }
    match v { _                => {} }
    match v {                        }
        //^ Missing match arm
}
"#,
        );
    }

    #[test]
    fn c_enum() {
        check_diagnostics(
            r#"
enum E { A, B }
fn main(v: E) {
    match v { E::A | E::B => {} }
    match v { _           => {} }
    match v { E::A        => {} }
        //^ Missing match arm
    match v {                   }
        //^ Missing match arm
}
"#,
        );
    }

    #[test]
    fn enum_() {
        check_diagnostics(
            r#"
struct A; struct B;
enum E { Tuple(A, B), Struct{ a: A, b: B } }
fn main(v: E) {
    match v {
        E::Tuple(a, b)    => {}
        E::Struct{ a, b } => {}
    }
    match v {
        E::Tuple(_, _) => {}
        E::Struct{..}  => {}
    }
    match v {
        E::Tuple(..) => {}
        _ => {}
    }
    match v { E::Tuple(..) => {} }
        //^ Missing match arm
    match v { }
        //^ Missing match arm
}
"#,
        );
    }

    #[test]
    fn boolean() {
        check_diagnostics(
            r#"
fn main() {
    match true {
        true  => {}
        false => {}
    }
    match true {
        true | false => {}
    }
    match true {
        true => {}
        _ => {}
    }
    match true {}
        //^^^^ Missing match arm
    match true { true => {} }
        //^^^^ Missing match arm

}
"#,
        );
    }
}