Skip to content

Commit

Permalink
Do not trigger unused_variables lint for variable modified through Ad…
Browse files Browse the repository at this point in the history
…dAssign

Visit an overloaded += like a method call and not like an assignment.
  • Loading branch information
bluss committed Mar 4, 2016
1 parent d5b6599 commit cfe4efd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/librustc/middle/liveness.rs
Expand Up @@ -1086,11 +1086,17 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}

hir::ExprAssignOp(_, ref l, ref r) => {
// see comment on lvalues in
// propagate_through_lvalue_components()
let succ = self.write_lvalue(&l, succ, ACC_WRITE|ACC_READ);
let succ = self.propagate_through_expr(&r, succ);
self.propagate_through_lvalue_components(&l, succ)
// an overloaded assign op is like a method call
if self.ir.tcx.is_method_call(expr.id) {
let succ = self.propagate_through_expr(&l, succ);
self.propagate_through_expr(&r, succ)
} else {
// see comment on lvalues in
// propagate_through_lvalue_components()
let succ = self.write_lvalue(&l, succ, ACC_WRITE|ACC_READ);
let succ = self.propagate_through_expr(&r, succ);
self.propagate_through_lvalue_components(&l, succ)
}
}

// Uninteresting cases: just propagate in rev exec order
Expand Down
19 changes: 19 additions & 0 deletions src/test/compile-fail/liveness-unused.rs
Expand Up @@ -127,5 +127,24 @@ fn f6() {
}) += 1;
}


struct MutRef<'a>(&'a mut i32);

impl<'a> AddAssign<i32> for MutRef<'a> {
fn add_assign(&mut self, rhs: i32) {
*self.0 += rhs;
}
}

fn f7() {
let mut a = 1;
{
// `b` does not trigger unused_variables
let mut b = MutRef(&mut a);
b += 1;
}
drop(a);
}

fn main() {
}

0 comments on commit cfe4efd

Please sign in to comment.