@@ -4023,63 +4023,60 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
40234023 && decl. can_be_made_mutable ( )
40244024 {
40254025 let mut is_for_loop = false ;
4026- let mut is_ref_pattern = false ;
4026+ let mut is_immut_ref_pattern = false ;
40274027 if let LocalInfo :: User ( BindingForm :: Var ( VarBindingForm {
40284028 opt_match_place : Some ( ( _, match_span) ) ,
40294029 ..
40304030 } ) ) = * decl. local_info ( )
40314031 {
40324032 if matches ! ( match_span. desugaring_kind( ) , Some ( DesugaringKind :: ForLoop ) ) {
40334033 is_for_loop = true ;
4034+ }
40344035
4035- if let Some ( body) = self . infcx . tcx . hir_maybe_body_owned_by ( self . mir_def_id ( ) ) {
4036- struct RefPatternFinder < ' tcx > {
4037- tcx : TyCtxt < ' tcx > ,
4038- binding_span : Span ,
4039- is_ref_pattern : bool ,
4040- }
4036+ if let Some ( body) = self . infcx . tcx . hir_maybe_body_owned_by ( self . mir_def_id ( ) ) {
4037+ struct RefPatternFinder < ' tcx > {
4038+ tcx : TyCtxt < ' tcx > ,
4039+ binding_span : Span ,
4040+ is_immut_ref_pattern : bool ,
4041+ }
40414042
4042- impl < ' tcx > Visitor < ' tcx > for RefPatternFinder < ' tcx > {
4043- type NestedFilter = OnlyBodies ;
4043+ impl < ' tcx > Visitor < ' tcx > for RefPatternFinder < ' tcx > {
4044+ type NestedFilter = OnlyBodies ;
40444045
4045- fn maybe_tcx ( & mut self ) -> Self :: MaybeTyCtxt {
4046- self . tcx
4047- }
4046+ fn maybe_tcx ( & mut self ) -> Self :: MaybeTyCtxt {
4047+ self . tcx
4048+ }
40484049
4049- fn visit_pat ( & mut self , pat : & ' tcx hir:: Pat < ' tcx > ) {
4050- if !self . is_ref_pattern
4051- && let hir:: PatKind :: Binding ( _, _, ident, _) = pat. kind
4052- && ident. span == self . binding_span
4053- {
4054- self . is_ref_pattern =
4055- self . tcx . hir_parent_iter ( pat. hir_id ) . any ( |( _, node) | {
4056- matches ! (
4057- node,
4058- hir:: Node :: Pat ( hir:: Pat {
4059- kind: hir:: PatKind :: Ref ( ..) ,
4060- ..
4061- } )
4062- )
4063- } ) ;
4064- }
4065- hir:: intravisit:: walk_pat ( self , pat) ;
4050+ fn visit_pat ( & mut self , pat : & ' tcx hir:: Pat < ' tcx > ) {
4051+ if !self . is_immut_ref_pattern
4052+ && let hir:: PatKind :: Binding ( _, _, ident, _) = pat. kind
4053+ && ident. span == self . binding_span
4054+ && matches ! (
4055+ self . tcx. parent_hir_node( pat. hir_id) ,
4056+ hir:: Node :: Pat ( hir:: Pat {
4057+ kind: hir:: PatKind :: Ref ( _, _, hir:: Mutability :: Not ) ,
4058+ ..
4059+ } )
4060+ )
4061+ {
4062+ self . is_immut_ref_pattern = true ;
40664063 }
4064+ hir:: intravisit:: walk_pat ( self , pat) ;
40674065 }
4066+ }
40684067
4069- let mut finder = RefPatternFinder {
4070- tcx : self . infcx . tcx ,
4071- binding_span : decl. source_info . span ,
4072- is_ref_pattern : false ,
4073- } ;
4068+ let mut finder = RefPatternFinder {
4069+ tcx : self . infcx . tcx ,
4070+ binding_span : decl. source_info . span ,
4071+ is_immut_ref_pattern : false ,
4072+ } ;
40744073
4075- finder. visit_body ( body) ;
4076- is_ref_pattern = finder. is_ref_pattern ;
4077- }
4074+ finder. visit_body ( body) ;
4075+ is_immut_ref_pattern = finder. is_immut_ref_pattern ;
40784076 }
40794077 }
40804078
4081- let ( span, message) = if is_for_loop
4082- && is_ref_pattern
4079+ let ( span, message) = if is_immut_ref_pattern
40834080 && let Ok ( binding_name) =
40844081 self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( decl. source_info . span )
40854082 {
0 commit comments