@@ -49,13 +49,16 @@ pub(super) fn pat_from_hir<'a, 'tcx>(
49
49
tcx,
50
50
typing_env,
51
51
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
+ } ) ,
59
62
} ;
60
63
let result = pcx. lower_pattern ( pat) ;
61
64
debug ! ( "pat_from_hir({:?}) = {:?}" , pat, result) ;
@@ -107,27 +110,22 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
107
110
if let Some ( s) = & mut self . rust_2024_migration_suggestion
108
111
&& !adjustments. is_empty ( )
109
112
{
110
- let mut min_mutbl = Mutability :: Mut ;
111
- let suggestion_str: String = adjustments
112
- . iter ( )
113
- . map ( |ref_ty| {
114
- let & ty:: Ref ( _, _, mutbl) = ref_ty. kind ( ) else {
115
- span_bug ! ( pat. span, "pattern implicitly dereferences a non-ref type" ) ;
116
- } ;
117
-
118
- match mutbl {
119
- Mutability :: Not => {
120
- min_mutbl = Mutability :: Not ;
121
- "&"
122
- }
123
- Mutability :: Mut => "&mut " ,
124
- }
125
- } )
126
- . collect ( ) ;
127
- s. suggestion . push ( ( pat. span . shrink_to_lo ( ) , suggestion_str) ) ;
128
- s. ref_pattern_count += adjustments. len ( ) ;
113
+ let implicit_deref_mutbls = adjustments. iter ( ) . map ( |ref_ty| {
114
+ let & ty:: Ref ( _, _, mutbl) = ref_ty. kind ( ) else {
115
+ span_bug ! ( pat. span, "pattern implicitly dereferences a non-ref type" ) ;
116
+ } ;
117
+ mutbl
118
+ } ) ;
119
+
120
+ if !s. suggest_eliding_modes {
121
+ let suggestion_str: String =
122
+ implicit_deref_mutbls. clone ( ) . map ( |mutbl| mutbl. ref_prefix_str ( ) ) . collect ( ) ;
123
+ s. suggestion . push ( ( pat. span . shrink_to_lo ( ) , suggestion_str) ) ;
124
+ s. ref_pattern_count += adjustments. len ( ) ;
125
+ }
129
126
130
127
// Remember if this changed the default binding mode, in case we want to label it.
128
+ let min_mutbl = implicit_deref_mutbls. min ( ) . unwrap ( ) ;
131
129
if s. default_mode_span . is_none_or ( |( _, old_mutbl) | min_mutbl < old_mutbl) {
132
130
opt_old_mode_span = Some ( s. default_mode_span ) ;
133
131
s. default_mode_span = Some ( ( pat. span , min_mutbl) ) ;
@@ -413,8 +411,14 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
413
411
{
414
412
// If this overrides a by-ref default binding mode, label the binding mode.
415
413
s. default_mode_labels . insert ( default_mode_span, default_ref_mutbl) ;
414
+ // If our suggestion is to elide redundnt modes, this will be one of them.
415
+ if s. suggest_eliding_modes {
416
+ s. suggestion . push ( ( pat. span . with_hi ( ident. span . lo ( ) ) , String :: new ( ) ) ) ;
417
+ s. binding_mode_count += 1 ;
418
+ }
416
419
}
417
- if explicit_ba. 0 == ByRef :: No
420
+ if !s. suggest_eliding_modes
421
+ && explicit_ba. 0 == ByRef :: No
418
422
&& let ByRef :: Yes ( mutbl) = mode. 0
419
423
{
420
424
let sugg_str = match mutbl {
0 commit comments