Skip to content

Commit 6004968

Browse files
committed
tests: Check stderr when trying to mutate inside Option::inspect()
1 parent d6deffe commit 6004968

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/128381>.
2+
3+
struct Struct {
4+
field: u32,
5+
}
6+
7+
fn main() {
8+
let mut some_struct = Some(Struct { field: 42 });
9+
some_struct.as_mut().inspect(|some_struct| {
10+
some_struct.field *= 10; //~ ERROR cannot assign to `some_struct.field`, which is behind a `&` reference
11+
// Users can't change type of `some_struct` param, so above error must not suggest it.
12+
});
13+
14+
// Same check as above but using `hir::ExprKind::Call` instead of `hir::ExprKind::MethodCall`.
15+
Option::inspect(some_struct.as_mut(), |some_struct| {
16+
some_struct.field *= 20; //~ ERROR cannot assign to `some_struct.field`, which is behind a `&` reference
17+
});
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0594]: cannot assign to `some_struct.field`, which is behind a `&` reference
2+
--> $DIR/option-inspect-mutation.rs:10:9
3+
|
4+
LL | some_struct.as_mut().inspect(|some_struct| {
5+
| ----------- consider changing this binding's type to be: `&mut &mut Struct`
6+
LL | some_struct.field *= 10;
7+
| ^^^^^^^^^^^^^^^^^^^^^^^ `some_struct` is a `&` reference, so it cannot be written to
8+
9+
error[E0594]: cannot assign to `some_struct.field`, which is behind a `&` reference
10+
--> $DIR/option-inspect-mutation.rs:16:9
11+
|
12+
LL | Option::inspect(some_struct.as_mut(), |some_struct| {
13+
| ----------- consider changing this binding's type to be: `&mut &mut Struct`
14+
LL | some_struct.field *= 20;
15+
| ^^^^^^^^^^^^^^^^^^^^^^^ `some_struct` is a `&` reference, so it cannot be written to
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)