From 6a12bae1941bdef7c2716ffefed5594e5d7e9f8e Mon Sep 17 00:00:00 2001
From: Eduardo Broto <ebroto@tutanota.com>
Date: Tue, 18 Aug 2020 22:19:30 +0200
Subject: [PATCH] no from/to bits in const: add tests cases for f64

---
 clippy_lints/src/transmute.rs      |  2 +-
 tests/ui/transmute.rs              | 17 +++++++++++++----
 tests/ui/transmute.stderr          | 18 +++++++++++++++---
 tests/ui/transmute_float_to_int.rs |  9 +++++++--
 4 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs
index 1d238b242b0..50d9c93f9d4 100644
--- a/clippy_lints/src/transmute.rs
+++ b/clippy_lints/src/transmute.rs
@@ -331,7 +331,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
             if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
             if match_def_path(cx, def_id, &paths::TRANSMUTE);
             then {
-                // Avoid suggesting f32::(from|to)_bits in const contexts.
+                // Avoid suggesting from/to bits in const contexts.
                 // See https://github.com/rust-lang/rust/issues/73736 for progress on making them `const fn`.
                 let const_context = in_constant(cx, e.hir_id);
 
diff --git a/tests/ui/transmute.rs b/tests/ui/transmute.rs
index 7caad34edb3..9f1948359e7 100644
--- a/tests/ui/transmute.rs
+++ b/tests/ui/transmute.rs
@@ -86,12 +86,21 @@ mod int_to_float {
     fn test() {
         let _: f32 = unsafe { std::mem::transmute(0_u32) };
         let _: f32 = unsafe { std::mem::transmute(0_i32) };
+        let _: f64 = unsafe { std::mem::transmute(0_u64) };
+        let _: f64 = unsafe { std::mem::transmute(0_i64) };
     }
 
-    // See issue #5747
-    const VALUE: f32 = unsafe { std::mem::transmute(0_u32) };
-    const fn from_bits(v: u32) -> f32 {
-        unsafe { std::mem::transmute(v) }
+    mod issue_5747 {
+        const VALUE32: f32 = unsafe { std::mem::transmute(0_u32) };
+        const VALUE64: f64 = unsafe { std::mem::transmute(0_i64) };
+
+        const fn from_bits_32(v: i32) -> f32 {
+            unsafe { std::mem::transmute(v) }
+        }
+
+        const fn from_bits_64(v: u64) -> f64 {
+            unsafe { std::mem::transmute(v) }
+        }
     }
 }
 
diff --git a/tests/ui/transmute.stderr b/tests/ui/transmute.stderr
index d817c08b52f..ad9953d12bc 100644
--- a/tests/ui/transmute.stderr
+++ b/tests/ui/transmute.stderr
@@ -128,8 +128,20 @@ error: transmute from a `i32` to a `f32`
 LL |         let _: f32 = unsafe { std::mem::transmute(0_i32) };
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_i32 as u32)`
 
+error: transmute from a `u64` to a `f64`
+  --> $DIR/transmute.rs:89:31
+   |
+LL |         let _: f64 = unsafe { std::mem::transmute(0_u64) };
+   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f64::from_bits(0_u64)`
+
+error: transmute from a `i64` to a `f64`
+  --> $DIR/transmute.rs:90:31
+   |
+LL |         let _: f64 = unsafe { std::mem::transmute(0_i64) };
+   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f64::from_bits(0_i64 as u64)`
+
 error: transmute from a `&[u8]` to a `&str`
-  --> $DIR/transmute.rs:99:28
+  --> $DIR/transmute.rs:108:28
    |
 LL |     let _: &str = unsafe { std::mem::transmute(b) };
    |                            ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(b).unwrap()`
@@ -137,10 +149,10 @@ LL |     let _: &str = unsafe { std::mem::transmute(b) };
    = note: `-D clippy::transmute-bytes-to-str` implied by `-D warnings`
 
 error: transmute from a `&mut [u8]` to a `&mut str`
-  --> $DIR/transmute.rs:100:32
+  --> $DIR/transmute.rs:109:32
    |
 LL |     let _: &mut str = unsafe { std::mem::transmute(mb) };
    |                                ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`
 
-error: aborting due to 22 previous errors
+error: aborting due to 24 previous errors
 
diff --git a/tests/ui/transmute_float_to_int.rs b/tests/ui/transmute_float_to_int.rs
index 8173944d959..1040fee4b34 100644
--- a/tests/ui/transmute_float_to_int.rs
+++ b/tests/ui/transmute_float_to_int.rs
@@ -11,9 +11,14 @@ fn float_to_int() {
 }
 
 mod issue_5747 {
-    const VALUE: u32 = unsafe { std::mem::transmute(1f32) };
+    const VALUE32: i32 = unsafe { std::mem::transmute(1f32) };
+    const VALUE64: u64 = unsafe { std::mem::transmute(1f64) };
 
-    const fn to_bits(v: f32) -> u32 {
+    const fn to_bits_32(v: f32) -> u32 {
+        unsafe { std::mem::transmute(v) }
+    }
+
+    const fn to_bits_64(v: f64) -> i64 {
         unsafe { std::mem::transmute(v) }
     }
 }