Skip to content

Commit e92800a

Browse files
authored
Rollup merge of rust-lang#145214 - notJoon:fix/enable-self-assignment, r=petrochenkov
fix: re-enable self-assignment ## Description Re-enables the self-assignment detection that was previously disabled due to unrelated regressions. The fix detects useless assignments like `x = x` and `foo.field = foo.field`. ## History The original regressions (rust-lang#81626, rust-lang#81658) were specifically about false positives in write-only field detection, not self-assignment detection. Belows are brief history for the rule that I understand. - Self-assignment detection was originally implemented in rust-lang#87129 to address rust-lang#75356 - The implementation was disabled alongside the revert of rust-lang#81473's "write-only fields" detection - rust-lang#81473 was reverted via rust-lang#86212 and rust-lang#83171 due to false positives in write-only field detection (rust-lang#81626, rust-lang#81658) - The self-assignment detection feature got removed, even though it wasn't the reason for the problems This PR only re-enables the self-assignment checks, which are orthogonal to the problematic write-only field analysis. ## Changes - Removed `#[allow(dead_code)]` from `compiler/rustc_passes/src/dead.rs` file - `handle_assign` and - `check_for_self_assign` - Added `ExprKind::Assign` handling in `visit_expr` to call both methods - Updated test expectations in `tests/ui/lint/dead-code/self-assign.rs`
2 parents 2a39cf1 + ba350ff commit e92800a

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
172172
}
173173
}
174174

175-
#[allow(dead_code)] // FIXME(81658): should be used + lint reinstated after #83171 relands.
176175
fn handle_assign(&mut self, expr: &'tcx hir::Expr<'tcx>) {
177176
if self
178177
.typeck_results()
@@ -189,7 +188,6 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
189188
}
190189
}
191190

192-
#[allow(dead_code)] // FIXME(81658): should be used + lint reinstated after #83171 relands.
193191
fn check_for_self_assign(&mut self, assign: &'tcx hir::Expr<'tcx>) {
194192
fn check_for_self_assign_helper<'tcx>(
195193
typeck_results: &'tcx ty::TypeckResults<'tcx>,
@@ -576,6 +574,10 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
576574
hir::ExprKind::OffsetOf(..) => {
577575
self.handle_offset_of(expr);
578576
}
577+
hir::ExprKind::Assign(ref lhs, ..) => {
578+
self.handle_assign(lhs);
579+
self.check_for_self_assign(expr);
580+
}
579581
_ => (),
580582
}
581583

tests/ui/lint/dead-code/self-assign.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@
77
//! - `dead_code` lint expansion for self-assignments was implemented in #87129.
88
//! - Unfortunately implementation components of #87129 had to be disabled as part of reverts
99
//! #86212, #83171 (to revert #81473) to address regressions #81626 and #81658.
10-
//! - Consequently, none of the following warnings are emitted.
10+
//! - Re-enabled in current version to properly detect self-assignments.
1111
1212
//@ check-pass
1313

14-
// Implementation of self-assignment `dead_code` lint expansions disabled due to reverts.
15-
//@ known-bug: #75356
16-
1714
#![allow(unused_assignments)]
1815
#![warn(dead_code)]
1916

2017
fn main() {
2118
let mut x = 0;
2219
x = x;
23-
// FIXME ~^ WARNING: useless assignment of variable of type `i32` to itself
20+
//~^ WARNING: useless assignment of variable of type `i32` to itself
2421

2522
x = (x);
26-
// FIXME ~^ WARNING: useless assignment of variable of type `i32` to itself
23+
//~^ WARNING: useless assignment of variable of type `i32` to itself
2724

2825
x = {x};
2926
// block expressions don't count as self-assignments
@@ -32,10 +29,10 @@ fn main() {
3229
struct S<'a> { f: &'a str }
3330
let mut s = S { f: "abc" };
3431
s = s;
35-
// FIXME ~^ WARNING: useless assignment of variable of type `S` to itself
32+
//~^ WARNING: useless assignment of variable of type `S<'_>` to itself
3633

3734
s.f = s.f;
38-
// FIXME ~^ WARNING: useless assignment of field of type `&str` to itself
35+
//~^ WARNING: useless assignment of field of type `&str` to itself
3936

4037

4138
struct N0 { x: Box<i32> }
@@ -44,11 +41,11 @@ fn main() {
4441
struct N3 { n: N2 };
4542
let mut n3 = N3 { n: N2(N1 { n: N0 { x: Box::new(42) } }) };
4643
n3.n.0.n.x = n3.n.0.n.x;
47-
// FIXME ~^ WARNING: useless assignment of field of type `Box<i32>` to itself
44+
//~^ WARNING: useless assignment of field of type `Box<i32>` to itself
4845

4946
let mut t = (1, ((2, 3, (4, 5)),));
5047
t.1.0.2.1 = t.1.0.2.1;
51-
// FIXME ~^ WARNING: useless assignment of field of type `i32` to itself
48+
//~^ WARNING: useless assignment of field of type `i32` to itself
5249

5350

5451
let mut y = 0;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
warning: useless assignment of variable of type `i32` to itself
2+
--> $DIR/self-assign.rs:19:5
3+
|
4+
LL | x = x;
5+
| ^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/self-assign.rs:15:9
9+
|
10+
LL | #![warn(dead_code)]
11+
| ^^^^^^^^^
12+
13+
warning: useless assignment of variable of type `i32` to itself
14+
--> $DIR/self-assign.rs:22:5
15+
|
16+
LL | x = (x);
17+
| ^^^^^^^
18+
19+
warning: useless assignment of variable of type `S<'_>` to itself
20+
--> $DIR/self-assign.rs:31:5
21+
|
22+
LL | s = s;
23+
| ^^^^^
24+
25+
warning: useless assignment of field of type `&str` to itself
26+
--> $DIR/self-assign.rs:34:5
27+
|
28+
LL | s.f = s.f;
29+
| ^^^^^^^^^
30+
31+
warning: useless assignment of field of type `Box<i32>` to itself
32+
--> $DIR/self-assign.rs:43:5
33+
|
34+
LL | n3.n.0.n.x = n3.n.0.n.x;
35+
| ^^^^^^^^^^^^^^^^^^^^^^^
36+
37+
warning: useless assignment of field of type `i32` to itself
38+
--> $DIR/self-assign.rs:47:5
39+
|
40+
LL | t.1.0.2.1 = t.1.0.2.1;
41+
| ^^^^^^^^^^^^^^^^^^^^^
42+
43+
warning: 6 warnings emitted
44+

0 commit comments

Comments
 (0)