Auto merge of #8144 - Gh0stm4chine:master, r=xFrednet
Add suggestion for neg_multiply lint This fixes #8115 by adding a suggestion for [neg_multiply]. My first issue on Github, any feedback or input is welcome 😃 changelog: create a suggestion for `neg_multiply`
This commit is contained in:
commit
9ae40436d2
@ -1,6 +1,8 @@
|
|||||||
use clippy_utils::consts::{self, Constant};
|
use clippy_utils::consts::{self, Constant};
|
||||||
use clippy_utils::diagnostics::span_lint;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
|
use clippy_utils::source::snippet_with_applicability;
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
|
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
@ -18,12 +20,16 @@ declare_clippy_lint! {
|
|||||||
///
|
///
|
||||||
/// ### Example
|
/// ### Example
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
/// x * -1
|
/// // Bad
|
||||||
|
/// let a = x * -1;
|
||||||
|
///
|
||||||
|
/// // Good
|
||||||
|
/// let b = -x;
|
||||||
/// ```
|
/// ```
|
||||||
#[clippy::version = "pre 1.29.0"]
|
#[clippy::version = "pre 1.29.0"]
|
||||||
pub NEG_MULTIPLY,
|
pub NEG_MULTIPLY,
|
||||||
style,
|
style,
|
||||||
"multiplying integers with `-1`"
|
"multiplying integers by `-1`"
|
||||||
}
|
}
|
||||||
|
|
||||||
declare_lint_pass!(NegMultiply => [NEG_MULTIPLY]);
|
declare_lint_pass!(NegMultiply => [NEG_MULTIPLY]);
|
||||||
@ -49,8 +55,19 @@ fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
|
|||||||
if let ExprKind::Lit(ref l) = lit.kind;
|
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 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 cx.typeck_results().expr_ty(exp).is_integral();
|
||||||
|
|
||||||
then {
|
then {
|
||||||
span_lint(cx, NEG_MULTIPLY, span, "negation by multiplying with `-1`");
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
|
let suggestion = format!("-{}", snippet_with_applicability(cx, exp.span, "..", &mut applicability));
|
||||||
|
span_lint_and_sugg(
|
||||||
|
cx,
|
||||||
|
NEG_MULTIPLY,
|
||||||
|
span,
|
||||||
|
"this multiplication by -1 can be written more succinctly",
|
||||||
|
"consider using",
|
||||||
|
suggestion,
|
||||||
|
applicability,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
45
tests/ui/neg_multiply.fixed
Normal file
45
tests/ui/neg_multiply.fixed
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// run-rustfix
|
||||||
|
#![warn(clippy::neg_multiply)]
|
||||||
|
#![allow(clippy::no_effect, clippy::unnecessary_operation, clippy::precedence)]
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
use std::ops::Mul;
|
||||||
|
|
||||||
|
struct X;
|
||||||
|
|
||||||
|
impl Mul<isize> for X {
|
||||||
|
type Output = X;
|
||||||
|
|
||||||
|
fn mul(self, _r: isize) -> Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul<X> for isize {
|
||||||
|
type Output = X;
|
||||||
|
|
||||||
|
fn mul(self, _r: X) -> X {
|
||||||
|
X
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = 0;
|
||||||
|
|
||||||
|
-x;
|
||||||
|
|
||||||
|
-x;
|
||||||
|
|
||||||
|
100 + -x;
|
||||||
|
|
||||||
|
-(100 + x);
|
||||||
|
|
||||||
|
-17;
|
||||||
|
|
||||||
|
0xcafe | -0xff00;
|
||||||
|
|
||||||
|
-1 * -1; // should be ok
|
||||||
|
|
||||||
|
X * -1; // should be ok
|
||||||
|
-1 * X; // should also be ok
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
|
// run-rustfix
|
||||||
#![warn(clippy::neg_multiply)]
|
#![warn(clippy::neg_multiply)]
|
||||||
#![allow(clippy::no_effect, clippy::unnecessary_operation)]
|
#![allow(clippy::no_effect, clippy::unnecessary_operation, clippy::precedence)]
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
use std::ops::Mul;
|
use std::ops::Mul;
|
||||||
|
|
||||||
@ -28,6 +30,14 @@ fn main() {
|
|||||||
|
|
||||||
-1 * x;
|
-1 * x;
|
||||||
|
|
||||||
|
100 + x * -1;
|
||||||
|
|
||||||
|
(100 + x) * -1;
|
||||||
|
|
||||||
|
-1 * 17;
|
||||||
|
|
||||||
|
0xcafe | 0xff00 * -1;
|
||||||
|
|
||||||
-1 * -1; // should be ok
|
-1 * -1; // should be ok
|
||||||
|
|
||||||
X * -1; // should be ok
|
X * -1; // should be ok
|
||||||
|
@ -1,16 +1,40 @@
|
|||||||
error: negation by multiplying with `-1`
|
error: this multiplication by -1 can be written more succinctly
|
||||||
--> $DIR/neg_multiply.rs:27:5
|
--> $DIR/neg_multiply.rs:29:5
|
||||||
|
|
|
|
||||||
LL | x * -1;
|
LL | x * -1;
|
||||||
| ^^^^^^
|
| ^^^^^^ help: consider using: `-x`
|
||||||
|
|
|
|
||||||
= note: `-D clippy::neg-multiply` implied by `-D warnings`
|
= note: `-D clippy::neg-multiply` implied by `-D warnings`
|
||||||
|
|
||||||
error: negation by multiplying with `-1`
|
error: this multiplication by -1 can be written more succinctly
|
||||||
--> $DIR/neg_multiply.rs:29:5
|
--> $DIR/neg_multiply.rs:31:5
|
||||||
|
|
|
|
||||||
LL | -1 * x;
|
LL | -1 * x;
|
||||||
| ^^^^^^
|
| ^^^^^^ help: consider using: `-x`
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: this multiplication by -1 can be written more succinctly
|
||||||
|
--> $DIR/neg_multiply.rs:33:11
|
||||||
|
|
|
||||||
|
LL | 100 + x * -1;
|
||||||
|
| ^^^^^^ help: consider using: `-x`
|
||||||
|
|
||||||
|
error: this multiplication by -1 can be written more succinctly
|
||||||
|
--> $DIR/neg_multiply.rs:35:5
|
||||||
|
|
|
||||||
|
LL | (100 + x) * -1;
|
||||||
|
| ^^^^^^^^^^^^^^ help: consider using: `-(100 + x)`
|
||||||
|
|
||||||
|
error: this multiplication by -1 can be written more succinctly
|
||||||
|
--> $DIR/neg_multiply.rs:37:5
|
||||||
|
|
|
||||||
|
LL | -1 * 17;
|
||||||
|
| ^^^^^^^ help: consider using: `-17`
|
||||||
|
|
||||||
|
error: this multiplication by -1 can be written more succinctly
|
||||||
|
--> $DIR/neg_multiply.rs:39:14
|
||||||
|
|
|
||||||
|
LL | 0xcafe | 0xff00 * -1;
|
||||||
|
| ^^^^^^^^^^^ help: consider using: `-0xff00`
|
||||||
|
|
||||||
|
error: aborting due to 6 previous errors
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user