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};
6 use rustc_middle::hir::map::Map;
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 /// **What it does:** Lints for suspicious operations in impls of arithmetic operators, e.g.
11 /// subtracting elements in an Add impl.
13 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
15 /// **Known problems:** None.
19 /// impl Add for Foo {
20 /// type Output = Foo;
22 /// fn add(self, other: Foo) -> Foo {
23 /// Foo(self.0 - other.0)
27 pub SUSPICIOUS_ARITHMETIC_IMPL,
29 "suspicious use of operators in impl of arithmetic trait"
32 declare_clippy_lint! {
33 /// **What it does:** Lints for suspicious operations in impls of OpAssign, e.g.
34 /// subtracting elements in an AddAssign impl.
36 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
38 /// **Known problems:** None.
42 /// impl AddAssign for Foo {
43 /// fn add_assign(&mut self, other: Foo) {
44 /// *self = *self - other;
48 pub SUSPICIOUS_OP_ASSIGN_IMPL,
50 "suspicious use of operators in impl of OpAssign trait"
53 declare_lint_pass!(SuspiciousImpl => [SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]);
55 impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl {
56 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
57 if let hir::ExprKind::Binary(binop, _, _) | hir::ExprKind::AssignOp(binop, ..) = expr.kind {
64 | hir::BinOpKind::Gt => return,
68 // Check for more than one binary operation in the implemented function
69 // Linting when multiple operations are involved can result in false positives
71 let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
72 if let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get(parent_fn);
73 if let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind;
74 let body = cx.tcx.hir().body(body_id);
75 let mut visitor = BinaryExprVisitor { nb_binops: 0 };
78 walk_expr(&mut visitor, &body.value);
79 if visitor.nb_binops > 1 {
85 if let Some(impl_trait) = check_binop(
90 "Add", "Sub", "Mul", "Div", "Rem", "BitAnd", "BitOr", "BitXor", "Shl", "Shr",
98 hir::BinOpKind::BitAnd,
99 hir::BinOpKind::BitOr,
100 hir::BinOpKind::BitXor,
107 SUSPICIOUS_ARITHMETIC_IMPL,
109 &format!("suspicious use of binary operator in `{}` impl", impl_trait),
113 if let Some(impl_trait) = check_binop(
134 hir::BinOpKind::BitAnd,
135 hir::BinOpKind::BitOr,
136 hir::BinOpKind::BitXor,
144 SUSPICIOUS_OP_ASSIGN_IMPL,
146 &format!("suspicious use of binary operator in `{}` impl", impl_trait),
154 cx: &LateContext<'_>,
155 expr: &hir::Expr<'_>,
156 binop: hir::BinOpKind,
157 traits: &[&'static str],
158 expected_ops: &[hir::BinOpKind],
159 ) -> Option<&'static str> {
160 let mut trait_ids = vec![];
161 let [krate, module] = crate::utils::paths::OPS_MODULE;
164 let path = [krate, module, t];
165 if let Some(trait_id) = get_trait_def_id(cx, &path) {
166 trait_ids.push(trait_id);
172 // Get the actually implemented trait
173 let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
176 if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
177 if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.res.def_id());
178 if binop != expected_ops[idx];
180 return Some(traits[idx])
187 struct BinaryExprVisitor {
191 impl<'tcx> Visitor<'tcx> for BinaryExprVisitor {
192 type Map = Map<'tcx>;
194 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
196 hir::ExprKind::Binary(..)
197 | hir::ExprKind::Unary(hir::UnOp::UnNot | hir::UnOp::UnNeg, _)
198 | hir::ExprKind::AssignOp(..) => self.nb_binops += 1,
202 walk_expr(self, expr);
205 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
206 NestedVisitorMap::None