1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::{get_trait_def_id, paths, trait_ref_of_method};
3 use if_chain::if_chain;
5 use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_middle::hir::map::Map;
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
11 /// **What it does:** Lints for suspicious operations in impls of arithmetic operators, e.g.
12 /// subtracting elements in an Add impl.
14 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
16 /// **Known problems:** None.
20 /// impl Add for Foo {
21 /// type Output = Foo;
23 /// fn add(self, other: Foo) -> Foo {
24 /// Foo(self.0 - other.0)
28 pub SUSPICIOUS_ARITHMETIC_IMPL,
30 "suspicious use of operators in impl of arithmetic trait"
33 declare_clippy_lint! {
34 /// **What it does:** Lints for suspicious operations in impls of OpAssign, e.g.
35 /// subtracting elements in an AddAssign impl.
37 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
39 /// **Known problems:** None.
43 /// impl AddAssign for Foo {
44 /// fn add_assign(&mut self, other: Foo) {
45 /// *self = *self - other;
49 pub SUSPICIOUS_OP_ASSIGN_IMPL,
51 "suspicious use of operators in impl of OpAssign trait"
54 declare_lint_pass!(SuspiciousImpl => [SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]);
56 impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
57 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
58 if let hir::ExprKind::Binary(binop, _, _) | hir::ExprKind::AssignOp(binop, ..) = expr.kind {
65 | hir::BinOpKind::Gt => return,
69 // Check for more than one binary operation in the implemented function
70 // Linting when multiple operations are involved can result in false positives
72 let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
73 if let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get(parent_fn);
74 if let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind;
75 let body = cx.tcx.hir().body(body_id);
76 let mut visitor = BinaryExprVisitor { nb_binops: 0 };
79 walk_expr(&mut visitor, &body.value);
80 if visitor.nb_binops > 1 {
86 if let Some(impl_trait) = check_binop(
91 "Add", "Sub", "Mul", "Div", "Rem", "BitAnd", "BitOr", "BitXor", "Shl", "Shr",
99 hir::BinOpKind::BitAnd,
100 hir::BinOpKind::BitOr,
101 hir::BinOpKind::BitXor,
108 SUSPICIOUS_ARITHMETIC_IMPL,
110 &format!("suspicious use of binary operator in `{}` impl", impl_trait),
114 if let Some(impl_trait) = check_binop(
135 hir::BinOpKind::BitAnd,
136 hir::BinOpKind::BitOr,
137 hir::BinOpKind::BitXor,
145 SUSPICIOUS_OP_ASSIGN_IMPL,
147 &format!("suspicious use of binary operator in `{}` impl", impl_trait),
155 cx: &LateContext<'_>,
156 expr: &hir::Expr<'_>,
157 binop: hir::BinOpKind,
158 traits: &[&'static str],
159 expected_ops: &[hir::BinOpKind],
160 ) -> Option<&'static str> {
161 let mut trait_ids = vec![];
162 let [krate, module] = paths::OPS_MODULE;
165 let path = [krate, module, t];
166 if let Some(trait_id) = get_trait_def_id(cx, &path) {
167 trait_ids.push(trait_id);
173 // Get the actually implemented trait
174 let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
177 if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
178 if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.res.def_id());
179 if binop != expected_ops[idx];
181 return Some(traits[idx])
188 struct BinaryExprVisitor {
192 impl<'tcx> Visitor<'tcx> for BinaryExprVisitor {
193 type Map = Map<'tcx>;
195 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
197 hir::ExprKind::Binary(..)
198 | hir::ExprKind::Unary(hir::UnOp::Not | hir::UnOp::Neg, _)
199 | hir::ExprKind::AssignOp(..) => self.nb_binops += 1,
203 walk_expr(self, expr);
206 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
207 NestedVisitorMap::None