Skip to content

Commit 1498da8

Browse files
authored
Rollup merge of rust-lang#70927 - GuillaumeGomez:cleanup-e0510, r=Dylan-DPC
Clean up E0510 explanation r? @Dylan-DPC
2 parents bad8f0b + 80e3126 commit 1498da8

File tree

1 file changed

+20
-7
lines changed
  • src/librustc_error_codes/error_codes

1 file changed

+20
-7
lines changed
+20-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
Cannot mutate place in this match guard.
1+
The matched value was assigned in a match guard.
22

3-
When matching on a variable it cannot be mutated in the match guards, as this
4-
could cause the match to be non-exhaustive:
3+
Erroneous code example:
54

65
```compile_fail,E0510
76
let mut x = Some(0);
87
match x {
9-
None => (),
10-
Some(_) if { x = None; false } => (),
11-
Some(v) => (), // No longer matches
8+
None => {}
9+
Some(_) if { x = None; false } => {} // error!
10+
Some(_) => {}
1211
}
1312
```
1413

14+
When matching on a variable it cannot be mutated in the match guards, as this
15+
could cause the match to be non-exhaustive.
16+
1517
Here executing `x = None` would modify the value being matched and require us
16-
to go "back in time" to the `None` arm.
18+
to go "back in time" to the `None` arm. To fix it, change the value in the match
19+
arm:
20+
21+
```
22+
let mut x = Some(0);
23+
match x {
24+
None => {}
25+
Some(_) => {
26+
x = None; // ok!
27+
}
28+
}
29+
```

0 commit comments

Comments
 (0)