2 use rustc::{declare_lint, lint_array};
3 use if_chain::if_chain;
5 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
7 use crate::utils::{get_trait_def_id, span_lint};
9 /// **What it does:** Lints for suspicious operations in impls of arithmetic operators, e.g.
10 /// subtracting elements in an Add impl.
12 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
14 /// **Known problems:** None.
18 /// impl Add for Foo {
19 /// type Output = Foo;
21 /// fn add(self, other: Foo) -> Foo {
22 /// Foo(self.0 - other.0)
26 declare_clippy_lint! {
27 pub SUSPICIOUS_ARITHMETIC_IMPL,
29 "suspicious use of operators in impl of arithmetic trait"
32 /// **What it does:** Lints for suspicious operations in impls of OpAssign, e.g.
33 /// subtracting elements in an AddAssign impl.
35 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
37 /// **Known problems:** None.
41 /// impl AddAssign for Foo {
42 /// fn add_assign(&mut self, other: Foo) {
43 /// *self = *self - other;
47 declare_clippy_lint! {
48 pub SUSPICIOUS_OP_ASSIGN_IMPL,
50 "suspicious use of operators in impl of OpAssign trait"
53 #[derive(Copy, Clone)]
54 pub struct SuspiciousImpl;
56 impl LintPass for SuspiciousImpl {
57 fn get_lints(&self) -> LintArray {
58 lint_array![SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]
62 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
63 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
64 if let hir::ExprKind::Binary(binop, _, _) = expr.node {
75 // Check if the binary expression is part of another bi/unary expression
77 let mut parent_expr = cx.tcx.hir.get_parent_node(expr.id);
78 while parent_expr != ast::CRATE_NODE_ID {
79 if let hir::map::Node::NodeExpr(e) = cx.tcx.hir.get(parent_expr) {
81 hir::ExprKind::Binary(..)
82 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
83 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => return,
87 parent_expr = cx.tcx.hir.get_parent_node(parent_expr);
90 let mut visitor = BinaryExprVisitor {
91 in_binary_expr: false,
93 walk_expr(&mut visitor, expr);
95 if visitor.in_binary_expr {
99 if let Some(impl_trait) = check_binop(
103 &["Add", "Sub", "Mul", "Div"],
113 SUSPICIOUS_ARITHMETIC_IMPL,
116 r#"Suspicious use of binary operator in `{}` impl"#,
122 if let Some(impl_trait) = check_binop(
143 hir::BinOpKind::BitAnd,
144 hir::BinOpKind::BitOr,
145 hir::BinOpKind::BitXor,
153 SUSPICIOUS_OP_ASSIGN_IMPL,
156 r#"Suspicious use of binary operator in `{}` impl"#,
168 binop: hir::BinOpKind,
170 expected_ops: &[hir::BinOpKind],
171 ) -> Option<&'a str> {
172 let mut trait_ids = vec![];
173 let [krate, module] = crate::utils::paths::OPS_MODULE;
176 let path = [krate, module, t];
177 if let Some(trait_id) = get_trait_def_id(cx, &path) {
178 trait_ids.push(trait_id);
184 // Get the actually implemented trait
185 let parent_fn = cx.tcx.hir.get_parent(expr.id);
186 let parent_impl = cx.tcx.hir.get_parent(parent_fn);
189 if parent_impl != ast::CRATE_NODE_ID;
190 if let hir::map::Node::NodeItem(item) = cx.tcx.hir.get(parent_impl);
191 if let hir::ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node;
192 if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.def.def_id());
193 if binop != expected_ops[idx];
195 return Some(traits[idx])
202 struct BinaryExprVisitor {
203 in_binary_expr: bool,
206 impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor {
207 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
209 hir::ExprKind::Binary(..)
210 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
211 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => {
212 self.in_binary_expr = true
217 walk_expr(self, expr);
219 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
220 NestedVisitorMap::None