Skip to content

Commit 62b6aea

Browse files
committed
Add testcase for side-effectful reassign
1 parent 6c90230 commit 62b6aea

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

tests/ui/field_reassign_with_default.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ struct B {
1111
j: i64,
1212
}
1313

14+
/// Implements .next() that returns a different number each time.
15+
struct SideEffect(i32);
16+
17+
impl SideEffect {
18+
fn new() -> SideEffect { SideEffect(0) }
19+
fn next(&mut self) -> i32 {
20+
self.0 += 1;
21+
self.0
22+
}
23+
}
24+
1425
fn main() {
1526
// wrong, produces first error in stderr
1627
let mut a: A = Default::default();
@@ -86,4 +97,11 @@ fn main() {
8697
let mut x = A::default();
8798
x.i = 42;
8899
x.j = 21 + x.i as i64;
100+
101+
// right, we bail out if there's a reassignment to the same variable, since there is a risk of side-effects affecting the outcome
102+
let mut x = A::default();
103+
let mut side_effect = SideEffect::new();
104+
x.i = side_effect.next();
105+
x.j = 2;
106+
x.i = side_effect.next();
89107
}

tests/ui/field_reassign_with_default.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
11
error: field assignment outside of initializer for an instance created with Default::default()
2-
--> $DIR/field_reassign_with_default.rs:17:5
2+
--> $DIR/field_reassign_with_default.rs:28:5
33
|
44
LL | a.i = 42;
55
| ^^^^^^^^^
66
|
77
= note: `-D clippy::field-reassign-with-default` implied by `-D warnings`
88
note: consider initializing the variable with `A { i: 42, ..Default::default() }` and removing relevant reassignments
9-
--> $DIR/field_reassign_with_default.rs:16:5
9+
--> $DIR/field_reassign_with_default.rs:27:5
1010
|
1111
LL | let mut a: A = Default::default();
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

1414
error: field assignment outside of initializer for an instance created with Default::default()
15-
--> $DIR/field_reassign_with_default.rs:57:5
15+
--> $DIR/field_reassign_with_default.rs:68:5
1616
|
1717
LL | a.j = 43;
1818
| ^^^^^^^^^
1919
|
2020
note: consider initializing the variable with `A { j: 43, i: 42 }` and removing relevant reassignments
21-
--> $DIR/field_reassign_with_default.rs:56:5
21+
--> $DIR/field_reassign_with_default.rs:67:5
2222
|
2323
LL | let mut a: A = Default::default();
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525

2626
error: field assignment outside of initializer for an instance created with Default::default()
27-
--> $DIR/field_reassign_with_default.rs:62:5
27+
--> $DIR/field_reassign_with_default.rs:73:5
2828
|
2929
LL | a.i = 42;
3030
| ^^^^^^^^^
3131
|
3232
note: consider initializing the variable with `A { i: 42, j: 44 }` and removing relevant reassignments
33-
--> $DIR/field_reassign_with_default.rs:61:5
33+
--> $DIR/field_reassign_with_default.rs:72:5
3434
|
3535
LL | let mut a: A = Default::default();
3636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3737

3838
error: field assignment outside of initializer for an instance created with Default::default()
39-
--> $DIR/field_reassign_with_default.rs:68:5
39+
--> $DIR/field_reassign_with_default.rs:79:5
4040
|
4141
LL | a.i = 42;
4242
| ^^^^^^^^^
4343
|
4444
note: consider initializing the variable with `A { i: 42, ..Default::default() }` and removing relevant reassignments
45-
--> $DIR/field_reassign_with_default.rs:67:5
45+
--> $DIR/field_reassign_with_default.rs:78:5
4646
|
4747
LL | let mut a = A::default();
4848
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4949

5050
error: field assignment outside of initializer for an instance created with Default::default()
51-
--> $DIR/field_reassign_with_default.rs:78:5
51+
--> $DIR/field_reassign_with_default.rs:89:5
5252
|
5353
LL | a.i = Default::default();
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5555
|
5656
note: consider initializing the variable with `A::default()` and removing relevant reassignments
57-
--> $DIR/field_reassign_with_default.rs:77:5
57+
--> $DIR/field_reassign_with_default.rs:88:5
5858
|
5959
LL | let mut a: A = Default::default();
6060
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6161

6262
error: field assignment outside of initializer for an instance created with Default::default()
63-
--> $DIR/field_reassign_with_default.rs:82:5
63+
--> $DIR/field_reassign_with_default.rs:93:5
6464
|
6565
LL | a.i = Default::default();
6666
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6767
|
6868
note: consider initializing the variable with `A { j: 45, ..Default::default() }` and removing relevant reassignments
69-
--> $DIR/field_reassign_with_default.rs:81:5
69+
--> $DIR/field_reassign_with_default.rs:92:5
7070
|
7171
LL | let mut a: A = Default::default();
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)