Skip to content

Commit 6a12bae

Browse files
committed
no from/to bits in const: add tests cases for f64
1 parent df4d42f commit 6a12bae

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

clippy_lints/src/transmute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
331331
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
332332
if match_def_path(cx, def_id, &paths::TRANSMUTE);
333333
then {
334-
// Avoid suggesting f32::(from|to)_bits in const contexts.
334+
// Avoid suggesting from/to bits in const contexts.
335335
// See https://github.com/rust-lang/rust/issues/73736 for progress on making them `const fn`.
336336
let const_context = in_constant(cx, e.hir_id);
337337

tests/ui/transmute.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,21 @@ mod int_to_float {
8686
fn test() {
8787
let _: f32 = unsafe { std::mem::transmute(0_u32) };
8888
let _: f32 = unsafe { std::mem::transmute(0_i32) };
89+
let _: f64 = unsafe { std::mem::transmute(0_u64) };
90+
let _: f64 = unsafe { std::mem::transmute(0_i64) };
8991
}
9092

91-
// See issue #5747
92-
const VALUE: f32 = unsafe { std::mem::transmute(0_u32) };
93-
const fn from_bits(v: u32) -> f32 {
94-
unsafe { std::mem::transmute(v) }
93+
mod issue_5747 {
94+
const VALUE32: f32 = unsafe { std::mem::transmute(0_u32) };
95+
const VALUE64: f64 = unsafe { std::mem::transmute(0_i64) };
96+
97+
const fn from_bits_32(v: i32) -> f32 {
98+
unsafe { std::mem::transmute(v) }
99+
}
100+
101+
const fn from_bits_64(v: u64) -> f64 {
102+
unsafe { std::mem::transmute(v) }
103+
}
95104
}
96105
}
97106

tests/ui/transmute.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,31 @@ error: transmute from a `i32` to a `f32`
128128
LL | let _: f32 = unsafe { std::mem::transmute(0_i32) };
129129
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f32::from_bits(0_i32 as u32)`
130130

131+
error: transmute from a `u64` to a `f64`
132+
--> $DIR/transmute.rs:89:31
133+
|
134+
LL | let _: f64 = unsafe { std::mem::transmute(0_u64) };
135+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f64::from_bits(0_u64)`
136+
137+
error: transmute from a `i64` to a `f64`
138+
--> $DIR/transmute.rs:90:31
139+
|
140+
LL | let _: f64 = unsafe { std::mem::transmute(0_i64) };
141+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `f64::from_bits(0_i64 as u64)`
142+
131143
error: transmute from a `&[u8]` to a `&str`
132-
--> $DIR/transmute.rs:99:28
144+
--> $DIR/transmute.rs:108:28
133145
|
134146
LL | let _: &str = unsafe { std::mem::transmute(b) };
135147
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(b).unwrap()`
136148
|
137149
= note: `-D clippy::transmute-bytes-to-str` implied by `-D warnings`
138150

139151
error: transmute from a `&mut [u8]` to a `&mut str`
140-
--> $DIR/transmute.rs:100:32
152+
--> $DIR/transmute.rs:109:32
141153
|
142154
LL | let _: &mut str = unsafe { std::mem::transmute(mb) };
143155
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`
144156

145-
error: aborting due to 22 previous errors
157+
error: aborting due to 24 previous errors
146158

tests/ui/transmute_float_to_int.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ fn float_to_int() {
1111
}
1212

1313
mod issue_5747 {
14-
const VALUE: u32 = unsafe { std::mem::transmute(1f32) };
14+
const VALUE32: i32 = unsafe { std::mem::transmute(1f32) };
15+
const VALUE64: u64 = unsafe { std::mem::transmute(1f64) };
1516

16-
const fn to_bits(v: f32) -> u32 {
17+
const fn to_bits_32(v: f32) -> u32 {
18+
unsafe { std::mem::transmute(v) }
19+
}
20+
21+
const fn to_bits_64(v: f64) -> i64 {
1722
unsafe { std::mem::transmute(v) }
1823
}
1924
}

0 commit comments

Comments
 (0)