Skip to content

Commit eb0b95b

Browse files
committed
Auto merge of #87129 - FabianWolff:issue-75356, r=varkor
Warn about useless assignments of variables/fields to themselves This PR fixes #75356. Following `@varkor's` suggestion in #75356 (comment), I have implemented this warning as part of the `dead_code` lint. Unlike the `-Wself-assign` implementation in [Clang](https://github.com/llvm/llvm-project/blob/56e6d4742e6909bd7d2db201cc5e0e3e77c6f282/clang/lib/Sema/SemaExpr.cpp#L13875-L13909), my implementation also warns about self-assignments of struct fields (`s.x = s.x`). r? `@varkor`
2 parents c7331d6 + dc639c9 commit eb0b95b

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed

compiler/rustc_passes/src/dead.rs

+54
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,59 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
150150
}
151151
}
152152

153+
fn check_for_self_assign(&mut self, assign: &'tcx hir::Expr<'tcx>) {
154+
fn check_for_self_assign_helper(
155+
tcx: TyCtxt<'tcx>,
156+
typeck_results: &'tcx ty::TypeckResults<'tcx>,
157+
lhs: &'tcx hir::Expr<'tcx>,
158+
rhs: &'tcx hir::Expr<'tcx>,
159+
) -> bool {
160+
match (&lhs.kind, &rhs.kind) {
161+
(hir::ExprKind::Path(ref qpath_l), hir::ExprKind::Path(ref qpath_r)) => {
162+
if let (Res::Local(id_l), Res::Local(id_r)) = (
163+
typeck_results.qpath_res(qpath_l, lhs.hir_id),
164+
typeck_results.qpath_res(qpath_r, rhs.hir_id),
165+
) {
166+
if id_l == id_r {
167+
return true;
168+
}
169+
}
170+
return false;
171+
}
172+
(hir::ExprKind::Field(lhs_l, ident_l), hir::ExprKind::Field(lhs_r, ident_r)) => {
173+
if ident_l == ident_r {
174+
return check_for_self_assign_helper(tcx, typeck_results, lhs_l, lhs_r);
175+
}
176+
return false;
177+
}
178+
_ => {
179+
return false;
180+
}
181+
}
182+
}
183+
184+
if let hir::ExprKind::Assign(lhs, rhs, _) = assign.kind {
185+
if check_for_self_assign_helper(self.tcx, self.typeck_results(), lhs, rhs)
186+
&& !assign.span.from_expansion()
187+
{
188+
let is_field_assign = matches!(lhs.kind, hir::ExprKind::Field(..));
189+
self.tcx.struct_span_lint_hir(
190+
lint::builtin::DEAD_CODE,
191+
assign.hir_id,
192+
assign.span,
193+
|lint| {
194+
lint.build(&format!(
195+
"useless assignment of {} of type `{}` to itself",
196+
if is_field_assign { "field" } else { "variable" },
197+
self.typeck_results().expr_ty(lhs),
198+
))
199+
.emit();
200+
},
201+
)
202+
}
203+
}
204+
}
205+
153206
fn handle_field_pattern_match(
154207
&mut self,
155208
lhs: &hir::Pat<'_>,
@@ -287,6 +340,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
287340
}
288341
hir::ExprKind::Assign(ref left, ref right, ..) => {
289342
self.handle_assign(left);
343+
self.check_for_self_assign(expr);
290344
self.visit_expr(right);
291345
return;
292346
}

src/test/ui/issues/issue-3290.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-pass
22
#![feature(box_syntax)]
3+
#![allow(dead_code)]
34

45
pub fn main() {
56
let mut x: Box<_> = box 3;
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Test that dead code warnings are issued for superfluous assignments of
2+
// fields or variables to themselves (issue #75356).
3+
4+
// check-pass
5+
#![allow(unused_assignments)]
6+
#![warn(dead_code)]
7+
8+
fn main() {
9+
let mut x = 0;
10+
x = x;
11+
//~^ WARNING: useless assignment of variable of type `i32` to itself
12+
13+
x = (x);
14+
//~^ WARNING: useless assignment of variable of type `i32` to itself
15+
16+
x = {x};
17+
// block expressions don't count as self-assignments
18+
19+
20+
struct S<'a> { f: &'a str }
21+
let mut s = S { f: "abc" };
22+
s = s;
23+
//~^ WARNING: useless assignment of variable of type `S` to itself
24+
25+
s.f = s.f;
26+
//~^ WARNING: useless assignment of field of type `&str` to itself
27+
28+
29+
struct N0 { x: Box<i32> }
30+
struct N1 { n: N0 }
31+
struct N2(N1);
32+
struct N3 { n: N2 };
33+
let mut n3 = N3 { n: N2(N1 { n: N0 { x: Box::new(42) } }) };
34+
n3.n.0.n.x = n3.n.0.n.x;
35+
//~^ WARNING: useless assignment of field of type `Box<i32>` to itself
36+
37+
let mut t = (1, ((2, 3, (4, 5)),));
38+
t.1.0.2.1 = t.1.0.2.1;
39+
//~^ WARNING: useless assignment of field of type `i32` to itself
40+
41+
42+
let mut y = 0;
43+
macro_rules! assign_to_y {
44+
($cur:expr) => {{
45+
y = $cur;
46+
}};
47+
}
48+
assign_to_y!(y);
49+
// self-assignments in macro expansions are not reported either
50+
}
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:10:5
3+
|
4+
LL | x = x;
5+
| ^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/self-assign.rs:6: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:13:5
15+
|
16+
LL | x = (x);
17+
| ^^^^^^^
18+
19+
warning: useless assignment of variable of type `S` to itself
20+
--> $DIR/self-assign.rs:22:5
21+
|
22+
LL | s = s;
23+
| ^^^^^
24+
25+
warning: useless assignment of field of type `&str` to itself
26+
--> $DIR/self-assign.rs:25: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:34: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:38:5
39+
|
40+
LL | t.1.0.2.1 = t.1.0.2.1;
41+
| ^^^^^^^^^^^^^^^^^^^^^
42+
43+
warning: 6 warnings emitted
44+

src/test/ui/nll/issue-50461-used-mut-from-moves.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-pass
22

33
#![deny(unused_mut)]
4+
#![allow(dead_code)]
45

56
struct Foo {
67
pub value: i32

src/test/ui/self/self-re-assign.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// that we do not glue_drop before we glue_take (#3290).
44

55
#![feature(box_syntax)]
6+
#![allow(dead_code)]
67

78
use std::rc::Rc;
89

0 commit comments

Comments
 (0)