@@ -14,7 +14,6 @@ use rustc::{declare_lint_pass, declare_tool_lint};
14
14
use rustc_errors:: Applicability ;
15
15
use std:: cmp:: Ordering ;
16
16
use std:: collections:: Bound ;
17
- use std:: ops:: Deref ;
18
17
use syntax:: ast:: LitKind ;
19
18
use syntax:: source_map:: Span ;
20
19
@@ -255,9 +254,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches {
255
254
256
255
#[ rustfmt:: skip]
257
256
fn check_single_match ( cx : & LateContext < ' _ , ' _ > , ex : & Expr , arms : & [ Arm ] , expr : & Expr ) {
258
- if arms. len ( ) == 2 &&
259
- arms[ 0 ] . pats . len ( ) == 1 && arms[ 0 ] . guard . is_none ( ) &&
260
- arms[ 1 ] . pats . len ( ) == 1 && arms[ 1 ] . guard . is_none ( ) {
257
+ if arms. len ( ) == 2 && arms[ 0 ] . guard . is_none ( ) && arms[ 1 ] . guard . is_none ( ) {
261
258
let els = remove_blocks ( & arms[ 1 ] . body ) ;
262
259
let els = if is_unit_expr ( els) {
263
260
None
@@ -283,7 +280,7 @@ fn check_single_match_single_pattern(
283
280
expr : & Expr ,
284
281
els : Option < & Expr > ,
285
282
) {
286
- if is_wild ( & arms[ 1 ] . pats [ 0 ] ) {
283
+ if is_wild ( & arms[ 1 ] . pat ) {
287
284
report_single_match_single_pattern ( cx, ex, arms, expr, els) ;
288
285
}
289
286
}
@@ -308,7 +305,7 @@ fn report_single_match_single_pattern(
308
305
"try this" ,
309
306
format ! (
310
307
"if let {} = {} {}{}" ,
311
- snippet( cx, arms[ 0 ] . pats [ 0 ] . span, ".." ) ,
308
+ snippet( cx, arms[ 0 ] . pat . span, ".." ) ,
312
309
snippet( cx, ex. span, ".." ) ,
313
310
expr_block( cx, & arms[ 0 ] . body, None , ".." ) ,
314
311
els_str,
@@ -336,7 +333,7 @@ fn check_single_match_opt_like(
336
333
( & paths:: RESULT , "Ok" ) ,
337
334
] ;
338
335
339
- let path = match arms[ 1 ] . pats [ 0 ] . node {
336
+ let path = match arms[ 1 ] . pat . node {
340
337
PatKind :: TupleStruct ( ref path, ref inner, _) => {
341
338
// Contains any non wildcard patterns (e.g., `Err(err)`)?
342
339
if !inner. iter ( ) . all ( is_wild) {
@@ -365,9 +362,9 @@ fn check_match_bool(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Ex
365
362
expr. span ,
366
363
"you seem to be trying to match on a boolean expression" ,
367
364
move |db| {
368
- if arms. len ( ) == 2 && arms [ 0 ] . pats . len ( ) == 1 {
365
+ if arms. len ( ) == 2 {
369
366
// no guards
370
- let exprs = if let PatKind :: Lit ( ref arm_bool) = arms[ 0 ] . pats [ 0 ] . node {
367
+ let exprs = if let PatKind :: Lit ( ref arm_bool) = arms[ 0 ] . pat . node {
371
368
if let ExprKind :: Lit ( ref lit) = arm_bool. node {
372
369
match lit. node {
373
370
LitKind :: Bool ( true ) => Some ( ( & * arms[ 0 ] . body , & * arms[ 1 ] . body ) ) ,
@@ -446,7 +443,7 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
446
443
let ex_ty = walk_ptrs_ty ( cx. tables . expr_ty ( ex) ) ;
447
444
if match_type ( cx, ex_ty, & paths:: RESULT ) {
448
445
for arm in arms {
449
- if let PatKind :: TupleStruct ( ref path, ref inner, _) = arm. pats [ 0 ] . node {
446
+ if let PatKind :: TupleStruct ( ref path, ref inner, _) = arm. pat . node {
450
447
let path_str = print:: to_string ( print:: NO_ANN , |s| s. print_qpath ( path, false ) ) ;
451
448
if_chain ! {
452
449
if path_str == "Err" ;
@@ -457,9 +454,9 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
457
454
// `Err(_)` arm with `panic!` found
458
455
span_note_and_lint( cx,
459
456
MATCH_WILD_ERR_ARM ,
460
- arm. pats [ 0 ] . span,
457
+ arm. pat . span,
461
458
"Err(_) will match all errors, maybe not a good idea" ,
462
- arm. pats [ 0 ] . span,
459
+ arm. pat . span,
463
460
"to remove this warning, match each error separately \
464
461
or use unreachable macro") ;
465
462
}
@@ -482,13 +479,11 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
482
479
let mut wildcard_span = None ;
483
480
let mut wildcard_ident = None ;
484
481
for arm in arms {
485
- for pat in & arm. pats {
486
- if let PatKind :: Wild = pat. node {
487
- wildcard_span = Some ( pat. span ) ;
488
- } else if let PatKind :: Binding ( _, _, ident, None ) = pat. node {
489
- wildcard_span = Some ( pat. span ) ;
490
- wildcard_ident = Some ( ident) ;
491
- }
482
+ if let PatKind :: Wild = arm. pat . node {
483
+ wildcard_span = Some ( arm. pat . span ) ;
484
+ } else if let PatKind :: Binding ( _, _, ident, None ) = arm. pat . node {
485
+ wildcard_span = Some ( arm. pat . span ) ;
486
+ wildcard_ident = Some ( ident) ;
492
487
}
493
488
}
494
489
@@ -510,15 +505,13 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
510
505
// covered by the set of guards that cover it, but that's really hard to do.
511
506
continue ;
512
507
}
513
- for pat in & arm. pats {
514
- if let PatKind :: Path ( ref path) = pat. deref ( ) . node {
515
- if let QPath :: Resolved ( _, p) = path {
516
- missing_variants. retain ( |e| e. ctor_def_id != Some ( p. res . def_id ( ) ) ) ;
517
- }
518
- } else if let PatKind :: TupleStruct ( ref path, ..) = pat. deref ( ) . node {
519
- if let QPath :: Resolved ( _, p) = path {
520
- missing_variants. retain ( |e| e. ctor_def_id != Some ( p. res . def_id ( ) ) ) ;
521
- }
508
+ if let PatKind :: Path ( ref path) = arm. pat . node {
509
+ if let QPath :: Resolved ( _, p) = path {
510
+ missing_variants. retain ( |e| e. ctor_def_id != Some ( p. res . def_id ( ) ) ) ;
511
+ }
512
+ } else if let PatKind :: TupleStruct ( ref path, ..) = arm. pat . node {
513
+ if let QPath :: Resolved ( _, p) = path {
514
+ missing_variants. retain ( |e| e. ctor_def_id != Some ( p. res . def_id ( ) ) ) ;
522
515
}
523
516
}
524
517
}
@@ -588,9 +581,9 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr:
588
581
)
589
582
} ;
590
583
591
- suggs. extend ( arms. iter ( ) . flat_map ( |a| & a . pats ) . filter_map ( |p | {
592
- if let PatKind :: Ref ( ref refp, _) = p . node {
593
- Some ( ( p . span , snippet ( cx, refp. span , ".." ) . to_string ( ) ) )
584
+ suggs. extend ( arms. iter ( ) . filter_map ( |a | {
585
+ if let PatKind :: Ref ( ref refp, _) = a . pat . node {
586
+ Some ( ( a . pat . span , snippet ( cx, refp. span , ".." ) . to_string ( ) ) )
594
587
} else {
595
588
None
596
589
}
@@ -605,12 +598,7 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr:
605
598
}
606
599
607
600
fn check_match_as_ref ( cx : & LateContext < ' _ , ' _ > , ex : & Expr , arms : & [ Arm ] , expr : & Expr ) {
608
- if arms. len ( ) == 2
609
- && arms[ 0 ] . pats . len ( ) == 1
610
- && arms[ 0 ] . guard . is_none ( )
611
- && arms[ 1 ] . pats . len ( ) == 1
612
- && arms[ 1 ] . guard . is_none ( )
613
- {
601
+ if arms. len ( ) == 2 && arms[ 0 ] . guard . is_none ( ) && arms[ 1 ] . guard . is_none ( ) {
614
602
let arm_ref: Option < BindingAnnotation > = if is_none_arm ( & arms[ 0 ] ) {
615
603
is_ref_some_arm ( & arms[ 1 ] )
616
604
} else if is_none_arm ( & arms[ 1 ] ) {
@@ -666,14 +654,9 @@ fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm]) -> Vec<Sp
666
654
arms. iter ( )
667
655
. flat_map ( |arm| {
668
656
if let Arm {
669
- ref pats , guard : None , ..
657
+ ref pat , guard : None , ..
670
658
} = * arm
671
659
{
672
- pats. iter ( )
673
- } else {
674
- [ ] . iter ( )
675
- }
676
- . filter_map ( |pat| {
677
660
if let PatKind :: Range ( ref lhs, ref rhs, ref range_end) = pat. node {
678
661
let lhs = constant ( cx, cx. tables , lhs) ?. 0 ;
679
662
let rhs = constant ( cx, cx. tables , rhs) ?. 0 ;
@@ -694,9 +677,8 @@ fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm]) -> Vec<Sp
694
677
node : ( value. clone ( ) , Bound :: Included ( value) ) ,
695
678
} ) ;
696
679
}
697
-
698
- None
699
- } )
680
+ }
681
+ None
700
682
} )
701
683
. collect ( )
702
684
}
@@ -743,7 +725,7 @@ fn is_unit_expr(expr: &Expr) -> bool {
743
725
744
726
// Checks if arm has the form `None => None`
745
727
fn is_none_arm ( arm : & Arm ) -> bool {
746
- match arm. pats [ 0 ] . node {
728
+ match arm. pat . node {
747
729
PatKind :: Path ( ref path) if match_qpath ( path, & paths:: OPTION_NONE ) => true ,
748
730
_ => false ,
749
731
}
@@ -752,7 +734,7 @@ fn is_none_arm(arm: &Arm) -> bool {
752
734
// Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
753
735
fn is_ref_some_arm ( arm : & Arm ) -> Option < BindingAnnotation > {
754
736
if_chain ! {
755
- if let PatKind :: TupleStruct ( ref path, ref pats, _) = arm. pats [ 0 ] . node;
737
+ if let PatKind :: TupleStruct ( ref path, ref pats, _) = arm. pat . node;
756
738
if pats. len( ) == 1 && match_qpath( path, & paths:: OPTION_SOME ) ;
757
739
if let PatKind :: Binding ( rb, .., ident, _) = pats[ 0 ] . node;
758
740
if rb == BindingAnnotation :: Ref || rb == BindingAnnotation :: RefMut ;
@@ -772,9 +754,8 @@ fn is_ref_some_arm(arm: &Arm) -> Option<BindingAnnotation> {
772
754
fn has_only_ref_pats ( arms : & [ Arm ] ) -> bool {
773
755
let mapped = arms
774
756
. iter ( )
775
- . flat_map ( |a| & a. pats )
776
- . map ( |p| {
777
- match p. node {
757
+ . map ( |a| {
758
+ match a. pat . node {
778
759
PatKind :: Ref ( ..) => Some ( true ) , // &-patterns
779
760
PatKind :: Wild => Some ( false ) , // an "anything" wildcard is also fine
780
761
_ => None , // any other pattern is not fine
0 commit comments