fix unused assigment issue for variable with drop, issue 148418

(cherry picked from commit 00f3155794)
This commit is contained in:
yukang 2025-11-20 19:47:21 +08:00 committed by Josh Stone
parent 1087de60a9
commit 807097675a
7 changed files with 120 additions and 22 deletions

View file

@ -982,8 +982,10 @@ impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
self.checked_places,
self.body,
) {
statements.clear();
continue;
statements.retain(|_, access| access.is_direct);
if statements.is_empty() {
continue;
}
}
let typo = maybe_suggest_typo();

View file

@ -33,15 +33,15 @@ fn main() {
// Drops are right-to-left: `z`, `y`, `x`.
let (x, Ok(y) | Err(y), z);
// Assignment order doesn't matter.
z = LogDrop(o, 1);
y = LogDrop(o, 2);
x = LogDrop(o, 3);
z = LogDrop(o, 1); //~ WARN value assigned to `z` is never read
y = LogDrop(o, 2); //~ WARN value assigned to `y` is never read
x = LogDrop(o, 3); //~ WARN value assigned to `x` is never read
});
assert_drop_order(1..=2, |o| {
// The first or-pattern alternative determines the bindings' drop order: `y`, `x`.
let ((true, x, y) | (false, y, x));
x = LogDrop(o, 2);
y = LogDrop(o, 1);
x = LogDrop(o, 2); //~ WARN value assigned to `x` is never read
y = LogDrop(o, 1); //~ WARN value assigned to `y` is never read
});
// `let pat = expr;` should have the same drop order.
@ -61,15 +61,21 @@ fn main() {
// `match` should have the same drop order.
assert_drop_order(1..=3, |o| {
// Drops are right-to-left: `z`, `y` `x`.
match (LogDrop(o, 3), Ok(LogDrop(o, 2)), LogDrop(o, 1)) { (x, Ok(y) | Err(y), z) => {} }
match (LogDrop(o, 3), Ok(LogDrop(o, 2)), LogDrop(o, 1)) {
(x, Ok(y) | Err(y), z) => {}
}
});
assert_drop_order(1..=2, |o| {
// The first or-pattern alternative determines the bindings' drop order: `y`, `x`.
match (true, LogDrop(o, 2), LogDrop(o, 1)) { (true, x, y) | (false, y, x) => {} }
match (true, LogDrop(o, 2), LogDrop(o, 1)) {
(true, x, y) | (false, y, x) => {}
}
});
assert_drop_order(1..=2, |o| {
// That drop order is used regardless of which or-pattern alternative matches: `y`, `x`.
match (false, LogDrop(o, 1), LogDrop(o, 2)) { (true, x, y) | (false, y, x) => {} }
match (false, LogDrop(o, 1), LogDrop(o, 2)) {
(true, x, y) | (false, y, x) => {}
}
});
// Function params are visited one-by-one, and the order of bindings within a param's pattern is

View file

@ -0,0 +1,43 @@
warning: value assigned to `x` is never read
--> $DIR/or-pattern-drop-order.rs:43:9
|
LL | x = LogDrop(o, 2);
| ^
|
= help: maybe it is overwritten before being read?
= note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default
warning: value assigned to `y` is never read
--> $DIR/or-pattern-drop-order.rs:44:9
|
LL | y = LogDrop(o, 1);
| ^
|
= help: maybe it is overwritten before being read?
warning: value assigned to `x` is never read
--> $DIR/or-pattern-drop-order.rs:38:9
|
LL | x = LogDrop(o, 3);
| ^
|
= help: maybe it is overwritten before being read?
warning: value assigned to `y` is never read
--> $DIR/or-pattern-drop-order.rs:37:9
|
LL | y = LogDrop(o, 2);
| ^
|
= help: maybe it is overwritten before being read?
warning: value assigned to `z` is never read
--> $DIR/or-pattern-drop-order.rs:36:9
|
LL | z = LogDrop(o, 1);
| ^
|
= help: maybe it is overwritten before being read?
warning: 5 warnings emitted

View file

@ -1,5 +1,6 @@
//@ check-fail
#![deny(unused)]
#![allow(dead_code)]
fn test_one_extra_assign() {
let mut value = b"0".to_vec(); //~ ERROR value assigned to `value` is never read
@ -26,8 +27,16 @@ fn test_indirect_assign() {
println!("{}", p.y);
}
fn main() {
test_one_extra_assign();
test_two_extra_assign();
test_indirect_assign();
struct Foo;
impl Drop for Foo {
fn drop(&mut self) {}
}
// testcase for issue #148418
fn test_unused_variable() {
let mut foo = Foo; //~ ERROR variable `foo` is assigned to, but never used
foo = Foo; //~ ERROR value assigned to `foo` is never read
}
fn main() {}

View file

@ -1,5 +1,5 @@
error: value assigned to `value` is never read
--> $DIR/unused-assign-148960.rs:5:21
--> $DIR/unused-assign-148960.rs:6:21
|
LL | let mut value = b"0".to_vec();
| ^^^^^^^^^^^^^
@ -13,7 +13,7 @@ LL | #![deny(unused)]
= note: `#[deny(unused_assignments)]` implied by `#[deny(unused)]`
error: value assigned to `x` is never read
--> $DIR/unused-assign-148960.rs:11:17
--> $DIR/unused-assign-148960.rs:12:17
|
LL | let mut x = 1;
| ^
@ -21,7 +21,7 @@ LL | let mut x = 1;
= help: maybe it is overwritten before being read?
error: value assigned to `x` is never read
--> $DIR/unused-assign-148960.rs:12:5
--> $DIR/unused-assign-148960.rs:13:5
|
LL | x = 2;
| ^^^^^
@ -29,12 +29,34 @@ LL | x = 2;
= help: maybe it is overwritten before being read?
error: value assigned to `p` is never read
--> $DIR/unused-assign-148960.rs:23:17
--> $DIR/unused-assign-148960.rs:24:17
|
LL | let mut p = Point { x: 1, y: 1 };
| ^^^^^^^^^^^^^^^^^^^^
|
= help: maybe it is overwritten before being read?
error: aborting due to 4 previous errors
error: variable `foo` is assigned to, but never used
--> $DIR/unused-assign-148960.rs:38:9
|
LL | let mut foo = Foo;
| ^^^^^^^
|
= note: consider using `_foo` instead
= note: `#[deny(unused_variables)]` implied by `#[deny(unused)]`
help: you might have meant to pattern match on the similarly named struct `Foo`
|
LL - let mut foo = Foo;
LL + let Foo = Foo;
|
error: value assigned to `foo` is never read
--> $DIR/unused-assign-148960.rs:39:5
|
LL | foo = Foo;
| ^^^
|
= help: maybe it is overwritten before being read?
error: aborting due to 6 previous errors

View file

@ -244,8 +244,8 @@ fn f10<T>(mut a: T, b: T) {
//~^ ERROR value assigned to `a` is never read
}
fn f10b<T>(mut a: Box<T>, b: Box<T>) {
a = b;
fn f10b<T>(mut a: Box<T>, b: Box<T>) { //~ ERROR variable `a` is assigned to, but never used
a = b; //~ ERROR value assigned to `a` is never read
}
// unused params warnings are not needed for intrinsic functions without bodies

View file

@ -283,5 +283,21 @@ LL | a = b;
|
= help: maybe it is overwritten before being read?
error: aborting due to 34 previous errors; 1 warning emitted
error: variable `a` is assigned to, but never used
--> $DIR/liveness-unused.rs:247:12
|
LL | fn f10b<T>(mut a: Box<T>, b: Box<T>) {
| ^^^^^
|
= note: consider using `_a` instead
error: value assigned to `a` is never read
--> $DIR/liveness-unused.rs:248:5
|
LL | a = b;
| ^
|
= help: maybe it is overwritten before being read?
error: aborting due to 36 previous errors; 1 warning emitted