From ad459184a311e4764072bec33f5cef95b63b838c Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sat, 17 Mar 2018 21:34:13 +0100 Subject: [PATCH] Don't lint comparison operators in arithmetic impls --- clippy_lints/src/suspicious_trait_impl.rs | 4 ++++ tests/ui/suspicious_arithmetic_impl.rs | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/suspicious_trait_impl.rs b/clippy_lints/src/suspicious_trait_impl.rs index ecf8e83a86f..2c3abc512bb 100644 --- a/clippy_lints/src/suspicious_trait_impl.rs +++ b/clippy_lints/src/suspicious_trait_impl.rs @@ -61,6 +61,10 @@ 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 { + match binop.node { + BiEq | BiLt | BiLe | BiNe | BiGe | BiGt => return, + _ => {}, + } // 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); diff --git a/tests/ui/suspicious_arithmetic_impl.rs b/tests/ui/suspicious_arithmetic_impl.rs index 22233a4b154..d5982efe12f 100644 --- a/tests/ui/suspicious_arithmetic_impl.rs +++ b/tests/ui/suspicious_arithmetic_impl.rs @@ -59,7 +59,11 @@ 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 + if self.0 <= other.0 { + Bar(-(self.0 & other.0)) // OK: UnNeg part of BiExpr as parent node + } else { + Bar(0) + } } }