@@ -152,21 +152,26 @@ fn report_single_pattern(
152
152
} ) if lit. node . is_str ( ) || lit. node . is_bytestr ( ) => pat_ref_count + 1 ,
153
153
_ => pat_ref_count,
154
154
} ;
155
- // References are only implicitly added to the pattern, so no overflow here.
156
- // e.g. will work: match &Some(_) { Some(_) => () }
157
- // will not: match Some(_) { &Some(_) => () }
158
- let ref_count_diff = ty_ref_count - pat_ref_count;
159
155
160
- // Try to remove address of expressions first.
161
- let ( ex, removed) = peel_n_hir_expr_refs ( ex, ref_count_diff) ;
162
- let ref_count_diff = ref_count_diff - removed;
156
+ // References are implicitly removed when `deref_patterns` are used.
157
+ // They are implicitly added when match ergonomics are used.
158
+ let ( ex, ref_or_deref_adjust) = if ty_ref_count > pat_ref_count {
159
+ let ref_count_diff = ty_ref_count - pat_ref_count;
160
+
161
+ // Try to remove address of expressions first.
162
+ let ( ex, removed) = peel_n_hir_expr_refs ( ex, ref_count_diff) ;
163
+
164
+ ( ex, "&" . repeat ( ref_count_diff - removed) )
165
+ } else {
166
+ ( ex, "*" . repeat ( pat_ref_count - ty_ref_count) )
167
+ } ;
163
168
164
169
let msg = "you seem to be trying to use `match` for an equality check. Consider using `if`" ;
165
170
let sugg = format ! (
166
171
"if {} == {}{} {}{els_str}" ,
167
172
snippet_with_context( cx, ex. span, ctxt, ".." , & mut app) . 0 ,
168
173
// PartialEq for different reference counts may not exist.
169
- "&" . repeat ( ref_count_diff ) ,
174
+ ref_or_deref_adjust ,
170
175
snippet_with_applicability( cx, arm. pat. span, ".." , & mut app) ,
171
176
expr_block( cx, arm. body, ctxt, ".." , Some ( expr. span) , & mut app) ,
172
177
) ;
0 commit comments