1 #![warn(clippy::match_same_arms)]
3 clippy::disallowed_names,
4 clippy::diverging_sub_expression,
5 clippy::uninlined_format_args
13 fn match_same_arms() {
17 let mut a = 42 + [23].len() as i32;
25 //~ ERROR match arms have same body
27 let mut a = 42 + [23].len() as i32;
38 51 => foo(), //~ ERROR match arms have same body
42 let _ = match Some(42) {
44 None => 24, //~ ERROR match arms have same body
47 let _ = match Some(42) {
52 let _ = match Some(42) {
54 Some(a) => 24, // bindings are different
58 let _ = match Some(42) {
59 Some(a) if a > 0 => 24,
60 Some(a) => 24, // one arm has a guard
64 match (Some(42), Some(42)) {
65 (Some(a), None) => bar(a),
66 (None, Some(a)) => bar(a), //~ ERROR match arms have same body
70 match (Some(42), Some(42)) {
71 (Some(a), ..) => bar(a),
72 (.., Some(a)) => bar(a), //~ ERROR match arms have same body
76 let _ = match Some(()) {
81 match (Some(42), Some("")) {
82 (Some(a), None) => bar(a),
83 (None, Some(a)) => bar(a), // bindings have different types
87 let x: Result<i32, &str> = Ok(3);
89 // No warning because of the guard.
91 Ok(x) if x * x == 64 => println!("ok"),
92 Ok(_) => println!("ok"),
93 Err(_) => println!("err"),
96 // This used to be a false positive; see issue #1996.
98 Ok(3) => println!("ok"),
99 Ok(x) if x * x == 64 => println!("ok 64"),
100 Ok(_) => println!("ok"),
101 Err(_) => println!("err"),
104 match (x, Some(1i32)) {
105 (Ok(x), Some(_)) => println!("ok {}", x),
106 (Ok(_), Some(x)) => println!("ok {}", x),
107 _ => println!("err"),
110 // No warning; different types for `x`.
111 match (x, Some(1.0f64)) {
112 (Ok(x), Some(_)) => println!("ok {}", x),
113 (Ok(_), Some(x)) => println!("ok {}", x),
114 _ => println!("err"),
117 // False negative #2251.
119 Ok(_tmp) => println!("ok"),
120 Ok(3) => println!("ok"),
121 Ok(_) => println!("ok"),
127 // False positive #1390
143 // still lint if the tokens are the same
156 match_expr_like_matches_macro_priority();
159 fn match_expr_like_matches_macro_priority() {
174 let _ = match Some(0) {
177 #[cfg(feature = "foo")]
188 // Don't lint. `Foo::X(0)` and `Foo::Z(_)` overlap with the arm in between.
189 let _ = match Foo::X(0) {
191 Foo::X(_) | Foo::Y(_) | Foo::Z(0) => 2,
196 // Suggest moving `Foo::Z(_)` up.
197 let _ = match Foo::X(0) {
199 Foo::X(_) | Foo::Y(_) => 2,
204 // Suggest moving `Foo::X(0)` down.
205 let _ = match Foo::X(0) {
207 Foo::Y(_) | Foo::Z(0) => 2,
228 Some(Bar { x: 0, y: 5, .. }) => 1,
229 Some(Bar { y: 10, z: 0, .. }) => 2,
231 Some(Bar { y: 0, x: 5, .. }) => 1,
238 2 => core::convert::identity::<u32>(todo!()),
239 3 => core::convert::identity::<u32>(todo!()),