@@ -264,7 +264,7 @@ declare_lint! {
264
264
pub struct UnusedParens ;
265
265
266
266
impl UnusedParens {
267
- fn check_unused_parens_core ( & self ,
267
+ fn check_unused_parens_expr ( & self ,
268
268
cx : & EarlyContext ,
269
269
value : & ast:: Expr ,
270
270
msg : & str ,
@@ -313,6 +313,56 @@ impl UnusedParens {
313
313
}
314
314
}
315
315
}
316
+
317
+ fn check_unused_parens_pat ( & self ,
318
+ cx : & EarlyContext ,
319
+ value : & ast:: Pat ,
320
+ msg : & str ,
321
+ struct_lit_needs_parens : bool ) {
322
+ if let ast:: PatKind :: Paren ( _) = value. node {
323
+ // Does there need to be a check similar to `parser::contains_exterior_struct_lit`
324
+ // here?
325
+ if !struct_lit_needs_parens {
326
+ let span_msg = format ! ( "unnecessary parentheses around {}" , msg) ;
327
+ let mut err = cx. struct_span_lint ( UNUSED_PARENS ,
328
+ value. span ,
329
+ & span_msg) ;
330
+ // Remove exactly one pair of parentheses (rather than naïvely
331
+ // stripping all paren characters)
332
+ let mut ate_left_paren = false ;
333
+ let mut ate_right_paren = false ;
334
+ let parens_removed = pprust:: pat_to_string ( value)
335
+ . trim_matches ( |c| {
336
+ match c {
337
+ '(' => {
338
+ if ate_left_paren {
339
+ false
340
+ } else {
341
+ ate_left_paren = true ;
342
+ true
343
+ }
344
+ } ,
345
+ ')' => {
346
+ if ate_right_paren {
347
+ false
348
+ } else {
349
+ ate_right_paren = true ;
350
+ true
351
+ }
352
+ } ,
353
+ _ => false ,
354
+ }
355
+ } ) . to_owned ( ) ;
356
+ err. span_suggestion_short_with_applicability (
357
+ value. span ,
358
+ "remove these parentheses" ,
359
+ parens_removed,
360
+ Applicability :: MachineApplicable
361
+ ) ;
362
+ err. emit ( ) ;
363
+ }
364
+ }
365
+ }
316
366
}
317
367
318
368
impl LintPass for UnusedParens {
@@ -354,18 +404,32 @@ impl EarlyLintPass for UnusedParens {
354
404
}
355
405
let msg = format ! ( "{} argument" , call_kind) ;
356
406
for arg in args_to_check {
357
- self . check_unused_parens_core ( cx, arg, & msg, false ) ;
407
+ self . check_unused_parens_expr ( cx, arg, & msg, false ) ;
358
408
}
359
409
return ;
360
410
}
361
411
} ;
362
- self . check_unused_parens_core ( cx, & value, msg, struct_lit_needs_parens) ;
412
+ self . check_unused_parens_expr ( cx, & value, msg, struct_lit_needs_parens) ;
413
+ }
414
+
415
+ fn check_pat ( & mut self , cx : & EarlyContext , p : & ast:: Pat ) {
416
+ use ast:: PatKind :: * ;
417
+ let ( value, msg) = match p. node {
418
+ Paren ( ref pat) => {
419
+ match pat. node {
420
+ Wild => ( p, "wildcard pattern" ) ,
421
+ _ => return ,
422
+ }
423
+ }
424
+ _ => return ,
425
+ } ;
426
+ self . check_unused_parens_pat ( cx, & value, msg, false ) ;
363
427
}
364
428
365
429
fn check_stmt ( & mut self , cx : & EarlyContext , s : & ast:: Stmt ) {
366
430
if let ast:: StmtKind :: Local ( ref local) = s. node {
367
431
if let Some ( ref value) = local. init {
368
- self . check_unused_parens_core ( cx, & value, "assigned value" , false ) ;
432
+ self . check_unused_parens_expr ( cx, & value, "assigned value" , false ) ;
369
433
}
370
434
}
371
435
}
0 commit comments