1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use rustc::lint::*;
use rustc::hir;
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use syntax::ast;
use utils::{get_trait_def_id, span_lint};

/// **What it does:** Lints for suspicious operations in impls of arithmetic operators, e.g.
/// subtracting elements in an Add impl.
///
/// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// impl Add for Foo {
///     type Output = Foo;
///
///     fn add(self, other: Foo) -> Foo {
///         Foo(self.0 - other.0)
///     }
/// }
/// ```
declare_clippy_lint! {
    pub SUSPICIOUS_ARITHMETIC_IMPL,
    correctness,
    "suspicious use of operators in impl of arithmetic trait"
}

/// **What it does:** Lints for suspicious operations in impls of OpAssign, e.g.
/// subtracting elements in an AddAssign impl.
///
/// **Why this is bad?** This is probably a typo or copy-and-paste error and not intended.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// impl AddAssign for Foo {
///     fn add_assign(&mut self, other: Foo) {
///         *self = *self - other;
///     }
/// }
/// ```
declare_clippy_lint! {
    pub SUSPICIOUS_OP_ASSIGN_IMPL,
    correctness,
    "suspicious use of operators in impl of OpAssign trait"
}

#[derive(Copy, Clone)]
pub struct SuspiciousImpl;

impl LintPass for SuspiciousImpl {
    fn get_lints(&self) -> LintArray {
        lint_array![SUSPICIOUS_ARITHMETIC_IMPL, SUSPICIOUS_OP_ASSIGN_IMPL]
    }
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SuspiciousImpl {
    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
        use rustc::hir::BinOp_::*;
        if let hir::ExprBinary(binop, _, _) = expr.node {
            match binop.node {
                BiEq | BiLt | BiLe | BiNe | BiGe | BiGt => return,
                _ => {},
            }
            // Check if the binary expression is part of another bi/unary expression
            // as a child node
            let mut parent_expr = cx.tcx.hir.get_parent_node(expr.id);
            while parent_expr != ast::CRATE_NODE_ID {
                if let hir::map::Node::NodeExpr(e) = cx.tcx.hir.get(parent_expr) {
                    match e.node {
                        hir::ExprBinary(..)
                        | hir::ExprUnary(hir::UnOp::UnNot, _)
                        | hir::ExprUnary(hir::UnOp::UnNeg, _) => return,
                        _ => {},
                    }
                }
                parent_expr = cx.tcx.hir.get_parent_node(parent_expr);
            }
            // as a parent node
            let mut visitor = BinaryExprVisitor {
                in_binary_expr: false,
            };
            walk_expr(&mut visitor, expr);

            if visitor.in_binary_expr {
                return;
            }

            if let Some(impl_trait) = check_binop(
                cx,
                expr,
                &binop.node,
                &["Add", "Sub", "Mul", "Div"],
                &[BiAdd, BiSub, BiMul, BiDiv],
            ) {
                span_lint(
                    cx,
                    SUSPICIOUS_ARITHMETIC_IMPL,
                    binop.span,
                    &format!(
                        r#"Suspicious use of binary operator in `{}` impl"#,
                        impl_trait
                    ),
                );
            }

            if let Some(impl_trait) = check_binop(
                cx,
                expr,
                &binop.node,
                &[
                    "AddAssign",
                    "SubAssign",
                    "MulAssign",
                    "DivAssign",
                    "BitAndAssign",
                    "BitOrAssign",
                    "BitXorAssign",
                    "RemAssign",
                    "ShlAssign",
                    "ShrAssign",
                ],
                &[
                    BiAdd, BiSub, BiMul, BiDiv, BiBitAnd, BiBitOr, BiBitXor, BiRem, BiShl, BiShr
                ],
            ) {
                span_lint(
                    cx,
                    SUSPICIOUS_OP_ASSIGN_IMPL,
                    binop.span,
                    &format!(
                        r#"Suspicious use of binary operator in `{}` impl"#,
                        impl_trait
                    ),
                );
            }
        }
    }
}

fn check_binop<'a>(
    cx: &LateContext,
    expr: &hir::Expr,
    binop: &hir::BinOp_,
    traits: &[&'a str],
    expected_ops: &[hir::BinOp_],
) -> Option<&'a str> {
    let mut trait_ids = vec![];
    let [krate, module] = ::utils::paths::OPS_MODULE;

    for t in traits {
        let path = [krate, module, t];
        if let Some(trait_id) = get_trait_def_id(cx, &path) {
            trait_ids.push(trait_id);
        } else {
            return None;
        }
    }

    // Get the actually implemented trait
    let parent_fn = cx.tcx.hir.get_parent(expr.id);
    let parent_impl = cx.tcx.hir.get_parent(parent_fn);

    if_chain! {
        if parent_impl != ast::CRATE_NODE_ID;
        if let hir::map::Node::NodeItem(item) = cx.tcx.hir.get(parent_impl);
        if let hir::Item_::ItemImpl(_, _, _, _, Some(ref trait_ref), _, _) = item.node;
        if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.def.def_id());
        if *binop != expected_ops[idx];
        then{
            return Some(traits[idx])
        }
    }

    None
}

struct BinaryExprVisitor {
    in_binary_expr: bool,
}

impl<'a, 'tcx: 'a> Visitor<'tcx> for BinaryExprVisitor {
    fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
        match expr.node {
            hir::ExprBinary(..)
            | hir::ExprUnary(hir::UnOp::UnNot, _)
            | hir::ExprUnary(hir::UnOp::UnNeg, _) => {
                self.in_binary_expr = true
            },
            _ => {},
        }

        walk_expr(self, expr);
    }
    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
        NestedVisitorMap::None
    }
}