Skip to content

Commit

Permalink
Merge pull request #2511 from flip1995/sus_impl
Browse files Browse the repository at this point in the history
UnNeg and UnNot count as additional operations now
  • Loading branch information
oli-obk authored Mar 6, 2018
2 parents 9683f38 + ed45356 commit 7d5ecd5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
23 changes: 14 additions & 9 deletions clippy_lints/src/suspicious_trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/suspicious_arithmetic_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7d5ecd5

Please sign in to comment.