Skip to content

Commit cfe4efd

Browse files
committed
Do not trigger unused_variables lint for variable modified through AddAssign
Visit an overloaded += like a method call and not like an assignment.
1 parent d5b6599 commit cfe4efd

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/librustc/middle/liveness.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1086,11 +1086,17 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10861086
}
10871087

10881088
hir::ExprAssignOp(_, ref l, ref r) => {
1089-
// see comment on lvalues in
1090-
// propagate_through_lvalue_components()
1091-
let succ = self.write_lvalue(&l, succ, ACC_WRITE|ACC_READ);
1092-
let succ = self.propagate_through_expr(&r, succ);
1093-
self.propagate_through_lvalue_components(&l, succ)
1089+
// an overloaded assign op is like a method call
1090+
if self.ir.tcx.is_method_call(expr.id) {
1091+
let succ = self.propagate_through_expr(&l, succ);
1092+
self.propagate_through_expr(&r, succ)
1093+
} else {
1094+
// see comment on lvalues in
1095+
// propagate_through_lvalue_components()
1096+
let succ = self.write_lvalue(&l, succ, ACC_WRITE|ACC_READ);
1097+
let succ = self.propagate_through_expr(&r, succ);
1098+
self.propagate_through_lvalue_components(&l, succ)
1099+
}
10941100
}
10951101

10961102
// Uninteresting cases: just propagate in rev exec order

src/test/compile-fail/liveness-unused.rs

+19
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,24 @@ fn f6() {
127127
}) += 1;
128128
}
129129

130+
131+
struct MutRef<'a>(&'a mut i32);
132+
133+
impl<'a> AddAssign<i32> for MutRef<'a> {
134+
fn add_assign(&mut self, rhs: i32) {
135+
*self.0 += rhs;
136+
}
137+
}
138+
139+
fn f7() {
140+
let mut a = 1;
141+
{
142+
// `b` does not trigger unused_variables
143+
let mut b = MutRef(&mut a);
144+
b += 1;
145+
}
146+
drop(a);
147+
}
148+
130149
fn main() {
131150
}

0 commit comments

Comments
 (0)