1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::utils::{get_trait_def_id, span_lint};
11 use if_chain::if_chain;
13 use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
14 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
15 use rustc::{declare_tool_lint, lint_array};
18 /// **What it does:** Lints for suspicious operations in impls of arithmetic operators, e.g.
19 /// subtracting elements in an Add impl.
21 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
23 /// **Known problems:** None.
27 /// impl Add for Foo {
28 /// type Output = Foo;
30 /// fn add(self, other: Foo) -> Foo {
31 /// Foo(self.0 - other.0)
35 declare_clippy_lint! {
36 pub SUSPICIOUS_ARITHMETIC_IMPL,
38 "suspicious use of operators in impl of arithmetic trait"
41 /// **What it does:** Lints for suspicious operations in impls of OpAssign, e.g.
42 /// subtracting elements in an AddAssign impl.
44 /// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
46 /// **Known problems:** None.
50 /// impl AddAssign for Foo {
51 /// fn add_assign(&mut self, other: Foo) {
52 /// *self = *self - other;
56 declare_clippy_lint! {
57 pub SUSPICIOUS_OP_ASSIGN_IMPL,
59 "suspicious use of operators in impl of OpAssign trait"
62 #[derive(Copy, Clone)]
63 pub struct SuspiciousImpl;
65 impl LintPass for SuspiciousImpl {
66 fn get_lints(&self) -> LintArray {
67 lint_array![SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]
71 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
72 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
73 if let hir::ExprKind::Binary(binop, _, _) = expr.node {
80 | hir::BinOpKind::Gt => return,
83 // Check if the binary expression is part of another bi/unary expression
85 let mut parent_expr = cx.tcx.hir().get_parent_node(expr.id);
86 while parent_expr != ast::CRATE_NODE_ID {
87 if let hir::Node::Expr(e) = cx.tcx.hir().get(parent_expr) {
89 hir::ExprKind::Binary(..)
90 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
91 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => return,
95 parent_expr = cx.tcx.hir().get_parent_node(parent_expr);
98 let mut visitor = BinaryExprVisitor { in_binary_expr: false };
99 walk_expr(&mut visitor, expr);
101 if visitor.in_binary_expr {
105 if let Some(impl_trait) = check_binop(
109 &["Add", "Sub", "Mul", "Div"],
119 SUSPICIOUS_ARITHMETIC_IMPL,
121 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
125 if let Some(impl_trait) = check_binop(
146 hir::BinOpKind::BitAnd,
147 hir::BinOpKind::BitOr,
148 hir::BinOpKind::BitXor,
156 SUSPICIOUS_OP_ASSIGN_IMPL,
158 &format!(r#"Suspicious use of binary operator in `{}` impl"#, impl_trait),
166 cx: &LateContext<'_, '_>,
168 binop: hir::BinOpKind,
170 expected_ops: &[hir::BinOpKind],
171 ) -> Option<&'a str> {
172 let mut trait_ids = vec![];
173 let [krate, module] = crate::utils::paths::OPS_MODULE;
176 let path = [krate, module, t];
177 if let Some(trait_id) = get_trait_def_id(cx, &path) {
178 trait_ids.push(trait_id);
184 // Get the actually implemented trait
185 let parent_fn = cx.tcx.hir().get_parent(expr.id);
186 let parent_impl = cx.tcx.hir().get_parent(parent_fn);
189 if parent_impl != ast::CRATE_NODE_ID;
190 if let hir::Node::Item(item) = cx.tcx.hir().get(parent_impl);
191 if let hir::ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node;
192 if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.def.def_id());
193 if binop != expected_ops[idx];
195 return Some(traits[idx])
202 struct BinaryExprVisitor {
203 in_binary_expr: bool,
206 impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor {
207 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
209 hir::ExprKind::Binary(..)
210 | hir::ExprKind::Unary(hir::UnOp::UnNot, _)
211 | hir::ExprKind::Unary(hir::UnOp::UnNeg, _) => self.in_binary_expr = true,
215 walk_expr(self, expr);
217 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
218 NestedVisitorMap::None