]> git.lizzy.rs Git - rust.git/blob - tests/ui/match_same_arms2.rs
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[rust.git] / tests / ui / match_same_arms2.rs
1 #![warn(clippy::match_same_arms)]
2 #![allow(clippy::blacklisted_name)]
3
4 fn bar<T>(_: T) {}
5 fn foo() -> bool {
6     unimplemented!()
7 }
8
9 fn match_same_arms() {
10     let _ = match 42 {
11         42 => {
12             foo();
13             let mut a = 42 + [23].len() as i32;
14             if true {
15                 a += 7;
16             }
17             a = -31 - a;
18             a
19         },
20         _ => {
21             //~ ERROR match arms have same body
22             foo();
23             let mut a = 42 + [23].len() as i32;
24             if true {
25                 a += 7;
26             }
27             a = -31 - a;
28             a
29         },
30     };
31
32     let _ = match 42 {
33         42 => foo(),
34         51 => foo(), //~ ERROR match arms have same body
35         _ => true,
36     };
37
38     let _ = match Some(42) {
39         Some(_) => 24,
40         None => 24, //~ ERROR match arms have same body
41     };
42
43     let _ = match Some(42) {
44         Some(foo) => 24,
45         None => 24,
46     };
47
48     let _ = match Some(42) {
49         Some(42) => 24,
50         Some(a) => 24, // bindings are different
51         None => 0,
52     };
53
54     let _ = match Some(42) {
55         Some(a) if a > 0 => 24,
56         Some(a) => 24, // one arm has a guard
57         None => 0,
58     };
59
60     match (Some(42), Some(42)) {
61         (Some(a), None) => bar(a),
62         (None, Some(a)) => bar(a), //~ ERROR match arms have same body
63         _ => (),
64     }
65
66     match (Some(42), Some(42)) {
67         (Some(a), ..) => bar(a),
68         (.., Some(a)) => bar(a), //~ ERROR match arms have same body
69         _ => (),
70     }
71
72     let _ = match Some(()) {
73         Some(()) => 0.0,
74         None => -0.0,
75     };
76
77     match (Some(42), Some("")) {
78         (Some(a), None) => bar(a),
79         (None, Some(a)) => bar(a), // bindings have different types
80         _ => (),
81     }
82
83     let x: Result<i32, &str> = Ok(3);
84
85     // No warning because of the guard.
86     match x {
87         Ok(x) if x * x == 64 => println!("ok"),
88         Ok(_) => println!("ok"),
89         Err(_) => println!("err"),
90     }
91
92     // This used to be a false positive; see issue #1996.
93     match x {
94         Ok(3) => println!("ok"),
95         Ok(x) if x * x == 64 => println!("ok 64"),
96         Ok(_) => println!("ok"),
97         Err(_) => println!("err"),
98     }
99
100     match (x, Some(1i32)) {
101         (Ok(x), Some(_)) => println!("ok {}", x),
102         (Ok(_), Some(x)) => println!("ok {}", x),
103         _ => println!("err"),
104     }
105
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"),
111     }
112
113     // False negative #2251.
114     match x {
115         Ok(_tmp) => println!("ok"),
116         Ok(3) => println!("ok"),
117         Ok(_) => println!("ok"),
118         Err(_) => {
119             unreachable!();
120         },
121     }
122 }
123
124 fn main() {}