Skip to content

Commit cc1dee1

Browse files
committed
Fix incorrect suggestion for extra & in pattern
see #106182
1 parent cca80b9 commit cc1dee1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+17
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
758758
err.span_note(sp, format!("{msg}: `{sugg}`"));
759759
}
760760
}
761+
hir::Node::Pat(pt) if let PatKind::TupleStruct(_, pat_arr, _) = pt.kind => {
762+
for i in pat_arr.iter() {
763+
if let PatKind::Ref(the_ref, _) = i.kind
764+
&& let PatKind::Binding(mt, _, ident, _) = the_ref.kind {
765+
let hir::BindingAnnotation(_, mtblty) = mt;
766+
err.span_suggestion_verbose(
767+
i.span,
768+
format!("consider removing `&{mutability}` from the pattern"),
769+
mtblty.prefix_str().to_string() + &ident.name.to_string(),
770+
Applicability::MaybeIncorrect,
771+
);
772+
}
773+
}
774+
if let Some((sp, msg, sugg)) = mut_var_suggestion {
775+
err.span_note(sp, format!("{msg}: `{sugg}`"));
776+
}
777+
}
761778
hir::Node::Param(_) | hir::Node::Arm(_) | hir::Node::Pat(_) => {
762779
// rely on match ergonomics or it might be nested `&&pat`
763780
err.span_suggestion_verbose(

src/test/ui/mismatched_types/ref-pat-suggestions.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,8 @@ LL | let S(&mut _b) = S(0);
336336
| ^^^^^^^
337337
help: consider removing `&mut` from the pattern
338338
|
339-
LL - let S(&mut _b) = S(0);
340-
LL + let S(_b) = S(0);
341-
|
339+
LL | let S(_b) = S(0);
340+
| ~~
342341

343342
error[E0308]: mismatched types
344343
--> $DIR/ref-pat-suggestions.rs:31:14

0 commit comments

Comments
 (0)