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