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, _)
75 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _)
76 | hir::ExprKind::AssignOp(..) => return,
80 parent_expr = cx.tcx.hir().get_parent_node(parent_expr);
83 let mut visitor = BinaryExprVisitor { in_binary_expr: false };
84 walk_expr(&mut visitor, expr);
86 if visitor.in_binary_expr {
90 if let Some(impl_trait) = check_binop(
94 &["Add", "Sub", "Mul", "Div"],
104 SUSPICIOUS_ARITHMETIC_IMPL,
106 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
110 if let Some(impl_trait) = check_binop(
131 hir::BinOpKind::BitAnd,
132 hir::BinOpKind::BitOr,
133 hir::BinOpKind::BitXor,
141 SUSPICIOUS_OP_ASSIGN_IMPL,
143 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
151 cx: &LateContext<'_, '_>,
152 expr: &hir::Expr<'_>,
153 binop: hir::BinOpKind,
154 traits: &[&'static str],
155 expected_ops: &[hir::BinOpKind],
156 ) -> Option<&'static str> {
157 let mut trait_ids = vec![];
158 let [krate, module] = crate::utils::paths::OPS_MODULE;
161 let path = [krate, module, t];
162 if let Some(trait_id) = get_trait_def_id(cx, &path) {
163 trait_ids.push(trait_id);
169 // Get the actually implemented trait
170 let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
173 if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
174 if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.res.def_id());
175 if binop != expected_ops[idx];
177 return Some(traits[idx])
184 struct BinaryExprVisitor {
185 in_binary_expr: bool,
188 impl<'a, 'tcx> Visitor<'tcx> for BinaryExprVisitor {
189 type Map = Map<'tcx>;
191 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
193 hir::ExprKind::Binary(..)
194 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
195 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _)
196 | hir::ExprKind::AssignOp(..) => self.in_binary_expr = true,
200 walk_expr(self, expr);
202 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
203 NestedVisitorMap::None