about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGeoffrey Copin <copin.geoffrey@gmail.com>2020-10-23 23:40:57 +0200
committerGeoffrey Copin <copin.geoffrey@gmail.com>2020-10-24 00:04:37 +0200
commitd46edd99667ad342e6118b2216a0c24ee009e86c (patch)
tree00be29e3591d66ce3f9ae8486f62b96ba312b72c
parent30f80c3b8c4fcb5d0db37b84a77a58303322cf4e (diff)
downloadrust-d46edd99667ad342e6118b2216a0c24ee009e86c.tar.gz
rust-d46edd99667ad342e6118b2216a0c24ee009e86c.zip
Keep sign in int-to-float casts
-rw-r--r--clippy_lints/src/types.rs16
-rw-r--r--tests/ui/unnecessary_cast_fixable.fixed3
-rw-r--r--tests/ui/unnecessary_cast_fixable.rs3
-rw-r--r--tests/ui/unnecessary_cast_fixable.stderr38
4 files changed, 48 insertions, 12 deletions
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs
index 3a088709a7e..6a33aaaaab2 100644
--- a/clippy_lints/src/types.rs
+++ b/clippy_lints/src/types.rs
@@ -1236,6 +1236,13 @@ declare_clippy_lint! {
     /// let _ = 2i32 as i32;
     /// let _ = 0.5 as f32;
     /// ```
+    ///
+    /// Better:
+    ///
+    /// ```rust
+    /// let _ = 2_i32;
+    /// let _ = 0.5_f32;
+    /// ```
     pub UNNECESSARY_CAST,
     complexity,
     "cast to the same type, e.g., `x as i32` where `x: i32`"
@@ -1612,7 +1619,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
                     let to_nbits = fp_ty_mantissa_nbits(cast_to);
                     if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal();
                     then {
-                        show_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
+                        let literal_str = if is_unary_neg(ex) { format!("-{}", num_lit.integer) } else { num_lit.integer.into() };
+                        show_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
                         return;
                     }
                 }
@@ -1624,7 +1632,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
                     LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
                         show_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
                     },
-                    LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => (),
+                    LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
                     _ => {
                         if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
                             span_lint(
@@ -1649,6 +1657,10 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
     }
 }
 
