]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/matches.rs
Improve `matches` tests
[rust.git] / tests / compile-fail / matches.rs
1 #![feature(plugin)]
2
3 #![plugin(clippy)]
4 #![deny(clippy)]
5 #![allow(unused)]
6 #![deny(single_match_else)]
7
8 use std::borrow::Cow;
9
10 enum Foo { Bar, Baz(u8) }
11 use Foo::*;
12
13 enum ExprNode {
14     ExprAddrOf,
15     Butterflies,
16     Unicorns,
17 }
18
19 static NODE: ExprNode = ExprNode::Unicorns;
20
21 fn dummy() {
22 }
23
24 fn unwrap_addr() -> Option<&'static ExprNode> {
25     match ExprNode::Butterflies {
26         //~^ ERROR you seem to be trying to use match
27         //~| HELP try
28         //~| SUGGESTION if let ExprNode::ExprAddrOf = ExprNode::Butterflies { Some(&NODE) } else { let x = 5; None }
29         ExprNode::ExprAddrOf => Some(&NODE),
30         _ => { let x = 5; None },
31     }
32 }
33
34 fn single_match(){
35     let x = Some(1u8);
36
37     match x {
38         //~^ ERROR you seem to be trying to use match
39         //~| HELP try
40         //~| SUGGESTION if let Some(y) = x { println!("{:?}", y); };
41         Some(y) => { println!("{:?}", y); }
42         _ => ()
43     };
44
45     let z = (1u8,1u8);
46     match z {
47         //~^ ERROR you seem to be trying to use match
48         //~| HELP try
49         //~| SUGGESTION if let (2...3, 7...9) = z { dummy() };
50         (2...3, 7...9) => dummy(),
51         _ => {}
52     };
53
54     // Not linted (pattern guards used)
55     match x {
56         Some(y) if y == 0 => println!("{:?}", y),
57         _ => ()
58     }
59
60     // Not linted (no block with statements in the single arm)
61     match z {
62         (2...3, 7...9) => println!("{:?}", z),
63         _ => println!("nope"),
64     }
65 }
66
67 fn single_match_know_enum() {
68     let x = Some(1u8);
69     let y : Result<_, i8> = Ok(1i8);
70
71     match x {
72         //~^ ERROR you seem to be trying to use match
73         //~| HELP try
74         //~| SUGGESTION if let Some(y) = x { dummy() };
75         Some(y) => dummy(),
76         None => ()
77     };
78
79     match y {
80         //~^ ERROR you seem to be trying to use match
81         //~| HELP try
82         //~| SUGGESTION if let Ok(y) = y { dummy() };
83         Ok(y) => dummy(),
84         Err(..) => ()
85     };
86
87     let c = Cow::Borrowed("");
88
89     match c {
90         //~^ ERROR you seem to be trying to use match
91         //~| HELP try
92         //~| SUGGESTION if let Cow::Borrowed(..) = c { dummy() };
93         Cow::Borrowed(..) => dummy(),
94         Cow::Owned(..) => (),
95     };
96
97     let z = Foo::Bar;
98     // no warning
99     match z {
100         Bar => println!("42"),
101         Baz(_) => (),
102     }
103
104     match z {
105         Baz(_) => println!("42"),
106         Bar => (),
107     }
108 }
109
110 fn match_bool() {
111     let test: bool = true;
112
113     match test {
114     //~^ ERROR you seem to be trying to match on a boolean expression
115     //~| HELP try
116     //~| SUGGESTION if test { 0 } else { 42 };
117         true => 0,
118         false => 42,
119     };
120
121     let option = 1;
122     match option == 1 {
123     //~^ ERROR you seem to be trying to match on a boolean expression
124     //~| HELP try
125     //~| SUGGESTION if option == 1 { 1 } else { 0 };
126         true => 1,
127         false => 0,
128     };
129
130     match test {
131     //~^ ERROR you seem to be trying to match on a boolean expression
132     //~| HELP try
133     //~^^ SUGGESTION if !test { println!("Noooo!"); };
134         true => (),
135         false => { println!("Noooo!"); }
136     };
137
138     match test {
139     //~^ ERROR you seem to be trying to match on a boolean expression
140     //~| HELP try
141     //~^^ SUGGESTION if !test { println!("Noooo!"); };
142         false => { println!("Noooo!"); }
143         _ => (),
144     };
145
146     match test {
147     //~^ ERROR you seem to be trying to match on a boolean expression
148     //~| HELP try
149     //~| SUGGESTION if test { println!("Yes!"); } else { println!("Noooo!"); };
150         false => { println!("Noooo!"); }
151         true => { println!("Yes!"); }
152     };
153
154     // Not linted
155     match option {
156         1 ... 10 => 1,
157         11 ... 20 => 2,
158         _ => 3,
159     };
160 }
161
162 fn ref_pats() {
163     {
164         let v = &Some(0);
165         match v {
166             //~^ERROR add `&` to all patterns
167             //~|HELP instead of
168             //~|SUGGESTION match *v { .. }
169             &Some(v) => println!("{:?}", v),
170             &None => println!("none"),
171         }
172         match v {  // this doesn't trigger, we have a different pattern
173             &Some(v) => println!("some"),
174             other => println!("other"),
175         }
176     }
177     let tup =& (1, 2);
178     match tup {
179         //~^ERROR add `&` to all patterns
180         //~|HELP instead of
181         //~|SUGGESTION match *tup { .. }
182         &(v, 1) => println!("{}", v),
183         _ => println!("none"),
184     }
185     // special case: using & both in expr and pats
186     let w = Some(0);
187     match &w {
188         //~^ERROR add `&` to both
189         //~|HELP try
190         //~|SUGGESTION match w { .. }
191         &Some(v) => println!("{:?}", v),
192         &None => println!("none"),
193     }
194     // false positive: only wildcard pattern
195     let w = Some(0);
196     match w {
197         _ => println!("none"),
198     }
199
200     let a = &Some(0);
201     if let &None = a {
202         //~^ERROR add `&` to all patterns
203         //~|HELP instead of
204         //~|SUGGESTION if let .. = *a { .. }
205         println!("none");
206     }
207
208     let b = Some(0);
209     if let &None = &b {
210         //~^ERROR add `&` to both
211         //~|HELP try
212         //~|SUGGESTION if let .. = b { .. }
213         println!("none");
214     }
215 }
216
217 fn overlapping() {
218     const FOO : u64 = 2;
219
220     match 42 {
221         0 ... 10 => println!("0 ... 10"), //~ERROR: some ranges overlap
222         0 ... 11 => println!("0 ... 10"), //~NOTE overlaps with this
223         _ => (),
224     }
225
226     match 42 {
227         0 ... 5 => println!("0 ... 5"), //~ERROR: some ranges overlap
228         6 ... 7 => println!("6 ... 7"),
229         FOO ... 11 => println!("0 ... 10"), //~NOTE overlaps with this
230         _ => (),
231     }
232
233     match 42 {
234         2 => println!("2"), //~NOTE overlaps with this
235         0 ... 5 => println!("0 ... 5"), //~ERROR: some ranges overlap
236         _ => (),
237     }
238
239     match 42 {
240         0 ... 10 => println!("0 ... 10"),
241         11 ... 50 => println!("0 ... 10"),
242         _ => (),
243     }
244
245     if let None = Some(42) {
246         // nothing
247     } else if let None = Some(42) {
248         // another nothing :-)
249     }
250 }
251
252 fn main() {
253 }