]> git.lizzy.rs Git - rust.git/blob - tests/ui/single_match.rs
Merge pull request #3336 from HMPerson1/clone_on_copy_deref
[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 x = Some(1u8);
27     match x {
28         // Note the missing block braces.
29         // We suggest `if let Some(y) = x { .. }` because the macro
30         // is expanded before we can do anything.
31         Some(y) => println!("{:?}", y),
32         _ => ()
33     }
34
35     let z = (1u8,1u8);
36     match z {
37         (2...3, 7...9) => dummy(),
38         _ => {}
39     };
40
41     // Not linted (pattern guards used)
42     match x {
43         Some(y) if y == 0 => println!("{:?}", y),
44         _ => ()
45     }
46
47     // Not linted (no block with statements in the single arm)
48     match z {
49         (2...3, 7...9) => println!("{:?}", z),
50         _ => println!("nope"),
51     }
52 }
53
54 enum Foo { Bar, Baz(u8) }
55 use Foo::*;
56 use std::borrow::Cow;
57
58 fn single_match_know_enum() {
59     let x = Some(1u8);
60     let y : Result<_, i8> = Ok(1i8);
61
62     match x {
63         Some(y) => dummy(),
64         None => ()
65     };
66
67     match y {
68         Ok(y) => dummy(),
69         Err(..) => ()
70     };
71
72     let c = Cow::Borrowed("");
73
74     match c {
75         Cow::Borrowed(..) => dummy(),
76         Cow::Owned(..) => (),
77     };
78
79     let z = Foo::Bar;
80     // no warning
81     match z {
82         Bar => println!("42"),
83         Baz(_) => (),
84     }
85
86     match z {
87         Baz(_) => println!("42"),
88         Bar => (),
89     }
90 }
91
92 fn main() { }