Skip to content

Commit cb7af65

Browse files
authored
Merge pull request #2545 from flip1995/sus_arith
Don't lint comparison operators in arithmetic impls
2 parents 03f4ae0 + ad45918 commit cb7af65

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

clippy_lints/src/suspicious_trait_impl.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
6161
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
6262
use rustc::hir::BinOp_::*;
6363
if let hir::ExprBinary(binop, _, _) = expr.node {
64+
match binop.node {
65+
BiEq | BiLt | BiLe | BiNe | BiGe | BiGt => return,
66+
_ => {},
67+
}
6468
// Check if the binary expression is part of another bi/unary expression
6569
// as a child node
6670
let mut parent_expr = cx.tcx.hir.get_parent_node(expr.id);

tests/ui/suspicious_arithmetic_impl.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ impl Sub for Bar {
5959
type Output = Bar;
6060

6161
fn sub(self, other: Self) -> Self {
62-
Bar(-(self.0 & other.0)) // OK: UnNeg part of BiExpr as parent node
62+
if self.0 <= other.0 {
63+
Bar(-(self.0 & other.0)) // OK: UnNeg part of BiExpr as parent node
64+
} else {
65+
Bar(0)
66+
}
6367
}
6468
}
6569

0 commit comments

Comments
 (0)