1
- use syntax:: ast:: { self , AstNode , Pat } ;
1
+ use syntax:: ast:: { self , AstNode } ;
2
2
3
3
use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
4
4
@@ -31,27 +31,16 @@ pub(crate) fn convert_two_arm_bool_match_to_matches_macro(
31
31
return None ;
32
32
}
33
33
34
- let mut normal_arm = None ;
35
- let mut normal_expr = None ;
36
- let mut wildcard_expr = None ;
37
- for arm in match_arm_list. arms ( ) {
38
- if matches ! ( arm. pat( ) , Some ( Pat :: WildcardPat ( _) ) ) && arm. guard ( ) . is_none ( ) {
39
- wildcard_expr = arm. expr ( ) ;
40
- } else if !matches ! ( arm. pat( ) , Some ( Pat :: WildcardPat ( _) ) ) {
41
- normal_arm = Some ( arm. clone ( ) ) ;
42
- normal_expr = arm. expr ( ) ;
43
- }
44
- }
34
+ let first_arm = match_arm_list. arms ( ) . next ( ) ?;
35
+ let first_arm_expr = first_arm. expr ( ) ;
45
36
46
37
let invert_matches;
47
- if is_bool_literal_expr ( & normal_expr , true ) && is_bool_literal_expr ( & wildcard_expr , false ) {
38
+ if is_bool_literal_expr ( & first_arm_expr , true ) {
48
39
invert_matches = false ;
49
- } else if is_bool_literal_expr ( & normal_expr, false )
50
- && is_bool_literal_expr ( & wildcard_expr, true )
51
- {
40
+ } else if is_bool_literal_expr ( & first_arm_expr, false ) {
52
41
invert_matches = true ;
53
42
} else {
54
- cov_mark:: hit!( non_invert_bool_literal_arms ) ;
43
+ cov_mark:: hit!( non_bool_literal_match ) ;
55
44
return None ;
56
45
}
57
46
@@ -64,10 +53,10 @@ pub(crate) fn convert_two_arm_bool_match_to_matches_macro(
64
53
target_range,
65
54
|builder| {
66
55
let mut arm_str = String :: new ( ) ;
67
- if let Some ( ref pat) = normal_arm . as_ref ( ) . unwrap ( ) . pat ( ) {
56
+ if let Some ( ref pat) = first_arm . pat ( ) {
68
57
arm_str += & pat. to_string ( ) ;
69
58
}
70
- if let Some ( ref guard) = normal_arm . as_ref ( ) . unwrap ( ) . guard ( ) {
59
+ if let Some ( ref guard) = first_arm . guard ( ) {
71
60
arm_str += & format ! ( " {}" , & guard. to_string( ) ) ;
72
61
}
73
62
if invert_matches {
@@ -129,7 +118,7 @@ fn foo(a: Option<u32>) -> bool {
129
118
130
119
#[ test]
131
120
fn not_applicable_non_bool_literal_arms ( ) {
132
- cov_mark:: check!( non_invert_bool_literal_arms ) ;
121
+ cov_mark:: check!( non_bool_literal_match ) ;
133
122
check_assist_not_applicable (
134
123
convert_two_arm_bool_match_to_matches_macro,
135
124
r#"
@@ -144,108 +133,84 @@ fn foo(a: Option<u32>) -> bool {
144
133
}
145
134
146
135
#[ test]
147
- fn not_applicable_both_false_arms ( ) {
148
- cov_mark:: check!( non_invert_bool_literal_arms) ;
149
- check_assist_not_applicable (
136
+ fn convert_simple_case ( ) {
137
+ check_assist (
150
138
convert_two_arm_bool_match_to_matches_macro,
151
139
r#"
152
140
fn foo(a: Option<u32>) -> bool {
153
141
match a$0 {
154
- Some(val ) => false ,
142
+ Some(_val ) => true ,
155
143
_ => false
156
144
}
157
145
}
158
- "# ,
159
- ) ;
160
- }
161
-
162
- #[ test]
163
- fn not_applicable_both_true_arms ( ) {
164
- cov_mark:: check!( non_invert_bool_literal_arms) ;
165
- check_assist_not_applicable (
166
- convert_two_arm_bool_match_to_matches_macro,
146
+ "# ,
167
147
r#"
168
148
fn foo(a: Option<u32>) -> bool {
169
- match a$0 {
170
- Some(val) => true,
171
- _ => true
172
- }
173
- }
174
- "# ,
175
- ) ;
176
- }
177
-
178
- #[ test]
179
- fn not_applicable_non_bool_match ( ) {
180
- cov_mark:: check!( non_invert_bool_literal_arms) ;
181
- check_assist_not_applicable (
182
- convert_two_arm_bool_match_to_matches_macro,
183
- r#"
184
- fn foo(a: Option<u32>) -> u32 {
185
- match a$0 {
186
- Some(_val) => 1,
187
- _ => 0
188
- }
149
+ matches!(a, Some(_val))
189
150
}
190
151
"# ,
191
152
) ;
192
153
}
193
154
194
155
#[ test]
195
- fn convert_simple_case ( ) {
156
+ fn convert_simple_invert_case ( ) {
196
157
check_assist (
197
158
convert_two_arm_bool_match_to_matches_macro,
198
159
r#"
199
160
fn foo(a: Option<u32>) -> bool {
200
161
match a$0 {
201
- Some(_val) => true ,
202
- _ => false
162
+ Some(_val) => false ,
163
+ _ => true
203
164
}
204
165
}
205
166
"# ,
206
167
r#"
207
168
fn foo(a: Option<u32>) -> bool {
208
- matches!(a, Some(_val))
169
+ ! matches!(a, Some(_val))
209
170
}
210
171
"# ,
211
172
) ;
212
173
}
213
174
214
175
#[ test]
215
- fn convert_simple_invert_case ( ) {
176
+ fn convert_with_guard_case ( ) {
216
177
check_assist (
217
178
convert_two_arm_bool_match_to_matches_macro,
218
179
r#"
219
180
fn foo(a: Option<u32>) -> bool {
220
181
match a$0 {
221
- Some(_val) => false ,
222
- _ => true
182
+ Some(val) if val > 3 => true ,
183
+ _ => false
223
184
}
224
185
}
225
186
"# ,
226
187
r#"
227
188
fn foo(a: Option<u32>) -> bool {
228
- ! matches!(a, Some(_val) )
189
+ matches!(a, Some(val) if val > 3 )
229
190
}
230
191
"# ,
231
192
) ;
232
193
}
233
194
234
195
#[ test]
235
- fn convert_with_guard_case ( ) {
196
+ fn convert_enum_match_cases ( ) {
236
197
check_assist (
237
198
convert_two_arm_bool_match_to_matches_macro,
238
199
r#"
239
- fn foo(a: Option<u32>) -> bool {
200
+ enum X { A, B }
201
+
202
+ fn foo(a: X) -> bool {
240
203
match a$0 {
241
- Some(val) if val > 3 => true,
204
+ X::A => true,
242
205
_ => false
243
206
}
244
207
}
245
208
"# ,
246
209
r#"
247
- fn foo(a: Option<u32>) -> bool {
248
- matches!(a, Some(val) if val > 3)
210
+ enum X { A, B }
211
+
212
+ fn foo(a: X) -> bool {
213
+ matches!(a, X::A)
249
214
}
250
215
"# ,
251
216
) ;
0 commit comments