3 use syntax::codemap::{Span, Spanned};
5 use crate::consts::{self, Constant};
6 use crate::utils::span_lint;
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).
18 declare_clippy_lint! {
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 if let ExprLit(ref l) = lit.node;
50 if let Constant::Int(val) = consts::lit_to_constant(&l.node, cx.tables.expr_ty(lit));
52 if cx.tables.expr_ty(exp).is_integral();
57 "Negation by multiplying with -1");