1 use crate::utils::{get_trait_def_id, span_lint};
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_tool_lint, lint_array};
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 {
71 | hir::BinOpKind::Gt => return,
74 // Check if the binary expression is part of another bi/unary expression
76 let mut parent_expr = cx.tcx.hir().get_parent_node(expr.id);
77 while parent_expr != ast::CRATE_NODE_ID {
78 if let hir::Node::Expr(e) = cx.tcx.hir().get(parent_expr) {
80 hir::ExprKind::Binary(..)
81 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
82 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => return,
86 parent_expr = cx.tcx.hir().get_parent_node(parent_expr);
89 let mut visitor = BinaryExprVisitor { in_binary_expr: false };
90 walk_expr(&mut visitor, expr);
92 if visitor.in_binary_expr {
96 if let Some(impl_trait) = check_binop(
100 &["Add", "Sub", "Mul", "Div"],
110 SUSPICIOUS_ARITHMETIC_IMPL,
112 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
116 if let Some(impl_trait) = check_binop(
137 hir::BinOpKind::BitAnd,
138 hir::BinOpKind::BitOr,
139 hir::BinOpKind::BitXor,
147 SUSPICIOUS_OP_ASSIGN_IMPL,
149 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
157 cx: &LateContext<'_, '_>,
159 binop: hir::BinOpKind,
161 expected_ops: &[hir::BinOpKind],
162 ) -> Option<&'a str> {
163 let mut trait_ids = vec![];
164 let [krate, module] = crate::utils::paths::OPS_MODULE;
167 let path = [krate, module, t];
168 if let Some(trait_id) = get_trait_def_id(cx, &path) {
169 trait_ids.push(trait_id);
175 // Get the actually implemented trait
176 let parent_fn = cx.tcx.hir().get_parent(expr.id);
177 let parent_impl = cx.tcx.hir().get_parent(parent_fn);
180 if parent_impl != ast::CRATE_NODE_ID;
181 if let hir::Node::Item(item) = cx.tcx.hir().get(parent_impl);
182 if let hir::ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node;
183 if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.def.def_id());
184 if binop != expected_ops[idx];
186 return Some(traits[idx])
193 struct BinaryExprVisitor {
194 in_binary_expr: bool,
197 impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor {
198 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
200 hir::ExprKind::Binary(..)
201 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
202 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => self.in_binary_expr = true,
206 walk_expr(self, expr);
208 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
209 NestedVisitorMap::None