@@ -198,29 +198,33 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
198
198
199
199
fn check_single_match_single_pattern ( cx : & LateContext , ex : & Expr , arms : & [ Arm ] , expr : & Expr , els : Option < & Expr > ) {
200
200
if arms[ 1 ] . pats [ 0 ] . node == PatKind :: Wild {
201
- let lint = if els. is_some ( ) {
202
- SINGLE_MATCH_ELSE
203
- } else {
204
- SINGLE_MATCH
205
- } ;
206
- let els_str = els. map_or ( String :: new ( ) , |els| format ! ( " else {}" , expr_block( cx, els, None , ".." ) ) ) ;
207
- span_lint_and_then ( cx,
208
- lint,
209
- expr. span ,
210
- "you seem to be trying to use match for destructuring a single pattern. \
211
- Consider using `if let`",
212
- |db| {
213
- db. span_suggestion ( expr. span ,
214
- "try this" ,
215
- format ! ( "if let {} = {} {}{}" ,
216
- snippet( cx, arms[ 0 ] . pats[ 0 ] . span, ".." ) ,
217
- snippet( cx, ex. span, ".." ) ,
218
- expr_block( cx, & arms[ 0 ] . body, None , ".." ) ,
219
- els_str) ) ;
220
- } ) ;
201
+ report_single_match_single_pattern ( cx, ex, arms, expr, els) ;
221
202
}
222
203
}
223
204
205
+ fn report_single_match_single_pattern ( cx : & LateContext , ex : & Expr , arms : & [ Arm ] , expr : & Expr , els : Option < & Expr > ) {
206
+ let lint = if els. is_some ( ) {
207
+ SINGLE_MATCH_ELSE
208
+ } else {
209
+ SINGLE_MATCH
210
+ } ;
211
+ let els_str = els. map_or ( String :: new ( ) , |els| format ! ( " else {}" , expr_block( cx, els, None , ".." ) ) ) ;
212
+ span_lint_and_then ( cx,
213
+ lint,
214
+ expr. span ,
215
+ "you seem to be trying to use match for destructuring a single pattern. \
216
+ Consider using `if let`",
217
+ |db| {
218
+ db. span_suggestion ( expr. span ,
219
+ "try this" ,
220
+ format ! ( "if let {} = {} {}{}" ,
221
+ snippet( cx, arms[ 0 ] . pats[ 0 ] . span, ".." ) ,
222
+ snippet( cx, ex. span, ".." ) ,
223
+ expr_block( cx, & arms[ 0 ] . body, None , ".." ) ,
224
+ els_str) ) ;
225
+ } ) ;
226
+ }
227
+
224
228
fn check_single_match_opt_like (
225
229
cx : & LateContext ,
226
230
ex : & Expr ,
@@ -253,26 +257,7 @@ fn check_single_match_opt_like(
253
257
254
258
for & ( ty_path, pat_path) in candidates {
255
259
if & path == pat_path && match_type ( cx, ty, ty_path) {
256
- let lint = if els. is_some ( ) {
257
- SINGLE_MATCH_ELSE
258
- } else {
259
- SINGLE_MATCH
260
- } ;
261
- let els_str = els. map_or ( String :: new ( ) , |els| format ! ( " else {}" , expr_block( cx, els, None , ".." ) ) ) ;
262
- span_lint_and_then ( cx,
263
- lint,
264
- expr. span ,
265
- "you seem to be trying to use match for destructuring a single pattern. Consider \
266
- using `if let`",
267
- |db| {
268
- db. span_suggestion ( expr. span ,
269
- "try this" ,
270
- format ! ( "if let {} = {} {}{}" ,
271
- snippet( cx, arms[ 0 ] . pats[ 0 ] . span, ".." ) ,
272
- snippet( cx, ex. span, ".." ) ,
273
- expr_block( cx, & arms[ 0 ] . body, None , ".." ) ,
274
- els_str) ) ;
275
- } ) ;
260
+ report_single_match_single_pattern ( cx, ex, arms, expr, els) ;
276
261
}
277
262
}
278
263
}
0 commit comments