Skip to content

Commit a03222c

Browse files
committed
Do not trigger unused_assignments for overloaded AssignOps
If `v` were a type with some kind of indirection, so that `v += 1` would have an effect even if `v` were not used anymore, the unused_assignments lint would mark a false positive. This exempts overloaded (non-primitive) assign ops from being treated as assignments (they are method calls). The previous compile-fail tests that ensure x += 1 can trigger for primitive types continue to pass. Added a representative test for the "view" indirection.
1 parent c97524b commit a03222c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/librustc/middle/liveness.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,9 @@ fn check_expr(this: &mut Liveness, expr: &Expr) {
14151415
}
14161416

14171417
hir::ExprAssignOp(_, ref l, _) => {
1418-
this.check_lvalue(&l);
1418+
if !this.ir.tcx.is_method_call(expr.id) {
1419+
this.check_lvalue(&l);
1420+
}
14191421

14201422
intravisit::walk_expr(this, expr);
14211423
}

src/test/run-pass/augmented-assignments.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![deny(unused_assignments)]
12+
1113
use std::mem;
1214
use std::ops::{
1315
AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, DivAssign, Index, MulAssign, RemAssign,
@@ -27,6 +29,8 @@ impl Slice {
2729
}
2830
}
2931

32+
struct View<'a>(&'a mut [i32]);
33+
3034
fn main() {
3135
let mut x = Int(1);
3236

@@ -78,6 +82,12 @@ fn main() {
7882
assert_eq!(array[0], 1);
7983
assert_eq!(array[1], 2);
8084
assert_eq!(array[2], 3);
85+
86+
// sized indirection
87+
// check that this does *not* trigger the unused_assignments lint
88+
let mut array = [0, 1, 2];
89+
let mut view = View(&mut array);
90+
view += 1;
8191
}
8292

8393
impl AddAssign for Int {
@@ -159,3 +169,11 @@ impl AddAssign<i32> for Slice {
159169
}
160170
}
161171
}
172+
173+
impl<'a> AddAssign<i32> for View<'a> {
174+
fn add_assign(&mut self, rhs: i32) {
175+
for lhs in self.0.iter_mut() {
176+
*lhs += rhs;
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)