]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/matches.rs
e49aeaa6ec8622369055511284067dcad152c33d
[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 && test {
147     //~^ ERROR you seem to be trying to match on a boolean expression
148     //~| HELP try
149     //~| SUGGESTION if !(test && test) { println!("Noooo!"); };
150     //~| ERROR equal expressions as operands
151         false => { println!("Noooo!"); }
152         _ => (),
153     };
154
155     match test {
156     //~^ ERROR you seem to be trying to match on a boolean expression
157     //~| HELP try
158     //~| SUGGESTION if test { println!("Yes!"); } else { println!("Noooo!"); };
159         false => { println!("Noooo!"); }
160         true => { println!("Yes!"); }
161     };
162
163     // Not linted
164     match option {
165         1 ... 10 => 1,
166         11 ... 20 => 2,
167         _ => 3,
168     };
169 }
170
171 fn ref_pats() {
172     {
173         let v = &Some(0);
174         match v {
175             //~^ERROR add `&` to all patterns
176             //~|HELP instead of
177             //~|SUGGESTION match *v { .. }
178             &Some(v) => println!("{:?}", v),
179             &None => println!("none"),
180         }
181         match v {  // this doesn't trigger, we have a different pattern
182             &Some(v) => println!("some"),
183             other => println!("other"),
184         }
185     }
186     let tup =& (1, 2);
187     match tup {
188         //~^ERROR add `&` to all patterns
189         //~|HELP instead of
190         //~|SUGGESTION match *tup { .. }
191         &(v, 1) => println!("{}", v),
192         _ => println!("none"),
193     }
194     // special case: using & both in expr and pats
195     let w = Some(0);
196     match &w {
197         //~^ERROR add `&` to both
198         //~|HELP try
199         //~|SUGGESTION match w { .. }
200         &Some(v) => println!("{:?}", v),
201         &None => println!("none"),
202     }
203     // false positive: only wildcard pattern
204     let w = Some(0);
205     match w {
206         _ => println!("none"),
207     }
208
209     let a = &Some(0);
210     if let &None = a {
211         //~^ERROR add `&` to all patterns
212         //~|HELP instead of
213         //~|SUGGESTION if let .. = *a { .. }
214         println!("none");
215     }
216
217     let b = Some(0);
218     if let &None = &b {
219         //~^ERROR add `&` to both
220         //~|HELP try
221         //~|SUGGESTION if let .. = b { .. }
222         println!("none");
223     }
224 }
225
226 fn overlapping() {
227     const FOO : u64 = 2;
228
229     match 42 {
230         0 ... 10 => println!("0 ... 10"), //~ERROR: some ranges overlap
231         0 ... 11 => println!("0 ... 10"), //~NOTE overlaps with this
232         _ => (),
233     }
234
235     match 42 {
236         0 ... 5 => println!("0 ... 5"), //~ERROR: some ranges overlap
237         6 ... 7 => println!("6 ... 7"),
238         FOO ... 11 => println!("0 ... 10"), //~NOTE overlaps with this
239         _ => (),
240     }
241
242     match 42 {
243         2 => println!("2"), //~NOTE overlaps with this
244         0 ... 5 => println!("0 ... 5"), //~ERROR: some ranges overlap
245         _ => (),
246     }
247
248     match 42 {
249         0 ... 10 => println!("0 ... 10"),
250         11 ... 50 => println!("0 ... 10"),
251         _ => (),
252     }
253
254     if let None = Some(42) {
255         // nothing
256     } else if let None = Some(42) {
257         // another nothing :-)
258     }
259 }
260
261 fn main() {
262 }