Skip to content

Don't lint comparison operators in arithmetic impls #2545

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 1 commit into from
Mar 17, 2018
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
4 changes: 4 additions & 0 deletions clippy_lints/src/suspicious_trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/suspicious_arithmetic_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down