Skip to content

Commit c418714

Browse files
authored
expand neg_multiply to lint float numbers as well (rust-lang#14447)
changelog: [`neg_multiply`]: lint float numbers as well
2 parents 7a92626 + da4f5a5 commit c418714

File tree

4 files changed

+112
-6
lines changed

4 files changed

+112
-6
lines changed

clippy_lints/src/neg_multiply.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ declare_clippy_lint! {
1616
/// ### Why is this bad?
1717
/// It's more readable to just negate.
1818
///
19-
/// ### Known problems
20-
/// This only catches integers (for now).
21-
///
2219
/// ### Example
2320
/// ```rust,ignore
2421
/// let a = x * -1;
@@ -53,8 +50,11 @@ impl<'tcx> LateLintPass<'tcx> for NegMultiply {
5350

5451
fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
5552
if let ExprKind::Lit(l) = lit.kind
56-
&& consts::lit_to_mir_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)) == Constant::Int(1)
57-
&& cx.typeck_results().expr_ty(exp).is_integral()
53+
&& matches!(
54+
consts::lit_to_mir_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)),
55+
Constant::Int(1) | Constant::F16(1.0) | Constant::F32(1.0) | Constant::F64(1.0) | Constant::F128(1.0)
56+
)
57+
&& cx.typeck_results().expr_ty(exp).is_numeric()
5858
{
5959
let mut applicability = Applicability::MachineApplicable;
6060
let (snip, from_macro) = snippet_with_context(cx, exp.span, span.ctxt(), "..", &mut applicability);

tests/ui/neg_multiply.fixed

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,32 @@ fn main() {
5353
X * -1; // should be ok
5454
-1 * X; // should also be ok
5555
}
56+
57+
fn float() {
58+
let x = 0.0;
59+
60+
-x;
61+
//~^ neg_multiply
62+
63+
-x;
64+
//~^ neg_multiply
65+
66+
100.0 + -x;
67+
//~^ neg_multiply
68+
69+
-(100.0 + x);
70+
//~^ neg_multiply
71+
72+
-17.0;
73+
//~^ neg_multiply
74+
75+
0.0 + -0.0;
76+
//~^ neg_multiply
77+
78+
-(3.0_f32 as f64);
79+
//~^ neg_multiply
80+
-(3.0_f32 as f64);
81+
//~^ neg_multiply
82+
83+
-1.0 * -1.0; // should be ok
84+
}

tests/ui/neg_multiply.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,32 @@ fn main() {
5353
X * -1; // should be ok
5454
-1 * X; // should also be ok
5555
}
56+
57+
fn float() {
58+
let x = 0.0;
59+
60+
x * -1.0;
61+
//~^ neg_multiply
62+
63+
-1.0 * x;
64+
//~^ neg_multiply
65+
66+
100.0 + x * -1.0;
67+
//~^ neg_multiply
68+
69+
(100.0 + x) * -1.0;
70+
//~^ neg_multiply
71+
72+
-1.0 * 17.0;
73+
//~^ neg_multiply
74+
75+
0.0 + 0.0 * -1.0;
76+
//~^ neg_multiply
77+
78+
3.0_f32 as f64 * -1.0;
79+
//~^ neg_multiply
80+
(3.0_f32 as f64) * -1.0;
81+
//~^ neg_multiply
82+
83+
-1.0 * -1.0; // should be ok
84+
}

tests/ui/neg_multiply.stderr

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,53 @@ error: this multiplication by -1 can be written more succinctly
4949
LL | (3_usize as i32) * -1;
5050
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3_usize as i32)`
5151

52-
error: aborting due to 8 previous errors
52+
error: this multiplication by -1 can be written more succinctly
53+
--> tests/ui/neg_multiply.rs:60:5
54+
|
55+
LL | x * -1.0;
56+
| ^^^^^^^^ help: consider using: `-x`
57+
58+
error: this multiplication by -1 can be written more succinctly
59+
--> tests/ui/neg_multiply.rs:63:5
60+
|
61+
LL | -1.0 * x;
62+
| ^^^^^^^^ help: consider using: `-x`
63+
64+
error: this multiplication by -1 can be written more succinctly
65+
--> tests/ui/neg_multiply.rs:66:13
66+
|
67+
LL | 100.0 + x * -1.0;
68+
| ^^^^^^^^ help: consider using: `-x`
69+
70+
error: this multiplication by -1 can be written more succinctly
71+
--> tests/ui/neg_multiply.rs:69:5
72+
|
73+
LL | (100.0 + x) * -1.0;
74+
| ^^^^^^^^^^^^^^^^^^ help: consider using: `-(100.0 + x)`
75+
76+
error: this multiplication by -1 can be written more succinctly
77+
--> tests/ui/neg_multiply.rs:72:5
78+
|
79+
LL | -1.0 * 17.0;
80+
| ^^^^^^^^^^^ help: consider using: `-17.0`
81+
82+
error: this multiplication by -1 can be written more succinctly
83+
--> tests/ui/neg_multiply.rs:75:11
84+
|
85+
LL | 0.0 + 0.0 * -1.0;
86+
| ^^^^^^^^^^ help: consider using: `-0.0`
87+
88+
error: this multiplication by -1 can be written more succinctly
89+
--> tests/ui/neg_multiply.rs:78:5
90+
|
91+
LL | 3.0_f32 as f64 * -1.0;
92+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3.0_f32 as f64)`
93+
94+
error: this multiplication by -1 can be written more succinctly
95+
--> tests/ui/neg_multiply.rs:80:5
96+
|
97+
LL | (3.0_f32 as f64) * -1.0;
98+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3.0_f32 as f64)`
99+
100+
error: aborting due to 16 previous errors
53101

0 commit comments

Comments
 (0)