]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/match_single_binding.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / match_single_binding.rs
CommitLineData
f20569fa
XL
1// run-rustfix
2
3#![warn(clippy::match_single_binding)]
4#![allow(unused_variables, clippy::many_single_char_names, clippy::toplevel_ref_arg)]
5
6struct Point {
7 x: i32,
8 y: i32,
9}
10
11fn coords() -> Point {
12 Point { x: 1, y: 2 }
13}
14
15macro_rules! foo {
16 ($param:expr) => {
17 match $param {
18 _ => println!("whatever"),
19 }
20 };
21}
22
23fn 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 // Lint
110 let x = 1;
111 let y = 1;
112 match match y {
113 0 => 1,
114 _ => 2,
115 } {
116 _ => println!("Single branch"),
117 }
118 // Ok
119 let x = 1;
120 let y = 1;
121 match match y {
122 0 => 1,
123 _ => 2,
124 } {
125 #[cfg(disabled_feature)]
126 0 => println!("Array index start"),
127 _ => println!("Not an array index start"),
128 }
129 // False negative
130 let x = 1;
131 match x {
132 // =>
133 _ => println!("Not an array index start"),
134 }
135}