From 5ad37b1a4b34af381b93f603e4c948928a8a6124 Mon Sep 17 00:00:00 2001 From: Oussama Date: Sun, 19 Dec 2021 09:48:25 +0100 Subject: [PATCH] add suggestion for neg_multiply lint --- clippy_lints/src/neg_multiply.rs | 26 ++++++++++++++++++++++---- tests/ui/neg_multiply.stderr | 8 ++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/neg_multiply.rs b/clippy_lints/src/neg_multiply.rs index cb67fab17..78645e50b 100644 --- a/clippy_lints/src/neg_multiply.rs +++ b/clippy_lints/src/neg_multiply.rs @@ -1,7 +1,8 @@ use clippy_utils::consts::{self, Constant}; -use clippy_utils::diagnostics::span_lint; +use clippy_utils::diagnostics::span_lint_and_sugg; use if_chain::if_chain; -use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp}; +use rustc_errors::Applicability; +use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::source_map::Span; @@ -18,7 +19,11 @@ declare_clippy_lint! { /// /// ### Example /// ```ignore - /// x * -1 + /// // Bad + /// let a = x * -1; + /// + /// // Good + /// let b = -x; /// ``` #[clippy::version = "pre 1.29.0"] pub NEG_MULTIPLY, @@ -49,8 +54,21 @@ fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) { if let ExprKind::Lit(ref l) = lit.kind; if consts::lit_to_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)) == Constant::Int(1); if cx.typeck_results().expr_ty(exp).is_integral(); + if let ExprKind::Path(QPath::Resolved(_, var_path)) = exp.kind; + then { - span_lint(cx, NEG_MULTIPLY, span, "negation by multiplying with `-1`"); + let var_name = var_path.segments[0].ident.name.as_str(); + let suggestion = format!("-{var}",var=var_name); + let applicability = Applicability::MachineApplicable; + span_lint_and_sugg( + cx, + NEG_MULTIPLY, + span, + "this `multiplication with -1` can be written more succinctly", + "consider using", + suggestion, + applicability, + ); } } } diff --git a/tests/ui/neg_multiply.stderr b/tests/ui/neg_multiply.stderr index ad677f6d6..ce5f46fca 100644 --- a/tests/ui/neg_multiply.stderr +++ b/tests/ui/neg_multiply.stderr @@ -1,16 +1,16 @@ -error: negation by multiplying with `-1` +error: this `multiplication with -1` can be written more succinctly --> $DIR/neg_multiply.rs:27:5 | LL | x * -1; - | ^^^^^^ + | ^^^^^^ help: consider using: `-x` | = note: `-D clippy::neg-multiply` implied by `-D warnings` -error: negation by multiplying with `-1` +error: this `multiplication with -1` can be written more succinctly --> $DIR/neg_multiply.rs:29:5 | LL | -1 * x; - | ^^^^^^ + | ^^^^^^ help: consider using: `-x` error: aborting due to 2 previous errors