Skip to content

Commit 37c1d6d

Browse files
authored
Rollup merge of #106291 - obeis:issue-106182, r=oli-obk
Fix incorrect suggestion for extra `&` in pattern Closes #106182
2 parents 7779386 + a74a488 commit 37c1d6d

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+17
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
761761
err.span_note(sp, format!("{msg}: `{sugg}`"));
762762
}
763763
}
764+
hir::Node::Pat(pt) if let PatKind::TupleStruct(_, pat_arr, _) = pt.kind => {
765+
for i in pat_arr.iter() {
766+
if let PatKind::Ref(the_ref, _) = i.kind
767+
&& let PatKind::Binding(mt, _, ident, _) = the_ref.kind {
768+
let hir::BindingAnnotation(_, mtblty) = mt;
769+
err.span_suggestion_verbose(
770+
i.span,
771+
format!("consider removing `&{mutability}` from the pattern"),
772+
mtblty.prefix_str().to_string() + &ident.name.to_string(),
773+
Applicability::MaybeIncorrect,
774+
);
775+
}
776+
}
777+
if let Some((sp, msg, sugg)) = mut_var_suggestion {
778+
err.span_note(sp, format!("{msg}: `{sugg}`"));
779+
}
780+
}
764781
hir::Node::Param(_) | hir::Node::Arm(_) | hir::Node::Pat(_) => {
765782
// rely on match ergonomics or it might be nested `&&pat`
766783
err.span_suggestion_verbose(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
struct _S(u32, Vec<i32>);
4+
5+
fn _foo(x: &_S) {
6+
match x {
7+
_S(mut _y, _v) => {
8+
//~^ ERROR mismatched types [E0308]
9+
}
10+
}
11+
}
12+
13+
fn main() {
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
struct _S(u32, Vec<i32>);
4+
5+
fn _foo(x: &_S) {
6+
match x {
7+
_S(& (mut _y), _v) => {
8+
//~^ ERROR mismatched types [E0308]
9+
}
10+
}
11+
}
12+
13+
fn main() {
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-106182.rs:7:12
3+
|
4+
LL | match x {
5+
| - this expression has type `&_S`
6+
LL | _S(& (mut _y), _v) => {
7+
| ^^^^^^^^^^ expected `u32`, found reference
8+
|
9+
= note: expected type `u32`
10+
found reference `&_`
11+
help: consider removing `&` from the pattern
12+
|
13+
LL | _S(mut _y, _v) => {
14+
| ~~~~~~
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.

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)