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