@@ -10,7 +10,7 @@ use syntax::ast::LitKind;
10
10
use syntax:: codemap:: Span ;
11
11
use utils:: paths;
12
12
use utils:: { match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block, walk_ptrs_ty,
13
- is_expn_of} ;
13
+ is_expn_of, remove_blocks } ;
14
14
use utils:: sugg:: Sugg ;
15
15
16
16
/// **What it does:** Checks for matches with a single arm where an `if let`
@@ -179,11 +179,12 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
179
179
if arms. len ( ) == 2 &&
180
180
arms[ 0 ] . pats . len ( ) == 1 && arms[ 0 ] . guard . is_none ( ) &&
181
181
arms[ 1 ] . pats . len ( ) == 1 && arms[ 1 ] . guard . is_none ( ) {
182
- let els = if is_unit_expr ( & arms[ 1 ] . body ) {
182
+ let els = remove_blocks ( & arms[ 1 ] . body ) ;
183
+ let els = if is_unit_expr ( els) {
183
184
None
184
- } else if let ExprBlock ( _) = arms [ 1 ] . body . node {
185
+ } else if let ExprBlock ( _) = els . node {
185
186
// matches with blocks that contain statements are prettier as `if let + else`
186
- Some ( & * arms [ 1 ] . body )
187
+ Some ( els )
187
188
} else {
188
189
// allow match arms with just expressions
189
190
return ;
@@ -198,29 +199,33 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
198
199
199
200
fn check_single_match_single_pattern ( cx : & LateContext , ex : & Expr , arms : & [ Arm ] , expr : & Expr , els : Option < & Expr > ) {
200
201
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
- } ) ;
202
+ report_single_match_single_pattern ( cx, ex, arms, expr, els) ;
221
203
}
222
204
}
223
205
206
+ fn report_single_match_single_pattern ( cx : & LateContext , ex : & Expr , arms : & [ Arm ] , expr : & Expr , els : Option < & Expr > ) {
207
+ let lint = if els. is_some ( ) {
208
+ SINGLE_MATCH_ELSE
209
+ } else {
210
+ SINGLE_MATCH
211
+ } ;
212
+ let els_str = els. map_or ( String :: new ( ) , |els| format ! ( " else {}" , expr_block( cx, els, None , ".." ) ) ) ;
213
+ span_lint_and_then ( cx,
214
+ lint,
215
+ expr. span ,
216
+ "you seem to be trying to use match for destructuring a single pattern. \
217
+ Consider using `if let`",
218
+ |db| {
219
+ db. span_suggestion ( expr. span ,
220
+ "try this" ,
221
+ format ! ( "if let {} = {} {}{}" ,
222
+ snippet( cx, arms[ 0 ] . pats[ 0 ] . span, ".." ) ,
223
+ snippet( cx, ex. span, ".." ) ,
224
+ expr_block( cx, & arms[ 0 ] . body, None , ".." ) ,
225
+ els_str) ) ;
226
+ } ) ;
227
+ }
228
+
224
229
fn check_single_match_opt_like (
225
230
cx : & LateContext ,
226
231
ex : & Expr ,
@@ -253,26 +258,7 @@ fn check_single_match_opt_like(
253
258
254
259
for & ( ty_path, pat_path) in candidates {
255
260
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
- } ) ;
261
+ report_single_match_single_pattern ( cx, ex, arms, expr, els) ;
276
262
}
277
263
}
278
264
}
0 commit comments