Skip to content

Commit 5aded99

Browse files
committed
don't lint on x = x + y inside an AddAssign impl
fixes #1302
1 parent ea0227f commit 5aded99

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

clippy_lints/src/assign_ops.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc::hir;
22
use rustc::lint::*;
3+
use syntax::ast;
34
use utils::{span_lint_and_then, snippet_opt, SpanlessEq, get_trait_def_id, implements_trait};
45
use utils::{higher, sugg};
56

@@ -135,6 +136,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
135136
} else {
136137
return; // useless if the trait doesn't exist
137138
};
139+
// check that we are not inside an `impl AssignOp` of this exact operation
140+
let parent_fn = cx.tcx.map.get_parent(e.id);
141+
let parent_impl = cx.tcx.map.get_parent(parent_fn);
142+
// the crate node is the only one that is not in the map
143+
if parent_impl != ast::CRATE_NODE_ID {
144+
if let hir::map::Node::NodeItem(item) = cx.tcx.map.get(parent_impl) {
145+
if let hir::Item_::ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
146+
if trait_ref.path.def.def_id() == trait_id {
147+
return;
148+
}
149+
}
150+
}
151+
}
138152
implements_trait($cx, $ty, trait_id, vec![$rty])
139153
},)*
140154
_ => false,

tests/compile-fail/assign_ops2.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,24 @@ fn main() {
3434
a %= 42 % a;
3535
a <<= 6 << a;
3636
}
37+
38+
// check that we don't lint on op assign impls, because that's just the way to impl them
39+
40+
use std::ops::{Mul, MulAssign};
41+
42+
#[derive(Copy, Clone, Debug, PartialEq)]
43+
pub struct Wrap(i64);
44+
45+
impl Mul<i64> for Wrap {
46+
type Output = Self;
47+
48+
fn mul(self, rhs: i64) -> Self {
49+
Wrap(self.0 * rhs)
50+
}
51+
}
52+
53+
impl MulAssign<i64> for Wrap {
54+
fn mul_assign(&mut self, rhs: i64) {
55+
*self = *self * rhs
56+
}
57+
}

0 commit comments

Comments
 (0)