From ed4535641bb4329d741dcbd162d8a7d7374a67db Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 6 Mar 2018 13:58:03 +0100 Subject: [PATCH] UnNeg and UnNot count as additional operations now --- clippy_lints/src/suspicious_trait_impl.rs | 23 ++++++++++++++--------- tests/ui/suspicious_arithmetic_impl.rs | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/suspicious_trait_impl.rs b/clippy_lints/src/suspicious_trait_impl.rs index 682ab4d15ea..ecf8e83a86f 100644 --- a/clippy_lints/src/suspicious_trait_impl.rs +++ b/clippy_lints/src/suspicious_trait_impl.rs @@ -61,18 +61,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) { use rustc::hir::BinOp_::*; if let hir::ExprBinary(binop, _, _) = expr.node { - // Check if the binary expression is part of another binary expression + // Check if the binary expression is part of another bi/unary expression // as a child node let mut parent_expr = cx.tcx.hir.get_parent_node(expr.id); while parent_expr != ast::CRATE_NODE_ID { - if_chain! { - if let hir::map::Node::NodeExpr(e) = cx.tcx.hir.get(parent_expr); - if let hir::ExprBinary(_, _, _) = e.node; - then { - return + if let hir::map::Node::NodeExpr(e) = cx.tcx.hir.get(parent_expr) { + match e.node { + hir::ExprBinary(..) + | hir::ExprUnary(hir::UnOp::UnNot, _) + | hir::ExprUnary(hir::UnOp::UnNeg, _) => return, + _ => {}, } } - parent_expr = cx.tcx.hir.get_parent_node(parent_expr); } // as a parent node @@ -180,8 +180,13 @@ struct BinaryExprVisitor { impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor { fn visit_expr(&mut self, expr: &'tcx hir::Expr) { - if let hir::ExprBinary(_, _, _) = expr.node { - self.in_binary_expr = true; + match expr.node { + hir::ExprBinary(..) + | hir::ExprUnary(hir::UnOp::UnNot, _) + | hir::ExprUnary(hir::UnOp::UnNeg, _) => { + self.in_binary_expr = true + }, + _ => {}, } walk_expr(self, expr); diff --git a/tests/ui/suspicious_arithmetic_impl.rs b/tests/ui/suspicious_arithmetic_impl.rs index 097627e1d7c..22233a4b154 100644 --- a/tests/ui/suspicious_arithmetic_impl.rs +++ b/tests/ui/suspicious_arithmetic_impl.rs @@ -45,6 +45,24 @@ impl Div for Foo { } } +struct Bar(i32); + +impl Add for Bar { + type Output = Bar; + + fn add(self, other: Self) -> Self { + Bar(self.0 & !other.0) // OK: UnNot part of BiExpr as child node + } +} + +impl Sub for Bar { + type Output = Bar; + + fn sub(self, other: Self) -> Self { + Bar(-(self.0 & other.0)) // OK: UnNeg part of BiExpr as parent node + } +} + fn main() {} fn do_nothing(x: u32) -> u32 {