]> git.lizzy.rs Git - rust.git/blob - tests/ui/single_match.rs
5c7cae249b4f26c79b16c4b3f2651178360e96b9
[rust.git] / tests / ui / single_match.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11
12
13 #![warn(clippy::single_match)]
14
15 fn dummy() {
16 }
17
18 fn single_match(){
19     let x = Some(1u8);
20
21     match x {
22         Some(y) => { println!("{:?}", y); }
23         _ => ()
24     };
25
26     let z = (1u8,1u8);
27     match z {
28         (2...3, 7...9) => dummy(),
29         _ => {}
30     };
31
32     // Not linted (pattern guards used)
33     match x {
34         Some(y) if y == 0 => println!("{:?}", y),
35         _ => ()
36     }
37
38     // Not linted (no block with statements in the single arm)
39     match z {
40         (2...3, 7...9) => println!("{:?}", z),
41         _ => println!("nope"),
42     }
43 }
44
45 enum Foo { Bar, Baz(u8) }
46 use Foo::*;
47 use std::borrow::Cow;
48
49 fn single_match_know_enum() {
50     let x = Some(1u8);
51     let y : Result<_, i8> = Ok(1i8);
52
53     match x {
54         Some(y) => dummy(),
55         None => ()
56     };
57
58     match y {
59         Ok(y) => dummy(),
60         Err(..) => ()
61     };
62
63     let c = Cow::Borrowed("");
64
65     match c {
66         Cow::Borrowed(..) => dummy(),
67         Cow::Owned(..) => (),
68     };
69
70     let z = Foo::Bar;
71     // no warning
72     match z {
73         Bar => println!("42"),
74         Baz(_) => (),
75     }
76
77     match z {
78         Baz(_) => println!("42"),
79         Bar => (),
80     }
81 }
82
83 fn main() { }