@@ -362,61 +362,7 @@ impl EarlyLintPass for MiscEarlyLints {
362362 }
363363 }
364364
365- if let PatKind :: TupleStruct ( _, ref patterns) | PatKind :: Tuple ( ref patterns) = pat. node {
366- fn span_lint ( cx : & EarlyContext < ' _ > , span : Span , only_one : bool ) {
367- span_lint_and_sugg (
368- cx,
369- UNNEEDED_WILDCARD_PATTERN ,
370- span,
371- if only_one {
372- "this pattern is unneeded as the `..` pattern can match that element"
373- } else {
374- "these patterns are unneeded as the `..` pattern can match those elements"
375- } ,
376- if only_one { "remove it" } else { "remove them" } ,
377- "" . to_string ( ) ,
378- Applicability :: MachineApplicable ,
379- ) ;
380- }
381-
382- fn is_rest < P : std:: ops:: Deref < Target = Pat > > ( pat : & P ) -> bool {
383- if let PatKind :: Rest = pat. node {
384- true
385- } else {
386- false
387- }
388- }
389-
390- fn is_wild < P : std:: ops:: Deref < Target = Pat > > ( pat : & & P ) -> bool {
391- if let PatKind :: Wild = pat. node {
392- true
393- } else {
394- false
395- }
396- }
397-
398- if let Some ( rest_index) = patterns. iter ( ) . position ( is_rest) {
399- if let Some ( ( left_index, left_pat) ) = patterns[ ..rest_index]
400- . iter ( )
401- . rev ( )
402- . take_while ( is_wild)
403- . enumerate ( )
404- . last ( )
405- {
406- span_lint ( cx, left_pat. span . until ( patterns[ rest_index] . span ) , left_index == 0 ) ;
407- }
408-
409- if let Some ( ( right_index, right_pat) ) =
410- patterns[ rest_index + 1 ..] . iter ( ) . take_while ( is_wild) . enumerate ( ) . last ( )
411- {
412- span_lint (
413- cx,
414- patterns[ rest_index] . span . shrink_to_hi ( ) . to ( right_pat. span ) ,
415- right_index == 0 ,
416- ) ;
417- }
418- }
419- }
365+ check_unneeded_wildcard_pattern ( cx, pat) ;
420366 }
421367
422368 fn check_fn ( & mut self , cx : & EarlyContext < ' _ > , _: FnKind < ' _ > , decl : & FnDecl , _: Span , _: NodeId ) {
@@ -611,3 +557,62 @@ impl MiscEarlyLints {
611557 }
612558 }
613559}
560+
561+ fn check_unneeded_wildcard_pattern ( cx : & EarlyContext < ' _ > , pat : & Pat ) {
562+ if let PatKind :: TupleStruct ( _, ref patterns) | PatKind :: Tuple ( ref patterns) = pat. node {
563+ fn span_lint ( cx : & EarlyContext < ' _ > , span : Span , only_one : bool ) {
564+ span_lint_and_sugg (
565+ cx,
566+ UNNEEDED_WILDCARD_PATTERN ,
567+ span,
568+ if only_one {
569+ "this pattern is unneeded as the `..` pattern can match that element"
570+ } else {
571+ "these patterns are unneeded as the `..` pattern can match those elements"
572+ } ,
573+ if only_one { "remove it" } else { "remove them" } ,
574+ "" . to_string ( ) ,
575+ Applicability :: MachineApplicable ,
576+ ) ;
577+ }
578+
579+ fn is_rest < P : std:: ops:: Deref < Target = Pat > > ( pat : & P ) -> bool {
580+ if let PatKind :: Rest = pat. node {
581+ true
582+ } else {
583+ false
584+ }
585+ }
586+
587+ #[ allow( clippy:: trivially_copy_pass_by_ref) ]
588+ fn is_wild < P : std:: ops:: Deref < Target = Pat > > ( pat : & & P ) -> bool {
589+ if let PatKind :: Wild = pat. node {
590+ true
591+ } else {
592+ false
593+ }
594+ }
595+
596+ if let Some ( rest_index) = patterns. iter ( ) . position ( is_rest) {
597+ if let Some ( ( left_index, left_pat) ) = patterns[ ..rest_index]
598+ . iter ( )
599+ . rev ( )
600+ . take_while ( is_wild)
601+ . enumerate ( )
602+ . last ( )
603+ {
604+ span_lint ( cx, left_pat. span . until ( patterns[ rest_index] . span ) , left_index == 0 ) ;
605+ }
606+
607+ if let Some ( ( right_index, right_pat) ) =
608+ patterns[ rest_index + 1 ..] . iter ( ) . take_while ( is_wild) . enumerate ( ) . last ( )
609+ {
610+ span_lint (
611+ cx,
612+ patterns[ rest_index] . span . shrink_to_hi ( ) . to ( right_pat. span ) ,
613+ right_index == 0 ,
614+ ) ;
615+ }
616+ }
617+ }
618+ }
0 commit comments