Skip to content

Commit 00f3155

Browse files
committed
fix unused assigment issue for variable with drop, issue 148418
1 parent 4930d3e commit 00f3155

File tree

7 files changed

+120
-22
lines changed

7 files changed

+120
-22
lines changed

compiler/rustc_mir_transform/src/liveness.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,8 +981,10 @@ impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
981981
self.checked_places,
982982
self.body,
983983
) {
984-
statements.clear();
985-
continue;
984+
statements.retain(|_, access| access.is_direct);
985+
if statements.is_empty() {
986+
continue;
987+
}
986988
}
987989

988990
let typo = maybe_suggest_typo();

tests/ui/drop/or-pattern-drop-order.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ fn main() {
3333
// Drops are right-to-left: `z`, `y`, `x`.
3434
let (x, Ok(y) | Err(y), z);
3535
// Assignment order doesn't matter.
36-
z = LogDrop(o, 1);
37-
y = LogDrop(o, 2);
38-
x = LogDrop(o, 3);
36+
z = LogDrop(o, 1); //~ WARN value assigned to `z` is never read
37+
y = LogDrop(o, 2); //~ WARN value assigned to `y` is never read
38+
x = LogDrop(o, 3); //~ WARN value assigned to `x` is never read
3939
});
4040
assert_drop_order(1..=2, |o| {
4141
// The first or-pattern alternative determines the bindings' drop order: `y`, `x`.
4242
let ((true, x, y) | (false, y, x));
43-
x = LogDrop(o, 2);
44-
y = LogDrop(o, 1);
43+
x = LogDrop(o, 2); //~ WARN value assigned to `x` is never read
44+
y = LogDrop(o, 1); //~ WARN value assigned to `y` is never read
4545
});
4646

