Skip to content

don't lint on x = x + y inside an AddAssign impl #1318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ clippy_lints = { version = "0.0.104", path = "clippy_lints" }
# end automatic update

[dev-dependencies]
compiletest_rs = "0.2.1"
compiletest_rs = "0.2.5"
lazy_static = "0.1.15"
regex = "0.1.71"
rustc-serialize = "0.3"
Expand Down
14 changes: 14 additions & 0 deletions clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc::hir;
use rustc::lint::*;
use syntax::ast;
use utils::{span_lint_and_then, snippet_opt, SpanlessEq, get_trait_def_id, implements_trait};
use utils::{higher, sugg};

Expand Down Expand Up @@ -135,6 +136,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
} else {
return; // useless if the trait doesn't exist
};
// check that we are not inside an `impl AssignOp` of this exact operation
let parent_fn = cx.tcx.map.get_parent(e.id);
let parent_impl = cx.tcx.map.get_parent(parent_fn);
// the crate node is the only one that is not in the map
if parent_impl != ast::CRATE_NODE_ID {
if let hir::map::Node::NodeItem(item) = cx.tcx.map.get(parent_impl) {
if let hir::Item_::ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
if trait_ref.path.def.def_id() == trait_id {
return;
}
}
}
}
implements_trait($cx, $ty, trait_id, vec![$rty])
},)*
_ => false,
Expand Down
21 changes: 21 additions & 0 deletions tests/compile-fail/assign_ops2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,24 @@ fn main() {
a %= 42 % a;
a <<= 6 << a;
}

// check that we don't lint on op assign impls, because that's just the way to impl them

use std::ops::{Mul, MulAssign};

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Wrap(i64);

impl Mul<i64> for Wrap {
type Output = Self;

fn mul(self, rhs: i64) -> Self {
Wrap(self.0 * rhs)
}
}

impl MulAssign<i64> for Wrap {
fn mul_assign(&mut self, rhs: i64) {
*self = *self * rhs
}
}