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).
21 "multiplying integers with -1"
24 #[derive(Copy, Clone)]
25 pub struct NegMultiply;
27 impl LintPass for NegMultiply {
28 fn get_lints(&self) -> LintArray {
29 lint_array!(NEG_MULTIPLY)
33 #[allow(match_same_arms)]
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NegMultiply {
35 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
36 if let ExprBinary(Spanned { node: BiMul, .. }, ref l, ref r) = e.node {
37 match (&l.node, &r.node) {
38 (&ExprUnary(..), &ExprUnary(..)) => (),
39 (&ExprUnary(UnNeg, ref lit), _) => check_mul(cx, e.span, lit, r),
40 (_, &ExprUnary(UnNeg, ref lit)) => check_mul(cx, e.span, lit, l),
47 fn check_mul(cx: &LateContext, span: Span, lit: &Expr, exp: &Expr) {
49 let ExprLit(ref l) = lit.node,
50 let Constant::Int(ref ci) = consts::lit_to_constant(&l.node),
51 let Some(val) = ci.to_u64(),
53 cx.tables.expr_ty(exp).is_integral()
58 "Negation by multiplying with -1");