Skip to content

Commit 69691da

Browse files
committed
Add test for mut arg: &Ty meant to be arg: &mut Ty
This is a mistake I've seen newcomers make where they want to express an "out" argument.
1 parent 7a0cde9 commit 69691da

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![deny(unused_assignments, unused_variables)]
2+
struct Object;
3+
4+
fn change_object(mut object: &Object) {
5+
let object2 = Object;
6+
object = object2; //~ ERROR mismatched types
7+
}
8+
9+
fn change_object2(mut object: &Object) { //~ ERROR variable `object` is assigned to, but never used
10+
let object2 = Object;
11+
object = &object2;
12+
//~^ ERROR `object2` does not live long enough
13+
//~| ERROR value assigned to `object` is never read
14+
}
15+
16+
fn main() {
17+
let object = Object;
18+
change_object(&object);
19+
change_object2(&object);
20+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:6:14
3+
|
4+
LL | fn change_object(mut object: &Object) {
5+
| ------- expected due to this parameter type
6+
LL | let object2 = Object;
7+
LL | object = object2;
8+
| ^^^^^^^ expected `&Object`, found `Object`
9+
|
10+
help: consider borrowing here
11+
|
12+
LL | object = &object2;
13+
| +
14+
15+
error: value assigned to `object` is never read
16+
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:11:5
17+
|
18+
LL | object = &object2;
19+
| ^^^^^^
20+
|
21+
= help: maybe it is overwritten before being read?
22+
note: the lint level is defined here
23+
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:1:9
24+
|
25+
LL | #![deny(unused_assignments, unused_variables)]
26+
| ^^^^^^^^^^^^^^^^^^
27+
28+
error: variable `object` is assigned to, but never used
29+
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:9:23
30+
|
31+
LL | fn change_object2(mut object: &Object) {
32+
| ^^^^^^
33+
|
34+
= note: consider using `_object` instead
35+
note: the lint level is defined here
36+
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:1:29
37+
|
38+
LL | #![deny(unused_assignments, unused_variables)]
39+
| ^^^^^^^^^^^^^^^^
40+
41+
error[E0597]: `object2` does not live long enough
42+
--> $DIR/mut-arg-of-borrowed-type-meant-to-be-arg-of-mut-borrow.rs:11:14
43+
|
44+
LL | fn change_object2(mut object: &Object) {
45+
| - let's call the lifetime of this reference `'1`
46+
LL | let object2 = Object;
47+
| ------- binding `object2` declared here
48+
LL | object = &object2;
49+
| ---------^^^^^^^^
50+
| | |
51+
| | borrowed value does not live long enough
52+
| assignment requires that `object2` is borrowed for `'1`
53+
...
54+
LL | }
55+
| - `object2` dropped here while still borrowed
56+
57+
error: aborting due to 4 previous errors
58+
59+
Some errors have detailed explanations: E0308, E0597.
60+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)