1 use crate::utils::{get_trait_def_id, span_lint, trait_ref_of_method};
2 use if_chain::if_chain;
4 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint_pass, declare_tool_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 pub SUSPICIOUS_ARITHMETIC_IMPL,
28 "suspicious use of operators in impl of arithmetic trait"
31 declare_clippy_lint! {
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 pub SUSPICIOUS_OP_ASSIGN_IMPL,
49 "suspicious use of operators in impl of OpAssign trait"
52 declare_lint_pass!(SuspiciousImpl => [SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]);
54 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
55 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
56 if let hir::ExprKind::Binary(binop, _, _) = expr.node {
63 | hir::BinOpKind::Gt => return,
66 // Check if the binary expression is part of another bi/unary expression
68 let mut parent_expr = cx.tcx.hir().get_parent_node_by_hir_id(expr.hir_id);
69 while parent_expr != hir::CRATE_HIR_ID {
70 if let hir::Node::Expr(e) = cx.tcx.hir().get_by_hir_id(parent_expr) {
72 hir::ExprKind::Binary(..)
73 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
74 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => return,
78 parent_expr = cx.tcx.hir().get_parent_node_by_hir_id(parent_expr);
81 let mut visitor = BinaryExprVisitor { in_binary_expr: false };
82 walk_expr(&mut visitor, expr);
84 if visitor.in_binary_expr {
88 if let Some(impl_trait) = check_binop(
92 &["Add", "Sub", "Mul", "Div"],
102 SUSPICIOUS_ARITHMETIC_IMPL,
104 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
108 if let Some(impl_trait) = check_binop(
129 hir::BinOpKind::BitAnd,
130 hir::BinOpKind::BitOr,
131 hir::BinOpKind::BitXor,
139 SUSPICIOUS_OP_ASSIGN_IMPL,
141 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
149 cx: &LateContext<'_, '_>,
151 binop: hir::BinOpKind,
153 expected_ops: &[hir::BinOpKind],
154 ) -> Option<&'a str> {
155 let mut trait_ids = vec![];
156 let [krate, module] = crate::utils::paths::OPS_MODULE;
159 let path = [krate, module, t];
160 if let Some(trait_id) = get_trait_def_id(cx, &path) {
161 trait_ids.push(trait_id);
167 // Get the actually implemented trait
168 let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
171 if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
172 if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.def.def_id());
173 if binop != expected_ops[idx];
175 return Some(traits[idx])
182 struct BinaryExprVisitor {
183 in_binary_expr: bool,
186 impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor {
187 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
189 hir::ExprKind::Binary(..)
190 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
191 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => self.in_binary_expr = true,
195 walk_expr(self, expr);
197 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
198 NestedVisitorMap::None