Skip to content

fix: re-enable self-assignment #145214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
}
}

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

#[allow(dead_code)] // FIXME(81658): should be used + lint reinstated after #83171 relands.
fn check_for_self_assign(&mut self, assign: &'tcx hir::Expr<'tcx>) {
fn check_for_self_assign_helper<'tcx>(
typeck_results: &'tcx ty::TypeckResults<'tcx>,
Expand Down Expand Up @@ -576,6 +574,10 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
hir::ExprKind::OffsetOf(..) => {
self.handle_offset_of(expr);
}
hir::ExprKind::Assign(ref lhs, ..) => {
self.handle_assign(lhs);
self.check_for_self_assign(expr);
}
_ => (),
}

Expand Down
17 changes: 7 additions & 10 deletions tests/ui/lint/dead-code/self-assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
//! - `dead_code` lint expansion for self-assignments was implemented in #87129.
//! - Unfortunately implementation components of #87129 had to be disabled as part of reverts
//! #86212, #83171 (to revert #81473) to address regressions #81626 and #81658.
//! - Consequently, none of the following warnings are emitted.
//! - Re-enabled in current version to properly detect self-assignments.

//@ check-pass

// Implementation of self-assignment `dead_code` lint expansions disabled due to reverts.
//@ known-bug: #75356

#![allow(unused_assignments)]
#![warn(dead_code)]

fn main() {
let mut x = 0;
x = x;
// FIXME ~^ WARNING: useless assignment of variable of type `i32` to itself
//~^ WARNING: useless assignment of variable of type `i32` to itself

x = (x);
// FIXME ~^ WARNING: useless assignment of variable of type `i32` to itself
//~^ WARNING: useless assignment of variable of type `i32` to itself

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

s.f = s.f;
// FIXME ~^ WARNING: useless assignment of field of type `&str` to itself
//~^ WARNING: useless assignment of field of type `&str` to itself


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

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


let mut y = 0;
Expand Down
44 changes: 44 additions & 0 deletions tests/ui/lint/dead-code/self-assign.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
warning: useless assignment of variable of type `i32` to itself
--> $DIR/self-assign.rs:19:5
|
LL | x = x;
| ^^^^^
|
note: the lint level is defined here
--> $DIR/self-assign.rs:15:9
|
LL | #![warn(dead_code)]
| ^^^^^^^^^

warning: useless assignment of variable of type `i32` to itself
--> $DIR/self-assign.rs:22:5
|
LL | x = (x);
| ^^^^^^^

warning: useless assignment of variable of type `S<'_>` to itself
--> $DIR/self-assign.rs:31:5
|
LL | s = s;
| ^^^^^

warning: useless assignment of field of type `&str` to itself
--> $DIR/self-assign.rs:34:5
|
LL | s.f = s.f;
| ^^^^^^^^^

warning: useless assignment of field of type `Box<i32>` to itself
--> $DIR/self-assign.rs:43:5
|
LL | n3.n.0.n.x = n3.n.0.n.x;
| ^^^^^^^^^^^^^^^^^^^^^^^

warning: useless assignment of field of type `i32` to itself
--> $DIR/self-assign.rs:47:5
|
LL | t.1.0.2.1 = t.1.0.2.1;
| ^^^^^^^^^^^^^^^^^^^^^

warning: 6 warnings emitted

Loading