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};
8 /// **What it does:** Lints for suspicious operations in impls of arithmetic operators, e.g.
9 /// subtracting elements in an Add impl.
11 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
13 /// **Known problems:** None.
17 /// impl Add for Foo {
18 /// type Output = Foo;
20 /// fn add(self, other: Foo) -> Foo {
21 /// Foo(self.0 - other.0)
25 declare_clippy_lint! {
26 pub SUSPICIOUS_ARITHMETIC_IMPL,
28 "suspicious use of operators in impl of arithmetic trait"
31 /// **What it does:** Lints for suspicious operations in impls of OpAssign, e.g.
32 /// subtracting elements in an AddAssign impl.
34 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
36 /// **Known problems:** None.
40 /// impl AddAssign for Foo {
41 /// fn add_assign(&mut self, other: Foo) {
42 /// *self = *self - other;
46 declare_clippy_lint! {
47 pub SUSPICIOUS_OP_ASSIGN_IMPL,
49 "suspicious use of operators in impl of OpAssign trait"
52 #[derive(Copy, Clone)]
53 pub struct SuspiciousImpl;
55 impl LintPass for SuspiciousImpl {
56 fn get_lints(&self) -> LintArray {
57 lint_array![SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]
60 fn name(&self) -> &'static str {
65 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
66 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
67 if let hir::ExprKind::Binary(binop, _, _) = expr.node {
74 | hir::BinOpKind::Gt => return,
77 // Check if the binary expression is part of another bi/unary expression
79 let mut parent_expr = cx.tcx.hir().get_parent_node_by_hir_id(expr.hir_id);
80 while parent_expr != hir::CRATE_HIR_ID {
81 if let hir::Node::Expr(e) = cx.tcx.hir().get_by_hir_id(parent_expr) {
83 hir::ExprKind::Binary(..)
84 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
85 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => return,
89 parent_expr = cx.tcx.hir().get_parent_node_by_hir_id(parent_expr);
92 let mut visitor = BinaryExprVisitor { 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,
115 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
119 if let Some(impl_trait) = check_binop(
140 hir::BinOpKind::BitAnd,
141 hir::BinOpKind::BitOr,
142 hir::BinOpKind::BitXor,
150 SUSPICIOUS_OP_ASSIGN_IMPL,
152 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
160 cx: &LateContext<'_, '_>,
162 binop: hir::BinOpKind,
164 expected_ops: &[hir::BinOpKind],
165 ) -> Option<&'a str> {
166 let mut trait_ids = vec![];
167 let [krate, module] = crate::utils::paths::OPS_MODULE;
170 let path = [krate, module, t];
171 if let Some(trait_id) = get_trait_def_id(cx, &path) {
172 trait_ids.push(trait_id);
178 // Get the actually implemented trait
179 let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
180 let parent_impl = cx.tcx.hir().get_parent_item(parent_fn);
183 if parent_impl != hir::CRATE_HIR_ID;
184 if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl);
185 if let hir::ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node;
186 if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.def.def_id());
187 if binop != expected_ops[idx];
189 return Some(traits[idx])
196 struct BinaryExprVisitor {
197 in_binary_expr: bool,
200 impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor {
201 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
203 hir::ExprKind::Binary(..)
204 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
205 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => self.in_binary_expr = true,
209 walk_expr(self, expr);
211 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
212 NestedVisitorMap::None