Skip to content

Commit 88e40bc

Browse files
committed
Add support for suggestion when using an expression
1 parent 5ad37b1 commit 88e40bc

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed

clippy_lints/src/neg_multiply.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use clippy_utils::consts::{self, Constant};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::source::snippet_with_applicability;
34
use if_chain::if_chain;
45
use rustc_errors::Applicability;
5-
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, UnOp};
6+
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
67
use rustc_lint::{LateContext, LateLintPass};
78
use rustc_session::{declare_lint_pass, declare_tool_lint};
89
use rustc_span::source_map::Span;
@@ -28,7 +29,7 @@ declare_clippy_lint! {
2829
#[clippy::version = "pre 1.29.0"]
2930
pub NEG_MULTIPLY,
3031
style,
31-
"multiplying integers with `-1`"
32+
"multiplying integers by `-1`"
3233
}
3334

3435
declare_lint_pass!(NegMultiply => [NEG_MULTIPLY]);
@@ -54,17 +55,15 @@ fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
5455
if let ExprKind::Lit(ref l) = lit.kind;
5556
if consts::lit_to_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)) == Constant::Int(1);
5657
if cx.typeck_results().expr_ty(exp).is_integral();
57-
if let ExprKind::Path(QPath::Resolved(_, var_path)) = exp.kind;
5858

5959
then {
60-
let var_name = var_path.segments[0].ident.name.as_str();
61-
let suggestion = format!("-{var}",var=var_name);
62-
let applicability = Applicability::MachineApplicable;
60+
let mut applicability = Applicability::MachineApplicable;
61+
let suggestion = format!("-{}", snippet_with_applicability(cx, exp.span, "..", &mut applicability));
6362
span_lint_and_sugg(
6463
cx,
6564
NEG_MULTIPLY,
6665
span,
67-
"this `multiplication with -1` can be written more succinctly",
66+
"this multiplication by -1 can be written more succinctly",
6867
"consider using",
6968
suggestion,
7069
applicability,

tests/ui/neg_multiply.fixed

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

tests/ui/neg_multiply.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
#![warn(clippy::neg_multiply)]
23
#![allow(clippy::no_effect, clippy::unnecessary_operation)]
34

@@ -28,6 +29,14 @@ fn main() {
2829

2930
-1 * x;
3031

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

3342
X * -1; // should be ok

tests/ui/neg_multiply.stderr

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
1-
error: this `multiplication with -1` can be written more succinctly
2-
--> $DIR/neg_multiply.rs:27:5
1+
error: operator precedence can trip the unwary
2+
--> $DIR/neg_multiply.rs:38:5
3+
|
4+
LL | 0xcafe | 0xff00 * -1;
5+
| ^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `0xcafe | (0xff00 * -1)`
6+
|
7+
= note: `-D clippy::precedence` implied by `-D warnings`
8+
9+
error: this multiplication by -1 can be written more succinctly
10+
--> $DIR/neg_multiply.rs:28:5
311
|
412
LL | x * -1;
513
| ^^^^^^ help: consider using: `-x`
614
|
715
= note: `-D clippy::neg-multiply` implied by `-D warnings`
816

9-
error: this `multiplication with -1` can be written more succinctly
10-
--> $DIR/neg_multiply.rs:29:5
17+
error: this multiplication by -1 can be written more succinctly
18+
--> $DIR/neg_multiply.rs:30:5
1119
|
1220
LL | -1 * x;
1321
| ^^^^^^ help: consider using: `-x`
1422

15-
error: aborting due to 2 previous errors
23+
error: this multiplication by -1 can be written more succinctly
24+
--> $DIR/neg_multiply.rs:32:11
25+
|
26+
LL | 100 + x * -1;
27+
| ^^^^^^ help: consider using: `-x`
28+
29+
error: this multiplication by -1 can be written more succinctly
30+
--> $DIR/neg_multiply.rs:34:5
31+
|
32+
LL | (100 + x) * -1;
33+
| ^^^^^^^^^^^^^^ help: consider using: `-(100 + x)`
34+
35+
error: this multiplication by -1 can be written more succinctly
36+
--> $DIR/neg_multiply.rs:36:5
37+
|
38+
LL | -1 * 17;
39+
| ^^^^^^^ help: consider using: `-17`
40+
41+
error: this multiplication by -1 can be written more succinctly
42+
--> $DIR/neg_multiply.rs:38:14
43+
|
44+
LL | 0xcafe | 0xff00 * -1;
45+
| ^^^^^^^^^^^ help: consider using: `-0xff00`
46+
47+
error: aborting due to 7 previous errors
1648

0 commit comments

Comments
 (0)