@@ -49,13 +49,16 @@ pub(super) fn pat_from_hir<'a, 'tcx>(
4949 tcx,
5050 typing_env,
5151 typeck_results,
52- rust_2024_migration_suggestion : migration_info. and ( Some ( Rust2024IncompatiblePatSugg {
53- suggestion : Vec :: new ( ) ,
54- ref_pattern_count : 0 ,
55- binding_mode_count : 0 ,
56- default_mode_span : None ,
57- default_mode_labels : Default :: default ( ) ,
58- } ) ) ,
52+ rust_2024_migration_suggestion : migration_info. and_then ( |info| {
53+ Some ( Rust2024IncompatiblePatSugg {
54+ suggest_eliding_modes : info. suggest_eliding_modes ,
55+ suggestion : Vec :: new ( ) ,
56+ ref_pattern_count : 0 ,
57+ binding_mode_count : 0 ,
58+ default_mode_span : None ,
59+ default_mode_labels : Default :: default ( ) ,
60+ } )
61+ } ) ,
5962 } ;
6063 let result = pcx. lower_pattern ( pat) ;
6164 debug ! ( "pat_from_hir({:?}) = {:?}" , pat, result) ;
@@ -106,27 +109,22 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
106109 if let Some ( s) = & mut self . rust_2024_migration_suggestion
107110 && !adjustments. is_empty ( )
108111 {
109- let mut min_mutbl = Mutability :: Mut ;
110- let suggestion_str: String = adjustments
111- . iter ( )
112- . map ( |ref_ty| {
113- let & ty:: Ref ( _, _, mutbl) = ref_ty. kind ( ) else {
114- span_bug ! ( pat. span, "pattern implicitly dereferences a non-ref type" ) ;
115- } ;
116-
117- match mutbl {
118- Mutability :: Not => {
119- min_mutbl = Mutability :: Not ;
120- "&"
121- }
122- Mutability :: Mut => "&mut " ,
123- }
124- } )
125- . collect ( ) ;
126- s. suggestion . push ( ( pat. span . shrink_to_lo ( ) , suggestion_str) ) ;
127- s. ref_pattern_count += adjustments. len ( ) ;
112+ let implicit_deref_mutbls = adjustments. iter ( ) . map ( |ref_ty| {
113+ let & ty:: Ref ( _, _, mutbl) = ref_ty. kind ( ) else {
114+ span_bug ! ( pat. span, "pattern implicitly dereferences a non-ref type" ) ;
115+ } ;
116+ mutbl
117+ } ) ;
118+
119+ if !s. suggest_eliding_modes {
120+ let suggestion_str: String =
121+ implicit_deref_mutbls. clone ( ) . map ( |mutbl| mutbl. ref_prefix_str ( ) ) . collect ( ) ;
122+ s. suggestion . push ( ( pat. span . shrink_to_lo ( ) , suggestion_str) ) ;
123+ s. ref_pattern_count += adjustments. len ( ) ;
124+ }
128125
129126 // Remember if this changed the default binding mode, in case we want to label it.
127+ let min_mutbl = implicit_deref_mutbls. min ( ) . unwrap ( ) ;
130128 if s. default_mode_span . is_none_or ( |( _, old_mutbl) | min_mutbl < old_mutbl) {
131129 opt_old_mode_span = Some ( s. default_mode_span ) ;
132130 s. default_mode_span = Some ( ( pat. span , min_mutbl) ) ;
@@ -412,8 +410,14 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
412410 {
413411 // If this overrides a by-ref default binding mode, label the binding mode.
414412 s. default_mode_labels . insert ( default_mode_span, default_ref_mutbl) ;
413+ // If our suggestion is to elide redundnt modes, this will be one of them.
414+ if s. suggest_eliding_modes {
415+ s. suggestion . push ( ( pat. span . with_hi ( ident. span . lo ( ) ) , String :: new ( ) ) ) ;
416+ s. binding_mode_count += 1 ;
417+ }
415418 }
416- if explicit_ba. 0 == ByRef :: No
419+ if !s. suggest_eliding_modes
420+ && explicit_ba. 0 == ByRef :: No
417421 && let ByRef :: Yes ( mutbl) = mode. 0
418422 {
419423 let sugg_str = match mutbl {
0 commit comments