+fn is_unary_neg(expr: &Expr<'_>) -> bool {
+    matches!(expr.kind, ExprKind::Unary(UnOp::UnNeg, _))
+}
+
 fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<&'e Lit> {
     match expr.kind {
         ExprKind::Lit(ref lit) => Some(lit),
diff --git a/tests/ui/unnecessary_cast_fixable.fixed b/tests/ui/unnecessary_cast_fixable.fixed
index 5aeb0340b26..350da4965d1 100644
--- a/tests/ui/unnecessary_cast_fixable.fixed
+++ b/tests/ui/unnecessary_cast_fixable.fixed
@@ -8,6 +8,9 @@ fn main() {
     100_f32;
     100_f64;
     100_f64;
+    let _ = -100_f32;
+    let _ = -100_f64;
+    let _ = -100_f64;
     // Should not trigger
     #[rustfmt::skip]
     let v = vec!(1);
diff --git a/tests/ui/unnecessary_cast_fixable.rs b/tests/ui/unnecessary_cast_fixable.rs
index 0f249c23055..ad2fb2e6289 100644
--- a/tests/ui/unnecessary_cast_fixable.rs
+++ b/tests/ui/unnecessary_cast_fixable.rs
@@ -8,6 +8,9 @@ fn main() {
     100 as f32;
     100 as f64;
     100_i32 as f64;
+    let _ = -100 as f32;
+    let _ = -100 as f64;
+    let _ = -100_i32 as f64;
     // Should not trigger
     #[rustfmt::skip]
     let v = vec!(1);
diff --git a/tests/ui/unnecessary_cast_fixable.stderr b/tests/ui/unnecessary_cast_fixable.stderr
index 5100e9798c4..5a210fc8909 100644
--- a/tests/ui/unnecessary_cast_fixable.stderr
+++ b/tests/ui/unnecessary_cast_fixable.stderr
@@ -18,59 +18,77 @@ error: casting integer literal to `f64` is unnecessary
 LL |     100_i32 as f64;
    |     ^^^^^^^^^^^^^^ help: try: `100_f64`
 
+error: casting integer literal to `f32` is unnecessary
+  --> $DIR/unnecessary_cast_fixable.rs:11:13
+   |
+LL |     let _ = -100 as f32;
+   |             ^^^^^^^^^^^ help: try: `-100_f32`
+
+error: casting integer literal to `f64` is unnecessary
+  --> $DIR/unnecessary_cast_fixable.rs:12:13
+   |
+LL |     let _ = -100 as f64;
+   |             ^^^^^^^^^^^ help: try: `-100_f64`
+
+error: casting integer literal to `f64` is unnecessary
+  --> $DIR/unnecessary_cast_fixable.rs:13:13
+   |
+LL |     let _ = -100_i32 as f64;
+   |             ^^^^^^^^^^^^^^^ help: try: `-100_f64`
+
 error: casting integer literal to `u32` is unnecessary
-  --> $DIR/unnecessary_cast_fixable.rs:22:5
+  --> $DIR/unnecessary_cast_fixable.rs:25:5
    |
 LL |     1 as u32;
    |     ^^^^^^^^ help: try: `1_u32`
 
 error: casting integer literal to `i32` is unnecessary
-  --> $DIR/unnecessary_cast_fixable.rs:23:5
+  --> $DIR/unnecessary_cast_fixable.rs:26:5
    |
 LL |     0x10 as i32;
    |     ^^^^^^^^^^^ help: try: `0x10_i32`
 
 error: casting integer literal to `usize` is unnecessary
-  --> $DIR/unnecessary_cast_fixable.rs:24:5
+  --> $DIR/unnecessary_cast_fixable.rs:27:5
    |
 LL |     0b10 as usize;
    |     ^^^^^^^^^^^^^ help: try: `0b10_usize`
 
 error: casting integer literal to `u16` is unnecessary
-  --> $DIR/unnecessary_cast_fixable.rs:25:5
+  --> $DIR/unnecessary_cast_fixable.rs:28:5
    |
 LL |     0o73 as u16;
    |     ^^^^^^^^^^^ help: try: `0o73_u16`
 
 error: casting integer literal to `u32` is unnecessary
-  --> $DIR/unnecessary_cast_fixable.rs:26:5
+  --> $DIR/unnecessary_cast_fixable.rs:29:5
    |
 LL |     1_000_000_000 as u32;
    |     ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
 
 error: casting float literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast_fixable.rs:28:5
+  --> $DIR/unnecessary_cast_fixable.rs:31:5
    |
 LL |     1.0 as f64;
    |     ^^^^^^^^^^ help: try: `1.0_f64`
 
 error: casting float literal to `f32` is unnecessary
-  --> $DIR/unnecessary_cast_fixable.rs:29:5
+  --> $DIR/unnecessary_cast_fixable.rs:32:5
    |
 LL |     0.5 as f32;
    |     ^^^^^^^^^^ help: try: `0.5_f32`
 
 error: casting integer literal to `i32` is unnecessary
-  --> $DIR/unnecessary_cast_fixable.rs:33:13
+  --> $DIR/unnecessary_cast_fixable.rs:36:13
    |
 LL |     let _ = -1 as i32;
    |             ^^^^^^^^^ help: try: `-1_i32`
 
 error: casting float literal to `f32` is unnecessary
-  --> $DIR/unnecessary_cast_fixable.rs:34:13
+  --> $DIR/unnecessary_cast_fixable.rs:37:13
    |
 LL |     let _ = -1.0 as f32;
    |             ^^^^^^^^^^^ help: try: `-1.0_f32`
 
-error: aborting due to 12 previous errors
+error: aborting due to 15 previous errors