@@ -47,60 +47,54 @@ declare_lint_pass!(RestWhenDestructuringStruct => [REST_WHEN_DESTRUCTURING_STRUC
4747
4848impl < ' tcx > LateLintPass < ' tcx > for RestWhenDestructuringStruct {
4949 fn check_pat ( & mut self , cx : & rustc_lint:: LateContext < ' tcx > , pat : & ' tcx rustc_hir:: Pat < ' tcx > ) {
50- if pat. span . in_external_macro ( cx. tcx . sess . source_map ( ) ) {
51- return ;
52- }
53-
54- if is_from_proc_macro ( cx, pat) {
55- return ;
56- }
57-
58- if let rustc_hir:: PatKind :: Struct ( path, fields, true ) = pat. kind {
59- let qty = cx. typeck_results ( ) . qpath_res ( & path, pat. hir_id ) ;
60- let ty = cx. typeck_results ( ) . pat_ty ( pat) ;
61- if let ty:: Adt ( a, _) = ty. kind ( ) {
62- let vid = qty
63- . opt_def_id ( )
64- . map_or ( VariantIdx :: ZERO , |x| a. variant_index_with_id ( x) ) ;
65- let mut rest_fields = a. variants ( ) [ vid]
66- . fields
67- . iter ( )
68- . map ( |field| field. ident ( cx. tcx ) )
69- . filter ( |pf| !fields. iter ( ) . any ( |x| x. ident == * pf) )
70- . map ( |x| format ! ( "{x}: _" ) ) ;
71- let fmt_fields = rest_fields. join ( ", " ) ;
50+ if let rustc_hir:: PatKind :: Struct ( path, fields, true ) = pat. kind
51+ && !pat. span . in_external_macro ( cx. tcx . sess . source_map ( ) )
52+ && !is_from_proc_macro ( cx, pat)
53+ && let qty = cx. typeck_results ( ) . qpath_res ( & path, pat. hir_id )
54+ && let ty = cx. typeck_results ( ) . pat_ty ( pat)
55+ && let ty:: Adt ( a, _) = ty. kind ( )
56+ {
57+ let vid = qty
58+ . opt_def_id ( )
59+ . map_or ( VariantIdx :: ZERO , |x| a. variant_index_with_id ( x) ) ;
60+ let mut rest_fields = a. variants ( ) [ vid]
61+ . fields
62+ . iter ( )
63+ . map ( |field| field. ident ( cx. tcx ) )
64+ . filter ( |pf| !fields. iter ( ) . any ( |x| x. ident == * pf) )
65+ . map ( |x| format ! ( "{x}: _" ) ) ;
66+ let fmt_fields = rest_fields. join ( ", " ) ;
7267
73- let sm = cx. sess ( ) . source_map ( ) ;
68+ let sm = cx. sess ( ) . source_map ( ) ;
7469
75- // It is not possible to get the span of the et cetera symbol at HIR level
76- // so we have to get it in a bit of a roundabout way:
70+ // It is not possible to get the span of the et cetera symbol at HIR level
71+ // so we have to get it in a bit of a roundabout way:
7772
78- // Find the end of the last field if any.
79- let last_field = fields. iter ( ) . last ( ) . map ( |x| x. span . shrink_to_hi ( ) ) ;
80- // If no last field take the whole pattern.
81- let last_field = last_field. unwrap_or ( pat. span . shrink_to_lo ( ) ) ;
82- // Create a new span starting and ending just before the first .
83- let before_dot = sm. span_extend_to_next_char ( last_field, '.' , true ) . shrink_to_hi ( ) ;
84- // Extend the span to the end of the line
85- let rest_of_line = sm. span_extend_to_next_char ( before_dot, '\n' , true ) ;
86- // Shrink the span so it only contains dots
87- let dotdot = sm. span_take_while ( rest_of_line, |x| * x == '.' ) ;
73+ // Find the end of the last field if any.
74+ let last_field = fields. iter ( ) . last ( ) . map ( |x| x. span . shrink_to_hi ( ) ) ;
75+ // If no last field take the whole pattern.
76+ let last_field = last_field. unwrap_or ( pat. span . shrink_to_lo ( ) ) ;
77+ // Create a new span starting and ending just before the first .
78+ let before_dot = sm. span_extend_to_next_char ( last_field, '.' , true ) . shrink_to_hi ( ) ;
79+ // Extend the span to the end of the line
80+ let rest_of_line = sm. span_extend_to_next_char ( before_dot, '\n' , true ) ;
81+ // Shrink the span so it only contains dots
82+ let dotdot = sm. span_take_while ( rest_of_line, |x| * x == '.' ) ;
8883
89- span_lint_and_then (
90- cx,
91- REST_WHEN_DESTRUCTURING_STRUCT ,
92- pat. span ,
93- "struct destructuring with rest (..)" ,
94- |diag| {
95- diag. span_suggestion_verbose (
96- dotdot,
97- "consider explicitly ignoring remaining fields with wildcard patterns (x: _)" ,
98- fmt_fields,
99- rustc_errors:: Applicability :: MachineApplicable ,
100- ) ;
101- } ,
102- ) ;
103- }
84+ span_lint_and_then (
85+ cx,
86+ REST_WHEN_DESTRUCTURING_STRUCT ,
87+ pat. span ,
88+ "struct destructuring with rest (..)" ,
89+ |diag| {
90+ diag. span_suggestion_verbose (
91+ dotdot,
92+ "consider explicitly ignoring remaining fields with wildcard patterns (x: _)" ,
93+ fmt_fields,
94+ rustc_errors:: Applicability :: MachineApplicable ,
95+ ) ;
96+ } ,
97+ ) ;
10498 }
10599 }
106100}
0 commit comments