]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/match_single_binding.rs
Auto merge of #89219 - nickkuk:str_split_once_get_unchecked, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / tests / ui / match_single_binding.rs
1 // run-rustfix
2
3 #![warn(clippy::match_single_binding)]
4 #![allow(unused_variables, clippy::toplevel_ref_arg)]
5
6 struct Point {
7     x: i32,
8     y: i32,
9 }
10
11 fn coords() -> Point {
12     Point { x: 1, y: 2 }
13 }
14
15 macro_rules! foo {
16     ($param:expr) => {
17         match $param {
18             _ => println!("whatever"),
19         }
20     };
21 }
22
23 fn main() {
24     let a = 1;
25     let b = 2;
26     let c = 3;
27     // Lint
28     match (a, b, c) {
29         (x, y, z) => {
30             println!("{} {} {}", x, y, z);
31         },
32     }
33     // Lint
34     match (a, b, c) {
35         (x, y, z) => println!("{} {} {}", x, y, z),
36     }
37     // Ok
38     foo!(a);
39     // Ok
40     match a {
41         2 => println!("2"),
42         _ => println!("Not 2"),
43     }
44     // Ok
45     let d = Some(5);
46     match d {
47         Some(d) => println!("{}", d),
48         _ => println!("None"),
49     }
50     // Lint
51     match a {
52         _ => println!("whatever"),
53     }
54     // Lint
55     match a {
56         _ => {
57             let x = 29;
58             println!("x has a value of {}", x);
59         },
60     }
61     // Lint
62     match a {
63         _ => {
64             let e = 5 * a;
65             if e >= 5 {
66                 println!("e is superior to 5");
67             }
68         },
69     }
70     // Lint
71     let p = Point { x: 0, y: 7 };
72     match p {
73         Point { x, y } => println!("Coords: ({}, {})", x, y),
74     }
75     // Lint
76     match p {
77         Point { x: x1, y: y1 } => println!("Coords: ({}, {})", x1, y1),
78     }
79     // Lint
80     let x = 5;
81     match x {
82         ref r => println!("Got a reference to {}", r),
83     }
84     // Lint
85     let mut x = 5;
86     match x {
87         ref mut mr => println!("Got a mutable reference to {}", mr),
88     }
89     // Lint
90     let product = match coords() {
91         Point { x, y } => x * y,
92     };
93     // Lint
94     let v = vec![Some(1), Some(2), Some(3), Some(4)];
95     #[allow(clippy::let_and_return)]
96     let _ = v
97         .iter()
98         .map(|i| match i.unwrap() {
99             unwrapped => unwrapped,
100         })
101         .collect::<Vec<u8>>();
102     // Ok
103     let x = 1;
104     match x {
105         #[cfg(disabled_feature)]
106         0 => println!("Disabled branch"),
107         _ => println!("Enabled branch"),
108     }
109
110     // Ok
111     let x = 1;
112     let y = 1;
113     match match y {
114         0 => 1,
115         _ => 2,
116     } {
117         #[cfg(disabled_feature)]
118         0 => println!("Array index start"),
119         _ => println!("Not an array index start"),
120     }
121     // False negative
122     let x = 1;
123     match x {
124         // =>
125         _ => println!("Not an array index start"),
126     }
127 }