]> git.lizzy.rs Git - rust.git/blob - tests/ui/match_same_arms.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / tests / ui / match_same_arms.rs
1 #![warn(clippy::match_same_arms)]
2 #![allow(
3     clippy::blacklisted_name,
4     clippy::collapsible_if,
5     clippy::cognitive_complexity,
6     clippy::eq_op,
7     clippy::needless_continue,
8     clippy::needless_return,
9     clippy::no_effect,
10     clippy::zero_divided_by_zero,
11     clippy::unused_unit
12 )]
13
14 fn bar<T>(_: T) {}
15 fn foo() -> bool {
16     unimplemented!()
17 }
18
19 pub enum Abc {
20     A,
21     B,
22     C,
23 }
24
25 #[allow(clippy::unused_unit)]
26 fn match_same_arms() {
27     let _ = match 42 {
28         42 => {
29             foo();
30             let mut a = 42 + [23].len() as i32;
31             if true {
32                 a += 7;
33             }
34             a = -31 - a;
35             a
36         },
37         _ => {
38             //~ ERROR match arms have same body
39             foo();
40             let mut a = 42 + [23].len() as i32;
41             if true {
42                 a += 7;
43             }
44             a = -31 - a;
45             a
46         },
47     };
48
49     let _ = match Abc::A {
50         Abc::A => 0,
51         Abc::B => 1,
52         _ => 0, //~ ERROR match arms have same body
53     };
54
55     let _ = match 42 {
56         42 => foo(),
57         51 => foo(), //~ ERROR match arms have same body
58         _ => true,
59     };
60
61     let _ = match Some(42) {
62         Some(_) => 24,
63         None => 24, //~ ERROR match arms have same body
64     };
65
66     let _ = match Some(42) {
67         Some(foo) => 24,
68         None => 24,
69     };
70
71     let _ = match Some(42) {
72         Some(42) => 24,
73         Some(a) => 24, // bindings are different
74         None => 0,
75     };
76
77     let _ = match Some(42) {
78         Some(a) if a > 0 => 24,
79         Some(a) => 24, // one arm has a guard
80         None => 0,
81     };
82
83     match (Some(42), Some(42)) {
84         (Some(a), None) => bar(a),
85         (None, Some(a)) => bar(a), //~ ERROR match arms have same body
86         _ => (),
87     }
88
89     match (Some(42), Some(42)) {
90         (Some(a), ..) => bar(a),
91         (.., Some(a)) => bar(a), //~ ERROR match arms have same body
92         _ => (),
93     }
94
95     match (1, 2, 3) {
96         (1, .., 3) => 42,
97         (.., 3) => 42, //~ ERROR match arms have same body
98         _ => 0,
99     };
100
101     let _ = match Some(()) {
102         Some(()) => 0.0,
103         None => -0.0,
104     };
105
106     match (Some(42), Some("")) {
107         (Some(a), None) => bar(a),
108         (None, Some(a)) => bar(a), // bindings have different types
109         _ => (),
110     }
111
112     let _ = match 42 {
113         42 => 1,
114         51 => 1, //~ ERROR match arms have same body
115         41 => 2,
116         52 => 2, //~ ERROR match arms have same body
117         _ => 0,
118     };
119
120     let _ = match 42 {
121         1 => 2,
122         2 => 2, //~ ERROR 2nd matched arms have same body
123         3 => 2, //~ ERROR 3rd matched arms have same body
124         4 => 3,
125         _ => 0,
126     };
127 }
128
129 mod issue4244 {
130     #[derive(PartialEq, PartialOrd, Eq, Ord)]
131     pub enum CommandInfo {
132         BuiltIn { name: String, about: Option<String> },
133         External { name: String, path: std::path::PathBuf },
134     }
135
136     impl CommandInfo {
137         pub fn name(&self) -> String {
138             match self {
139                 CommandInfo::BuiltIn { name, .. } => name.to_string(),
140                 CommandInfo::External { name, .. } => name.to_string(),
141             }
142         }
143     }
144 }
145
146 fn main() {}