no from/to bits in const: add tests cases for f64

This commit is contained in:
Eduardo Broto
2020-08-18 22:19:30 +02:00
parent df4d42fc2d
commit 6a12bae194
4 changed files with 36 additions and 10 deletions

View File

@@ -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 let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
if match_def_path(cx, def_id, &paths::TRANSMUTE); if match_def_path(cx, def_id, &paths::TRANSMUTE);
then { 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`. // 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); let const_context = in_constant(cx, e.hir_id);

View File

@@ -86,13 +86,22 @@ mod int_to_float {
fn test() { fn test() {
let _: f32 = unsafe { std::mem::transmute(0_u32) }; let _: f32 = unsafe { std::mem::transmute(0_u32) };
let _: f32 = unsafe { std::mem::transmute(0_i32) }; 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 mod issue_5747 {
const VALUE: f32 = unsafe { std::mem::transmute(0_u32) }; const VALUE32: f32 = unsafe { std::mem::transmute(0_u32) };
const fn from_bits(v: u32) -> f32 { const VALUE64: f64 = unsafe { std::mem::transmute(0_i64) };
const fn from_bits_32(v: i32) -> f32 {
unsafe { std::mem::transmute(v) } unsafe { std::mem::transmute(v) }
} }
const fn from_bits_64(v: u64) -> f64 {
unsafe { std::mem::transmute(v) }
}
}
} }
fn bytes_to_str(b: &[u8], mb: &mut [u8]) { fn bytes_to_str(b: &[u8], mb: &mut [u8]) {

View File

@@ -128,8 +128,20 @@ error: transmute from a `i32` to a `f32`
LL | let _: f32 = unsafe { std::mem::transmute(0_i32) }; LL | let _: f32 = unsafe { std::mem::transmute(0_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_i32 as u32)` | ^^^^^^^^^^^^^^^^^^^^^^^^^^ 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` 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) }; LL | let _: &str = unsafe { std::mem::transmute(b) };
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(b).unwrap()` | ^^^^^^^^^^^^^^^^^^^^^^ 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` = note: `-D clippy::transmute-bytes-to-str` implied by `-D warnings`
error: transmute from a `&mut [u8]` to a `&mut str` 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) }; LL | let _: &mut str = unsafe { std::mem::transmute(mb) };
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()` | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`
error: aborting due to 22 previous errors error: aborting due to 24 previous errors

View File

@@ -11,9 +11,14 @@ fn float_to_int() {
} }
mod issue_5747 { 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) } unsafe { std::mem::transmute(v) }
} }
} }