]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/match_single_binding.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / match_single_binding.rs
1 #![warn(clippy::match_single_binding)]
2 #![allow(
3 unused,
4 clippy::let_unit_value,
5 clippy::no_effect,
6 clippy::toplevel_ref_arg,
7 clippy::uninlined_format_args,
8 clippy::useless_vec
9 )]
10
11 struct Point {
12 x: i32,
13 y: i32,
14 }
15
16 fn coords() -> Point {
17 Point { x: 1, y: 2 }
18 }
19
20 macro_rules! foo {
21 ($param:expr) => {
22 match $param {
23 _ => println!("whatever"),
24 }
25 };
26 }
27
28 fn main() {
29 let a = 1;
30 let b = 2;
31 let c = 3;
32 // Lint
33 match (a, b, c) {
34 (x, y, z) => {
35 println!("{} {} {}", x, y, z);
36 },
37 }
38 // Lint
39 match (a, b, c) {
40 (x, y, z) => println!("{} {} {}", x, y, z),
41 }
42 // Ok
43 foo!(a);
44 // Ok
45 match a {
46 2 => println!("2"),
47 _ => println!("Not 2"),
48 }
49 // Ok
50 let d = Some(5);
51 match d {
52 Some(d) => println!("{}", d),
53 _ => println!("None"),
54 }
55 // Lint
56 match a {
57 _ => println!("whatever"),
58 }
59 // Lint
60 match a {
61 _ => {
62 let x = 29;
63 println!("x has a value of {}", x);
64 },
65 }
66 // Lint
67 match a {
68 _ => {
69 let e = 5 * a;
70 if e >= 5 {
71 println!("e is superior to 5");
72 }
73 },
74 }
75 // Lint
76 let p = Point { x: 0, y: 7 };
77 match p {
78 Point { x, y } => println!("Coords: ({}, {})", x, y),
79 }
80 // Lint
81 match p {
82 Point { x: x1, y: y1 } => println!("Coords: ({}, {})", x1, y1),
83 }
84 // Lint
85 let x = 5;
86 match x {
87 ref r => println!("Got a reference to {}", r),
88 }
89 // Lint
90 let mut x = 5;
91 match x {
92 ref mut mr => println!("Got a mutable reference to {}", mr),
93 }
94 // Lint
95 let product = match coords() {
96 Point { x, y } => x * y,
97 };
98 // Lint
99 let v = vec![Some(1), Some(2), Some(3), Some(4)];
100 #[allow(clippy::let_and_return)]
101 let _ = v
102 .iter()
103 .map(|i| match i.unwrap() {
104 unwrapped => unwrapped,
105 })
106 .collect::<Vec<u8>>();
107 // Ok
108 let x = 1;
109 match x {
110 #[cfg(disabled_feature)]
111 0 => println!("Disabled branch"),
112 _ => println!("Enabled branch"),
113 }
114
115 // Ok
116 let x = 1;
117 let y = 1;
118 match match y {
119 0 => 1,
120 _ => 2,
121 } {
122 #[cfg(disabled_feature)]
123 0 => println!("Array index start"),
124 _ => println!("Not an array index start"),
125 }
126
127 // Lint
128 let x = 1;
129 match x {
130 // =>
131 _ => println!("Not an array index start"),
132 }
133 }
134
135 fn issue_8723() {
136 let (mut val, idx) = ("a b", 1);
137
138 val = match val.split_at(idx) {
139 (pre, suf) => {
140 println!("{}", pre);
141 suf
142 },
143 };
144
145 let _ = val;
146 }
147
148 fn side_effects() {}
149
150 fn issue_9575() {
151 let _ = || match side_effects() {
152 _ => println!("Needs curlies"),
153 };
154 }
155
156 fn issue_9725(r: Option<u32>) {
157 match r {
158 x => match x {
159 Some(_) => {
160 println!("Some");
161 },
162 None => {
163 println!("None");
164 },
165 },
166 };
167 }
168
169 fn issue_10447() -> usize {
170 match 1 {
171 _ => (),
172 }
173
174 let a = match 1 {
175 _ => (),
176 };
177
178 match 1 {
179 _ => side_effects(),
180 }
181
182 let b = match 1 {
183 _ => side_effects(),
184 };
185
186 match 1 {
187 _ => println!("1"),
188 }
189
190 let c = match 1 {
191 _ => println!("1"),
192 };
193
194 let in_expr = [
195 match 1 {
196 _ => (),
197 },
198 match 1 {
199 _ => side_effects(),
200 },
201 match 1 {
202 _ => println!("1"),
203 },
204 ];
205
206 2
207 }