Skip to content

Commit

Permalink
no from/to bits in const: add tests cases for f64
Browse files Browse the repository at this point in the history
  • Loading branch information
ebroto committed Aug 18, 2020
1 parent df4d42f commit 6a12bae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
17 changes: 13 additions & 4 deletions tests/ui/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
}

Expand Down
18 changes: 15 additions & 3 deletions tests/ui/transmute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,31 @@ 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()`
|
= 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

9 changes: 7 additions & 2 deletions tests/ui/transmute_float_to_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
}
Expand Down

0 comments on commit 6a12bae

Please sign in to comment.