3 use syntax::codemap::{Span, Spanned};
5 use consts::{self, Constant};
8 /// **What it does:** Checks for multiplication by -1 as a form of negation.
10 /// **Why is this bad?** It's more readable to just negate.
12 /// **Known problems:** This only catches integers (for now)
14 /// **Example:** `x * -1`
18 "Warns on multiplying integers with -1"
21 #[derive(Copy, Clone)]
22 pub struct NegMultiply;
24 impl LintPass for NegMultiply {
25 fn get_lints(&self) -> LintArray {
26 lint_array!(NEG_MULTIPLY)
30 #[allow(match_same_arms)]
31 impl LateLintPass for NegMultiply {
32 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
33 if let ExprBinary(Spanned { node: BiMul, .. }, ref l, ref r) = e.node {
34 match (&l.node, &r.node) {
35 (&ExprUnary(..), &ExprUnary(..)) => (),
36 (&ExprUnary(UnNeg, ref lit), _) => check_mul(cx, e.span, lit, r),
37 (_, &ExprUnary(UnNeg, ref lit)) => check_mul(cx, e.span, lit, l),
44 fn check_mul(cx: &LateContext, span: Span, lit: &Expr, exp: &Expr) {
46 let ExprLit(ref l) = lit.node,
47 let Constant::Int(ref ci) = consts::lit_to_constant(&l.node),
48 let Some(val) = ci.to_u64(),
50 cx.tcx.expr_ty(exp).is_integral()
55 "Negation by multiplying with -1");