about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlapla-cogito <me@lapla.dev>2025-03-21 21:20:39 +0900
committerlapla-cogito <me@lapla.dev>2025-03-21 21:20:39 +0900
commitda4f5a58a929145127b5cf82ed271ca7279244b0 (patch)
tree42423dd5b6821b83323489a17fa3dd341d9220e8
parent90f5f559be8eafe833214b20bd36e364ded17097 (diff)
downloadrust-da4f5a58a929145127b5cf82ed271ca7279244b0.tar.gz
rust-da4f5a58a929145127b5cf82ed271ca7279244b0.zip
expand `neg_multiply` to lint float numbers as well
-rw-r--r--clippy_lints/src/neg_multiply.rs10
-rw-r--r--tests/ui/neg_multiply.fixed29
-rw-r--r--tests/ui/neg_multiply.rs29
-rw-r--r--tests/ui/neg_multiply.stderr50
4 files changed, 112 insertions, 6 deletions
diff --git a/clippy_lints/src/neg_multiply.rs b/clippy_lints/src/neg_multiply.rs
index 429afff9b66..86eb833becc 100644
--- a/clippy_lints/src/neg_multiply.rs
+++ b/clippy_lints/src/neg_multiply.rs
@@ -16,9 +16,6 @@ declare_clippy_lint! {
     /// ### Why is this bad?
     /// It's more readable to just negate.
     ///
-    /// ### Known problems
-    /// This only catches integers (for now).
-    ///
     /// ### Example
     /// ```rust,ignore
     /// let a = x * -1;
@@ -53,8 +50,11 @@ impl<'tcx> LateLintPass<'tcx> for NegMultiply {
 
 fn check_mul(cx: &LateContext<'_>, span: Span, lit: &Expr<'_>, exp: &Expr<'_>) {
     if let ExprKind::Lit(l) = lit.kind
-        && consts::lit_to_mir_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)) == Constant::Int(1)
-        && cx.typeck_results().expr_ty(exp).is_integral()
+        && matches!(
+            consts::lit_to_mir_constant(&l.node, cx.typeck_results().expr_ty_opt(lit)),
+            Constant::Int(1) | Constant::F16(1.0) | Constant::F32(1.0) | Constant::F64(1.0) | Constant::F128(1.0)
+        )
+        && cx.typeck_results().expr_ty(exp).is_numeric()
     {
         let mut applicability = Applicability::MachineApplicable;
         let (snip, from_macro) = snippet_with_context(cx, exp.span, span.ctxt(), "..", &mut applicability);
diff --git a/tests/ui/neg_multiply.fixed b/tests/ui/neg_multiply.fixed
index 995470493bf..ff6e08300e2 100644
--- a/tests/ui/neg_multiply.fixed
+++ b/tests/ui/neg_multiply.fixed
@@ -53,3 +53,32 @@ fn main() {
     X * -1; // should be ok
     -1 * X; // should also be ok
 }
+
+fn float() {
+    let x = 0.0;
+
+    -x;
+    //~^ neg_multiply
+
+    -x;
+    //~^ neg_multiply
+
+    100.0 + -x;
+    //~^ neg_multiply
+
+    -(100.0 + x);
+    //~^ neg_multiply
+
+    -17.0;
+    //~^ neg_multiply
+
+    0.0 + -0.0;
+    //~^ neg_multiply
+
+    -(3.0_f32 as f64);
+    //~^ neg_multiply
+    -(3.0_f32 as f64);
+    //~^ neg_multiply
+
+    -1.0 * -1.0; // should be ok
+}
diff --git a/tests/ui/neg_multiply.rs b/tests/ui/neg_multiply.rs
index 95b94e29517..b0f4e85c78e 100644
--- a/tests/ui/neg_multiply.rs
+++ b/tests/ui/neg_multiply.rs
@@ -53,3 +53,32 @@ fn main() {
     X * -1; // should be ok
     -1 * X; // should also be ok
 }
+
+fn float() {
+    let x = 0.0;
+
+    x * -1.0;
+    //~^ neg_multiply
+
+    -1.0 * x;
+    //~^ neg_multiply
+
+    100.0 + x * -1.0;
+    //~^ neg_multiply
+
+    (100.0 + x) * -1.0;
+    //~^ neg_multiply
+
+    -1.0 * 17.0;
+    //~^ neg_multiply
+
+    0.0 + 0.0 * -1.0;
+    //~^ neg_multiply
+
+    3.0_f32 as f64 * -1.0;
+    //~^ neg_multiply
+    (3.0_f32 as f64) * -1.0;
+    //~^ neg_multiply
+
+    -1.0 * -1.0; // should be ok
+}
diff --git a/tests/ui/neg_multiply.stderr b/tests/ui/neg_multiply.stderr
index 9efa5d3ba1f..2ef7e32ce05 100644
--- a/tests/ui/neg_multiply.stderr
+++ b/tests/ui/neg_multiply.stderr
@@ -49,5 +49,53 @@ error: this multiplication by -1 can be written more succinctly
 LL |     (3_usize as i32) * -1;
    |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3_usize as i32)`
 
-error: aborting due to 8 previous errors
+error: this multiplication by -1 can be written more succinctly
+  --> tests/ui/neg_multiply.rs:60:5
+   |
+LL |     x * -1.0;
+   |     ^^^^^^^^ help: consider using: `-x`
+
+error: this multiplication by -1 can be written more succinctly
+  --> tests/ui/neg_multiply.rs:63:5
+   |
+LL |     -1.0 * x;
+   |     ^^^^^^^^ help: consider using: `-x`
+
+error: this multiplication by -1 can be written more succinctly
+  --> tests/ui/neg_multiply.rs:66:13
+   |
+LL |     100.0 + x * -1.0;
+   |             ^^^^^^^^ help: consider using: `-x`
+
+error: this multiplication by -1 can be written more succinctly
+  --> tests/ui/neg_multiply.rs:69:5
+   |
+LL |     (100.0 + x) * -1.0;
+   |     ^^^^^^^^^^^^^^^^^^ help: consider using: `-(100.0 + x)`
+
+error: this multiplication by -1 can be written more succinctly
+  --> tests/ui/neg_multiply.rs:72:5
+   |
+LL |     -1.0 * 17.0;
+   |     ^^^^^^^^^^^ help: consider using: `-17.0`
+
+error: this multiplication by -1 can be written more succinctly
+  --> tests/ui/neg_multiply.rs:75:11
+   |
+LL |     0.0 + 0.0 * -1.0;
+   |           ^^^^^^^^^^ help: consider using: `-0.0`
+
+error: this multiplication by -1 can be written more succinctly
+  --> tests/ui/neg_multiply.rs:78:5
+   |
+LL |     3.0_f32 as f64 * -1.0;
+   |     ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3.0_f32 as f64)`
+
+error: this multiplication by -1 can be written more succinctly
+  --> tests/ui/neg_multiply.rs:80:5
+   |
+LL |     (3.0_f32 as f64) * -1.0;
+   |     ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `-(3.0_f32 as f64)`
+
+error: aborting due to 16 previous errors