Skip to content

Commit 9ae4043

Browse files
committed
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`
2 parents fea103d + 13cc452 commit 9ae4043

File tree

4 files changed

+108
-12
lines changed

4 files changed

+108
-12
lines changed

clippy_lints/src/neg_multiply.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use clippy_utils::consts::{self, Constant};
2-
use clippy_utils::diagnostics::span_lint;
2+
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::source::snippet_with_applicability;
34
use if_chain::if_chain;
5+
use rustc_errors::Applicability;
46
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
57
use rustc_lint::{LateContext, LateLintPass};
68
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -18,12 +20,16 @@ declare_clippy_lint! {
1820
///
1921
/// ### Example
2022
/// ```ignore
21-
/// x * -1
23+
/// // Bad
24+
/// let a = x * -1;
25+
///
26+
/// // Good
27+
/// let b = -x;
2228
/// ```
2329
#[clippy::version = "pre 1.29.0"]
2430
pub NEG_MULTIPLY,
2531
style,
26-
"multiplying integers with `-1`"
32+
"multiplying integers by `-1`"
2733
}
2834

2935
declare_lint_pass!(NegMultiply => [NEG_MULTIPLY]);
@@ -49,8 +55,19 @@ fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
4955
if let ExprKind::Lit(ref l) = lit.kind;
5056
if consts::lit_to_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)) == Constant::Int(1);
5157
if cx.typeck_results().expr_ty(exp).is_integral();
58+
5259
then {
53-
span_lint(cx, NEG_MULTIPLY, span, "negation by multiplying with `-1`");
60+
let mut applicability = Applicability::MachineApplicable;
61+
let suggestion = format!("-{}", snippet_with_applicability(cx, exp.span, "..", &mut applicability));
62+
span_lint_and_sugg(
63+
cx,
64+
NEG_MULTIPLY,
65+
span,
66+
"this multiplication by -1 can be written more succinctly",
67+
"consider using",
68+
suggestion,
69+
applicability,
70+
);
5471
}
5572
}
5673
}

tests/ui/neg_multiply.fixed

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// run-rustfix
2+
#![warn(clippy::neg_multiply)]
3+
#![allow(clippy::no_effect, clippy::unnecessary_operation, clippy::precedence)]
4+
#![allow(unused)]
5+
6+
use std::ops::Mul;
7+
8+
struct X;
9+
10+
impl Mul<isize> for X {
11+
type Output = X;
12+
13+
fn mul(self, _r: isize) -> Self {
14+
self
15+
}
16+
}
17+
18+
impl Mul<X> for isize {
19+
type Output = X;
20+
21+
fn mul(self, _r: X) -> X {
22+
X
23+
}
24+
}
25+
26+
fn main() {
27+
let x = 0;
28+
29+
-x;
30+
31+
-x;
32+
33+
100 + -x;
34+
35+
-(100 + x);
36+
37+
-17;
38+
39+
0xcafe | -0xff00;
40+
41+
-1 * -1; // should be ok
42+
43+
X * -1; // should be ok
44+
-1 * X; // should also be ok
45+
}

tests/ui/neg_multiply.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
// run-rustfix
12
#![warn(clippy::neg_multiply)]
2-
#![allow(clippy::no_effect, clippy::unnecessary_operation)]
3+
#![allow(clippy::no_effect, clippy::unnecessary_operation, clippy::precedence)]
4+
#![allow(unused)]
35

46
use std::ops::Mul;
57

@@ -28,6 +30,14 @@ fn main() {
2830

2931
-1 * x;
3032

33+
100 + x * -1;
34+
35+
(100 + x) * -1;
36+
37+
-1 * 17;
38+
39+
0xcafe | 0xff00 * -1;
40+
3141
-1 * -1; // should be ok
3242

3343
X * -1; // should be ok

tests/ui/neg_multiply.stderr

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1-
error: negation by multiplying with `-1`
2-
--> $DIR/neg_multiply.rs:27:5
1+
error: this multiplication by -1 can be written more succinctly
2+
--> $DIR/neg_multiply.rs:29:5
33
|
44
LL | x * -1;
5-
| ^^^^^^
5+
| ^^^^^^ help: consider using: `-x`
66
|
77
= note: `-D clippy::neg-multiply` implied by `-D warnings`
88

9-
error: negation by multiplying with `-1`
10-
--> $DIR/neg_multiply.rs:29:5
9+
error: this multiplication by -1 can be written more succinctly
10+
--> $DIR/neg_multiply.rs:31:5
1111
|
1212
LL | -1 * x;
13-
| ^^^^^^
13+
| ^^^^^^ help: consider using: `-x`
14+
15+
error: this multiplication by -1 can be written more succinctly
16+
--> $DIR/neg_multiply.rs:33:11
17+
|
18+
LL | 100 + x * -1;
19+
| ^^^^^^ help: consider using: `-x`
20+
21+
error: this multiplication by -1 can be written more succinctly
22+
--> $DIR/neg_multiply.rs:35:5
23+
|
24+
LL | (100 + x) * -1;
25+
| ^^^^^^^^^^^^^^ help: consider using: `-(100 + x)`
26+
27+
error: this multiplication by -1 can be written more succinctly
28+
--> $DIR/neg_multiply.rs:37:5
29+
|
30+
LL | -1 * 17;
31+
| ^^^^^^^ help: consider using: `-17`
32+
33+
error: this multiplication by -1 can be written more succinctly
34+
--> $DIR/neg_multiply.rs:39:14
35+
|
36+
LL | 0xcafe | 0xff00 * -1;
37+
| ^^^^^^^^^^^ help: consider using: `-0xff00`
1438

15-
error: aborting due to 2 previous errors
39+
error: aborting due to 6 previous errors
1640

0 commit comments

Comments
 (0)