From e7208ae2799e2754c78d67f7f2b2dc0f3814a614 Mon Sep 17 00:00:00 2001 From: Florian Hartwig Date: Sun, 28 Oct 2018 20:30:13 +0100 Subject: [PATCH] Extend neg_multiply lint to *= assignments --- clippy_lints/src/neg_multiply.rs | 6 ++++++ tests/ui/neg_multiply.rs | 4 +++- tests/ui/neg_multiply.stderr | 8 +++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/neg_multiply.rs b/clippy_lints/src/neg_multiply.rs index d3b72372c2f7..14da52fd5eae 100644 --- a/clippy_lints/src/neg_multiply.rs +++ b/clippy_lints/src/neg_multiply.rs @@ -53,6 +53,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NegMultiply { _ => (), } } + + if let ExprKind::AssignOp(Spanned {node: BinOpKind::Mul, .. }, _, ref rhs) = e.node { + if let ExprKind::Unary(UnNeg, ref lit) = rhs.node { + check_mul(cx, e.span, lit, rhs) + } + } } } diff --git a/tests/ui/neg_multiply.rs b/tests/ui/neg_multiply.rs index 446af7bbe948..2b7e7c3cab66 100644 --- a/tests/ui/neg_multiply.rs +++ b/tests/ui/neg_multiply.rs @@ -35,12 +35,14 @@ impl Mul for isize { } fn main() { - let x = 0; + let mut x = 0; x * -1; -1 * x; + x *= -1; + -1 * -1; // should be ok X * -1; // should be ok diff --git a/tests/ui/neg_multiply.stderr b/tests/ui/neg_multiply.stderr index ed96dd519ff6..14b3ee37d3e0 100644 --- a/tests/ui/neg_multiply.stderr +++ b/tests/ui/neg_multiply.stderr @@ -12,5 +12,11 @@ error: Negation by multiplying with -1 42 | -1 * x; | ^^^^^^ -error: aborting due to 2 previous errors +error: Negation by multiplying with -1 + --> $DIR/neg_multiply.rs:44:5 + | +44 | x *= -1; + | ^^^^^^^ + +error: aborting due to 3 previous errors