Merge pull request #2511 from flip1995/sus_impl

UnNeg and UnNot count as additional operations now
This commit is contained in:
Oliver Schneider 2018-03-06 14:35:54 +01:00 committed by GitHub
commit 7d5ecd5ad5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 9 deletions

View file

@ -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) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
use rustc::hir::BinOp_::*; use rustc::hir::BinOp_::*;
if let hir::ExprBinary(binop, _, _) = expr.node { 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 // as a child node
let mut parent_expr = cx.tcx.hir.get_parent_node(expr.id); let mut parent_expr = cx.tcx.hir.get_parent_node(expr.id);
while parent_expr != ast::CRATE_NODE_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::map::Node::NodeExpr(e) = cx.tcx.hir.get(parent_expr); match e.node {
if let hir::ExprBinary(_, _, _) = e.node; hir::ExprBinary(..)
then { | hir::ExprUnary(hir::UnOp::UnNot, _)
return | hir::ExprUnary(hir::UnOp::UnNeg, _) => return,
_ => {},
} }
} }
parent_expr = cx.tcx.hir.get_parent_node(parent_expr); parent_expr = cx.tcx.hir.get_parent_node(parent_expr);
} }
// as a parent node // as a parent node
@ -180,8 +180,13 @@ struct BinaryExprVisitor {
impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor { impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor {
fn visit_expr(&mut self, expr: &'tcx hir::Expr) { fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
if let hir::ExprBinary(_, _, _) = expr.node { match expr.node {
self.in_binary_expr = true; hir::ExprBinary(..)
| hir::ExprUnary(hir::UnOp::UnNot, _)
| hir::ExprUnary(hir::UnOp::UnNeg, _) => {
self.in_binary_expr = true
},
_ => {},
} }
walk_expr(self, expr); walk_expr(self, expr);

View file

@ -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 main() {}
fn do_nothing(x: u32) -> u32 { fn do_nothing(x: u32) -> u32 {