Skip to content

Commit 4f7c257

Browse files
committed
Do not suggest borrowing binding in pattern in let else
Fix #104838.
1 parent e46416e commit 4f7c257

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

compiler/rustc_mir_build/src/build/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
231231
remainder_span,
232232
pattern,
233233
None,
234-
Some((None, initializer_span)),
234+
Some((Some(&destination), initializer_span)),
235235
);
236236
this.visit_primary_bindings(
237237
pattern,

src/test/ui/by-move-pattern-binding.stderr

+9-5
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ LL + if let E::Bar(identifier) = &s.x {
3232
|
3333

3434
error[E0507]: cannot move out of a shared reference
35-
--> $DIR/by-move-pattern-binding.rs:25:17
35+
--> $DIR/by-move-pattern-binding.rs:25:31
3636
|
3737
LL | let &E::Bar(identifier) = &s.x else {
38-
| ^^^^^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
38+
| ---------- ^^^^
39+
| |
40+
| data moved here
41+
| move occurs because `identifier` has type `String`, which does not implement the `Copy` trait
42+
|
43+
help: consider removing the borrow
3944
|
40-
help: consider borrowing here
45+
LL - let &E::Bar(identifier) = &s.x else {
46+
LL + let E::Bar(identifier) = &s.x else {
4147
|
42-
LL | let &E::Bar(&identifier) = &s.x else {
43-
| +
4448

4549
error: aborting due to 3 previous errors
4650

src/test/ui/suggestions/dont-suggest-ref/simple.rs

-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ struct Testing {
319319
fn testing(a: &Testing) {
320320
let Some(_s) = a.a else {
321321
//~^ ERROR cannot move
322-
//~| HELP consider borrowing
323322
return;
324323
};
325324
}

src/test/ui/suggestions/dont-suggest-ref/simple.stderr

+5-7
Original file line numberDiff line numberDiff line change
@@ -902,15 +902,13 @@ LL | fn f4((&mut X(_t),): (&mut X,)) { }
902902
| move occurs because `_t` has type `Y`, which does not implement the `Copy` trait
903903

904904
error[E0507]: cannot move out of `a.a` as enum variant `Some` which is behind a shared reference
905-
--> $DIR/simple.rs:320:14
905+
--> $DIR/simple.rs:320:20
906906
|
907907
LL | let Some(_s) = a.a else {
908-
| ^^ move occurs because `a.a.0` has type `String`, which does not implement the `Copy` trait
909-
|
910-
help: consider borrowing here
911-
|
912-
LL | let Some(&_s) = a.a else {
913-
| +
908+
| -- ^^^
909+
| |
910+
| data moved here
911+
| move occurs because `_s` has type `String`, which does not implement the `Copy` trait
914912

915913
error: aborting due to 61 previous errors
916914

0 commit comments

Comments
 (0)