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<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
56 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<'_>) {
57 if let hir::ExprKind::Binary(binop, _, _) | hir::ExprKind::AssignOp(binop, ..) = expr.kind {
64 | hir::BinOpKind::Gt => return,
67 // Check if the binary expression is part of another bi/unary expression
68 // or operator assignment as a child node
69 let mut parent_expr = cx.tcx.hir().get_parent_node(expr.hir_id);
70 while parent_expr != hir::CRATE_HIR_ID {
71 if let hir::Node::Expr(e) = cx.tcx.hir().get(parent_expr) {
73 hir::ExprKind::Binary(..)
74 | hir::ExprKind::Unary(hir::UnOp::UnNot | hir::UnOp::UnNeg, _)
75 | hir::ExprKind::AssignOp(..) => return,
79 parent_expr = cx.tcx.hir().get_parent_node(parent_expr);
82 let mut visitor = BinaryExprVisitor { in_binary_expr: false };
83 walk_expr(&mut visitor, expr);
85 if visitor.in_binary_expr {
89 if let Some(impl_trait) = check_binop(
93 &["Add", "Sub", "Mul", "Div"],
103 SUSPICIOUS_ARITHMETIC_IMPL,
105 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
109 if let Some(impl_trait) = check_binop(
130 hir::BinOpKind::BitAnd,
131 hir::BinOpKind::BitOr,
132 hir::BinOpKind::BitXor,
140 SUSPICIOUS_OP_ASSIGN_IMPL,
142 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
150 cx: &LateContext<'_, '_>,
151 expr: &hir::Expr<'_>,
152 binop: hir::BinOpKind,
153 traits: &[&'static str],
154 expected_ops: &[hir::BinOpKind],
155 ) -> Option<&'static str> {
156 let mut trait_ids = vec![];
157 let [krate, module] = crate::utils::paths::OPS_MODULE;
160 let path = [krate, module, t];
161 if let Some(trait_id) = get_trait_def_id(cx, &path) {
162 trait_ids.push(trait_id);
168 // Get the actually implemented trait
169 let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
172 if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
173 if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.res.def_id());
174 if binop != expected_ops[idx];
176 return Some(traits[idx])
183 struct BinaryExprVisitor {
184 in_binary_expr: bool,
187 impl<'a, 'tcx> Visitor<'tcx> for BinaryExprVisitor {
188 type Map = Map<'tcx>;
190 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
192 hir::ExprKind::Binary(..)
193 | hir::ExprKind::Unary(hir::UnOp::UnNot | hir::UnOp::UnNeg, _)
194 | hir::ExprKind::AssignOp(..) => self.in_binary_expr = true,
198 walk_expr(self, expr);
200 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
201 NestedVisitorMap::None