4747
// `let pat = expr;` should have the same drop order.
@@ -61,15 +61,21 @@ fn main() {
6161
// `match` should have the same drop order.
6262
assert_drop_order(1..=3, |o| {
6363
// Drops are right-to-left: `z`, `y` `x`.
64-
match (LogDrop(o, 3), Ok(LogDrop(o, 2)), LogDrop(o, 1)) { (x, Ok(y) | Err(y), z) => {} }
64+
match (LogDrop(o, 3), Ok(LogDrop(o, 2)), LogDrop(o, 1)) {
65+
(x, Ok(y) | Err(y), z) => {}
66+
}
6567
});
6668
assert_drop_order(1..=2, |o| {
6769
// The first or-pattern alternative determines the bindings' drop order: `y`, `x`.
68-
match (true, LogDrop(o, 2), LogDrop(o, 1)) { (true, x, y) | (false, y, x) => {} }
70+
match (true, LogDrop(o, 2), LogDrop(o, 1)) {
71+
(true, x, y) | (false, y, x) => {}
72+
}
6973
});
7074
assert_drop_order(1..=2, |o| {
7175
// That drop order is used regardless of which or-pattern alternative matches: `y`, `x`.
72-
match (false, LogDrop(o, 1), LogDrop(o, 2)) { (true, x, y) | (false, y, x) => {} }
76+
match (false, LogDrop(o, 1), LogDrop(o, 2)) {
77+
(true, x, y) | (false, y, x) => {}
78+
}
7379
});
7480

7581
// Function params are visited one-by-one, and the order of bindings within a param's pattern is
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
warning: value assigned to `x` is never read
2+
--> $DIR/or-pattern-drop-order.rs:43:9
3+
|
4+
LL | x = LogDrop(o, 2);
5+
| ^
6+
|
7+
= help: maybe it is overwritten before being read?
8+
= note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default
9+
10+
warning: value assigned to `y` is never read
11+
--> $DIR/or-pattern-drop-order.rs:44:9
12+
|
13+
LL | y = LogDrop(o, 1);
14+
| ^
15+
|
16+
= help: maybe it is overwritten before being read?
17+
18+
warning: value assigned to `x` is never read
19+
--> $DIR/or-pattern-drop-order.rs:38:9
20+
|
21+
LL | x = LogDrop(o, 3);
22+
| ^
23+
|
24+
= help: maybe it is overwritten before being read?
25+
26+
warning: value assigned to `y` is never read
27+
--> $DIR/or-pattern-drop-order.rs:37:9
28+
|
29+
LL | y = LogDrop(o, 2);
30+
| ^
31+
|
32+
= help: maybe it is overwritten before being read?
33+
34+
warning: value assigned to `z` is never read
35+
--> $DIR/or-pattern-drop-order.rs:36:9
36+
|
37+
LL | z = LogDrop(o, 1);
38+
| ^
39+
|
40+
= help: maybe it is overwritten before being read?
41+
42+
warning: 5 warnings emitted
43+

tests/ui/lint/unused/unused-assign-148960.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ check-fail
22
#![deny(unused)]
3+
#![allow(dead_code)]
34

45
fn test_one_extra_assign() {
56
let mut value = b"0".to_vec(); //~ ERROR value assigned to `value` is never read
@@ -26,8 +27,16 @@ fn test_indirect_assign() {
2627
println!("{}", p.y);
2728
}
2829

29-
fn main() {
30-
test_one_extra_assign();
31-
test_two_extra_assign();
32-
test_indirect_assign();
30+
struct Foo;
31+
32+
impl Drop for Foo {
33+
fn drop(&mut self) {}
34+
}
35+
36+
// testcase for issue #148418
37+
fn test_unused_variable() {
38+
let mut foo = Foo; //~ ERROR variable `foo` is assigned to, but never used
39+
foo = Foo; //~ ERROR value assigned to `foo` is never read
3340
}
41+
42+
fn main() {}

tests/ui/lint/unused/unused-assign-148960.stderr

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: value assigned to `value` is never read
2-
--> $DIR/unused-assign-148960.rs:5:21
2+
--> $DIR/unused-assign-148960.rs:6:21
33
|
44
LL | let mut value = b"0".to_vec();
55
| ^^^^^^^^^^^^^
@@ -13,28 +13,50 @@ LL | #![deny(unused)]
1313
= note: `#[deny(unused_assignments)]` implied by `#[deny(unused)]`
1414

1515
error: value assigned to `x` is never read
16-
--> $DIR/unused-assign-148960.rs:11:17
16+
--> $DIR/unused-assign-148960.rs:12:17
1717
|
1818
LL | let mut x = 1;
1919
| ^
2020
|
2121
= help: maybe it is overwritten before being read?
2222

2323
error: value assigned to `x` is never read
24-
--> $DIR/unused-assign-148960.rs:12:5
24+
--> $DIR/unused-assign-148960.rs:13:5
2525
|
2626
LL | x = 2;
2727
| ^^^^^
2828
|
2929
= help: maybe it is overwritten before being read?
3030

3131
error: value assigned to `p` is never read
32-
--> $DIR/unused-assign-148960.rs:23:17
32+
--> $DIR/unused-assign-148960.rs:24:17
3333
|
3434
LL | let mut p = Point { x: 1, y: 1 };
3535
| ^^^^^^^^^^^^^^^^^^^^
3636
|
3737
= help: maybe it is overwritten before being read?
3838

39-
error: aborting due to 4 previous errors
39+
error: variable `foo` is assigned to, but never used
40+
--> $DIR/unused-assign-148960.rs:38:9
41+
|
42+
LL | let mut foo = Foo;
43+
| ^^^^^^^
44+
|
45+
= note: consider using `_foo` instead
46+
= note: `#[deny(unused_variables)]` implied by `#[deny(unused)]`
47+
help: you might have meant to pattern match on the similarly named struct `Foo`
48+
|
49+
LL - let mut foo = Foo;
50+
LL + let Foo = Foo;
51+
|
52+
53+
error: value assigned to `foo` is never read
54+
--> $DIR/unused-assign-148960.rs:39:5
55+
|
56+
LL | foo = Foo;
57+
| ^^^
58+
|
59+
= help: maybe it is overwritten before being read?
60+
61+
error: aborting due to 6 previous errors
4062

tests/ui/liveness/liveness-unused.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ fn f10<T>(mut a: T, b: T) {
244244
//~^ ERROR value assigned to `a` is never read
245245
}
246246

247-
fn f10b<T>(mut a: Box<T>, b: Box<T>) {
248-
a = b;
247+
fn f10b<T>(mut a: Box<T>, b: Box<T>) { //~ ERROR variable `a` is assigned to, but never used
248+
a = b; //~ ERROR value assigned to `a` is never read
249249
}
250250

251251
// unused params warnings are not needed for intrinsic functions without bodies

tests/ui/liveness/liveness-unused.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,21 @@ LL | a = b;
283283
|
284284
= help: maybe it is overwritten before being read?
285285

286-
error: aborting due to 34 previous errors; 1 warning emitted
286+
error: variable `a` is assigned to, but never used
287+
--> $DIR/liveness-unused.rs:247:12
288+
|
289+
LL | fn f10b<T>(mut a: Box<T>, b: Box<T>) {
290+
| ^^^^^
291+
|
292+
= note: consider using `_a` instead
293+
294+
error: value assigned to `a` is never read
295+
--> $DIR/liveness-unused.rs:248:5
296+
|
297+
LL | a = b;
298+
| ^
299+
|
300+
= help: maybe it is overwritten before being read?
301+
302+
error: aborting due to 36 previous errors; 1 warning emitted
287303

0 commit comments

Comments
 (0)