@@ -14,7 +14,6 @@ use rustc::{declare_lint_pass, declare_tool_lint};
1414use rustc_errors:: Applicability ;
1515use std:: cmp:: Ordering ;
1616use std:: collections:: Bound ;
17- use std:: ops:: Deref ;
1817use syntax:: ast:: LitKind ;
1918use syntax:: source_map:: Span ;
2019
@@ -255,9 +254,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches {
255254
256255#[ rustfmt:: skip]
257256fn 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 ( ) {
258+ if let PatKind :: Or ( ..) = arms[ 0 ] . pat . node {
259+ // don't lint for or patterns for now, this makes
260+ // the lint noisy in unnecessary situations
261+ return ;
262+ }
261263 let els = remove_blocks ( & arms[ 1 ] . body ) ;
262264 let els = if is_unit_expr ( els) {
263265 None
@@ -283,7 +285,7 @@ fn check_single_match_single_pattern(
283285 expr : & Expr ,
284286 els : Option < & Expr > ,
285287) {
286- if is_wild ( & arms[ 1 ] . pats [ 0 ] ) {
288+ if is_wild ( & arms[ 1 ] . pat ) {
287289 report_single_match_single_pattern ( cx, ex, arms, expr, els) ;
288290 }
289291}
@@ -308,7 +310,7 @@ fn report_single_match_single_pattern(
308310 "try this" ,
309311 format ! (
310312 "if let {} = {} {}{}" ,
311- snippet( cx, arms[ 0 ] . pats [ 0 ] . span, ".." ) ,
313+ snippet( cx, arms[ 0 ] . pat . span, ".." ) ,
312314 snippet( cx, ex. span, ".." ) ,
313315 expr_block( cx, & arms[ 0 ] . body, None , ".." ) ,
314316 els_str,
@@ -336,7 +338,7 @@ fn check_single_match_opt_like(
336338 ( & paths:: RESULT , "Ok" ) ,
337339 ] ;
338340
339- let path = match arms[ 1 ] . pats [ 0 ] . node {
341+ let path = match arms[ 1 ] . pat . node {
340342 PatKind :: TupleStruct ( ref path, ref inner, _) => {
341343 // Contains any non wildcard patterns (e.g., `Err(err)`)?
342344 if !inner. iter ( ) . all ( is_wild) {
@@ -365,9 +367,9 @@ fn check_match_bool(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Ex
365367 expr. span ,
366368 "you seem to be trying to match on a boolean expression" ,
367369 move |db| {
368- if arms. len ( ) == 2 && arms [ 0 ] . pats . len ( ) == 1 {
370+ if arms. len ( ) == 2 {
369371 // no guards
370- let exprs = if let PatKind :: Lit ( ref arm_bool) = arms[ 0 ] . pats [ 0 ] . node {
372+ let exprs = if let PatKind :: Lit ( ref arm_bool) = arms[ 0 ] . pat . node {
371373 if let ExprKind :: Lit ( ref lit) = arm_bool. node {
372374 match lit. node {
373375 LitKind :: Bool ( true ) => Some ( ( & * arms[ 0 ] . body , & * arms[ 1 ] . body ) ) ,
@@ -446,7 +448,7 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
446448 let ex_ty = walk_ptrs_ty ( cx. tables . expr_ty ( ex) ) ;
447449 if match_type ( cx, ex_ty, & paths:: RESULT ) {
448450 for arm in arms {
449- if let PatKind :: TupleStruct ( ref path, ref inner, _) = arm. pats [ 0 ] . node {
451+ if let PatKind :: TupleStruct ( ref path, ref inner, _) = arm. pat . node {
450452 let path_str = print:: to_string ( print:: NO_ANN , |s| s. print_qpath ( path, false ) ) ;
451453 if_chain ! {
452454 if path_str == "Err" ;
@@ -457,9 +459,9 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
457459 // `Err(_)` arm with `panic!` found
458460 span_note_and_lint( cx,
459461 MATCH_WILD_ERR_ARM ,
460- arm. pats [ 0 ] . span,
462+ arm. pat . span,
461463 "Err(_) will match all errors, maybe not a good idea" ,
462- arm. pats [ 0 ] . span,
464+ arm. pat . span,
463465 "to remove this warning, match each error separately \
464466 or use unreachable macro") ;
465467 }
@@ -482,13 +484,11 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
482484 let mut wildcard_span = None ;
483485 let mut wildcard_ident = None ;
484486 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- }
487+ if let PatKind :: Wild = arm. pat . node {
488+ wildcard_span = Some ( arm. pat . span ) ;
489+ } else if let PatKind :: Binding ( _, _, ident, None ) = arm. pat . node {
490+ wildcard_span = Some ( arm. pat . span ) ;
491+ wildcard_ident = Some ( ident) ;
492492 }
493493 }
494494
@@ -510,15 +510,13 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
510510 // covered by the set of guards that cover it, but that's really hard to do.
511511 continue ;
512512 }
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- }
513+ if let PatKind :: Path ( ref path) = arm. pat . node {
514+ if let QPath :: Resolved ( _, p) = path {
515+ missing_variants. retain ( |e| e. ctor_def_id != Some ( p. res . def_id ( ) ) ) ;
516+ }
517+ } else if let PatKind :: TupleStruct ( ref path, ..) = arm. pat . node {
518+ if let QPath :: Resolved ( _, p) = path {
519+ missing_variants. retain ( |e| e. ctor_def_id != Some ( p. res . def_id ( ) ) ) ;
522520 }
523521 }
524522 }
@@ -588,9 +586,9 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr:
588586 )
589587 } ;
590588
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 ( ) ) )
589+ suggs. extend ( arms. iter ( ) . filter_map ( |a | {
590+ if let PatKind :: Ref ( ref refp, _) = a . pat . node {
591+ Some ( ( a . pat . span , snippet ( cx, refp. span , ".." ) . to_string ( ) ) )
594592 } else {
595593 None
596594 }
@@ -605,12 +603,7 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr:
605603}
606604
607605fn 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- {
606+ if arms. len ( ) == 2 && arms[ 0 ] . guard . is_none ( ) && arms[ 1 ] . guard . is_none ( ) {
614607 let arm_ref: Option < BindingAnnotation > = if is_none_arm ( & arms[ 0 ] ) {
615608 is_ref_some_arm ( & arms[ 1 ] )
616609 } else if is_none_arm ( & arms[ 1 ] ) {
@@ -666,14 +659,9 @@ fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm]) -> Vec<Sp
666659 arms. iter ( )
667660 . flat_map ( |arm| {
668661 if let Arm {
669- ref pats , guard : None , ..
662+ ref pat , guard : None , ..
670663 } = * arm
671664 {
672- pats. iter ( )
673- } else {
674- [ ] . iter ( )
675- }
676- . filter_map ( |pat| {
677665 if let PatKind :: Range ( ref lhs, ref rhs, ref range_end) = pat. node {
678666 let lhs = constant ( cx, cx. tables , lhs) ?. 0 ;
679667 let rhs = constant ( cx, cx. tables , rhs) ?. 0 ;
@@ -694,9 +682,8 @@ fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm]) -> Vec<Sp
694682 node : ( value. clone ( ) , Bound :: Included ( value) ) ,
695683 } ) ;
696684 }
697-
698- None
699- } )
685+ }
686+ None
700687 } )
701688 . collect ( )
702689}
@@ -743,7 +730,7 @@ fn is_unit_expr(expr: &Expr) -> bool {
743730
744731// Checks if arm has the form `None => None`
745732fn is_none_arm ( arm : & Arm ) -> bool {
746- match arm. pats [ 0 ] . node {
733+ match arm. pat . node {
747734 PatKind :: Path ( ref path) if match_qpath ( path, & paths:: OPTION_NONE ) => true ,
748735 _ => false ,
749736 }
@@ -752,7 +739,7 @@ fn is_none_arm(arm: &Arm) -> bool {
752739// Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
753740fn is_ref_some_arm ( arm : & Arm ) -> Option < BindingAnnotation > {
754741 if_chain ! {
755- if let PatKind :: TupleStruct ( ref path, ref pats, _) = arm. pats [ 0 ] . node;
742+ if let PatKind :: TupleStruct ( ref path, ref pats, _) = arm. pat . node;
756743 if pats. len( ) == 1 && match_qpath( path, & paths:: OPTION_SOME ) ;
757744 if let PatKind :: Binding ( rb, .., ident, _) = pats[ 0 ] . node;
758745 if rb == BindingAnnotation :: Ref || rb == BindingAnnotation :: RefMut ;
@@ -772,9 +759,8 @@ fn is_ref_some_arm(arm: &Arm) -> Option<BindingAnnotation> {
772759fn has_only_ref_pats ( arms : & [ Arm ] ) -> bool {
773760 let mapped = arms
774761 . iter ( )
775- . flat_map ( |a| & a. pats )
776- . map ( |p| {
777- match p. node {
762+ . map ( |a| {
763+ match a. pat . node {
778764 PatKind :: Ref ( ..) => Some ( true ) , // &-patterns
779765 PatKind :: Wild => Some ( false ) , // an "anything" wildcard is also fine
780766 _ => None , // any other pattern is not fine
0 commit comments