1 #![warn(clippy::match_same_arms)]
2 #![allow(clippy::blacklisted_name)]
13 let mut a = 42 + [23].len() as i32;
21 //~ ERROR match arms have same body
23 let mut a = 42 + [23].len() as i32;
34 51 => foo(), //~ ERROR match arms have same body
38 let _ = match Some(42) {
40 None => 24, //~ ERROR match arms have same body
43 let _ = match Some(42) {
48 let _ = match Some(42) {
50 Some(a) => 24, // bindings are different
54 let _ = match Some(42) {
55 Some(a) if a > 0 => 24,
56 Some(a) => 24, // one arm has a guard
60 match (Some(42), Some(42)) {
61 (Some(a), None) => bar(a),
62 (None, Some(a)) => bar(a), //~ ERROR match arms have same body
66 match (Some(42), Some(42)) {
67 (Some(a), ..) => bar(a),
68 (.., Some(a)) => bar(a), //~ ERROR match arms have same body
72 let _ = match Some(()) {
77 match (Some(42), Some("")) {
78 (Some(a), None) => bar(a),
79 (None, Some(a)) => bar(a), // bindings have different types
83 let x: Result<i32, &str> = Ok(3);
85 // No warning because of the guard.
87 Ok(x) if x * x == 64 => println!("ok"),
88 Ok(_) => println!("ok"),
89 Err(_) => println!("err"),
92 // This used to be a false positive; see issue #1996.
94 Ok(3) => println!("ok"),
95 Ok(x) if x * x == 64 => println!("ok 64"),
96 Ok(_) => println!("ok"),
97 Err(_) => println!("err"),
100 match (x, Some(1i32)) {
101 (Ok(x), Some(_)) => println!("ok {}", x),
102 (Ok(_), Some(x)) => println!("ok {}", x),
103 _ => println!("err"),
106 // No warning; different types for `x`.
107 match (x, Some(1.0f64)) {
108 (Ok(x), Some(_)) => println!("ok {}", x),
109 (Ok(_), Some(x)) => println!("ok {}", x),
110 _ => println!("err"),
113 // False negative #2251.
115 Ok(_tmp) => println!("ok"),
116 Ok(3) => println!("ok"),
117 Ok(_) => println!("ok"),
123 // False positive #1390
139 // still lint if the tokens are the same
152 match_expr_like_matches_macro_priority();
155 fn match_expr_like_matches_macro_priority() {
170 let _ = match Some(0) {
173 #[cfg(feature = "foo")]
184 // Don't lint. `Foo::X(0)` and `Foo::Z(_)` overlap with the arm in between.
185 let _ = match Foo::X(0) {
187 Foo::X(_) | Foo::Y(_) | Foo::Z(0) => 2,
192 // Suggest moving `Foo::Z(_)` up.
193 let _ = match Foo::X(0) {
195 Foo::X(_) | Foo::Y(_) => 2,
200 // Suggest moving `Foo::X(0)` down.
201 let _ = match Foo::X(0) {
203 Foo::Y(_) | Foo::Z(0) => 2,
224 Some(Bar { x: 0, y: 5, .. }) => 1,
225 Some(Bar { y: 10, z: 0, .. }) => 2,
227 Some(Bar { y: 0, x: 5, .. }) => 1,