Skip to content

Commit

Permalink
Move transmute_float_to_int test cases into separate file
Browse files Browse the repository at this point in the history
`transmute.stderr` file line count exceeded due to the new test
cases so moving the new test cases into a separate file.
  • Loading branch information
krishna-veerareddy committed Dec 8, 2019
1 parent c77fc06 commit 23c03e4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 57 deletions.
10 changes: 0 additions & 10 deletions tests/ui/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ fn int_to_float() {
let _: f32 = unsafe { std::mem::transmute(0_i32) };
}

#[warn(clippy::transmute_float_to_int)]
fn float_to_int() {
let _: u32 = unsafe { std::mem::transmute(1f32) };
let _: i32 = unsafe { std::mem::transmute(1f32) };
let _: u64 = unsafe { std::mem::transmute(1f64) };
let _: i64 = unsafe { std::mem::transmute(1f64) };
let _: u64 = unsafe { std::mem::transmute(1.0) };
let _: u64 = unsafe { std::mem::transmute(-1.0) };
}

fn bytes_to_str(b: &[u8], mb: &mut [u8]) {
let _: &str = unsafe { std::mem::transmute(b) };
let _: &mut str = unsafe { std::mem::transmute(mb) };
Expand Down
56 changes: 9 additions & 47 deletions tests/ui/transmute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -190,95 +190,57 @@ 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 `f32` to a `u32`
--> $DIR/transmute.rs:131:27
|
LL | let _: u32 = unsafe { std::mem::transmute(1f32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f32.to_bits()`
|
= note: `-D clippy::transmute-float-to-int` implied by `-D warnings`

error: transmute from a `f32` to a `i32`
--> $DIR/transmute.rs:132:27
|
LL | let _: i32 = unsafe { std::mem::transmute(1f32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f32.to_bits() as i32`

error: transmute from a `f64` to a `u64`
--> $DIR/transmute.rs:133:27
|
LL | let _: u64 = unsafe { std::mem::transmute(1f64) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f64.to_bits()`

error: transmute from a `f64` to a `i64`
--> $DIR/transmute.rs:134:27
|
LL | let _: i64 = unsafe { std::mem::transmute(1f64) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f64.to_bits() as i64`

error: transmute from a `f64` to a `u64`
--> $DIR/transmute.rs:135:27
|
LL | let _: u64 = unsafe { std::mem::transmute(1.0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1.0f64.to_bits()`

error: transmute from a `f64` to a `u64`
--> $DIR/transmute.rs:136:27
|
LL | let _: u64 = unsafe { std::mem::transmute(-1.0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-1.0f64).to_bits()`

error: transmute from a `&[u8]` to a `&str`
--> $DIR/transmute.rs:140:28
--> $DIR/transmute.rs:130: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:141:32
--> $DIR/transmute.rs:131:32
|
LL | let _: &mut str = unsafe { std::mem::transmute(mb) };
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`

error: transmute from a pointer to a pointer
--> $DIR/transmute.rs:173:29
--> $DIR/transmute.rs:163:29
|
LL | let _: *const f32 = std::mem::transmute(ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr as *const f32`
|
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`

error: transmute from a pointer to a pointer
--> $DIR/transmute.rs:174:27
--> $DIR/transmute.rs:164:27
|
LL | let _: *mut f32 = std::mem::transmute(mut_ptr);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `mut_ptr as *mut f32`

error: transmute from a reference to a reference
--> $DIR/transmute.rs:176:23
--> $DIR/transmute.rs:166:23
|
LL | let _: &f32 = std::mem::transmute(&1u32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1u32 as *const u32 as *const f32)`

error: transmute from a reference to a reference
--> $DIR/transmute.rs:177:23
--> $DIR/transmute.rs:167:23
|
LL | let _: &f64 = std::mem::transmute(&1f32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&1f32 as *const f32 as *const f64)`

error: transmute from a reference to a reference
--> $DIR/transmute.rs:180:27
--> $DIR/transmute.rs:170:27
|
LL | let _: &mut f32 = std::mem::transmute(&mut 1u32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&mut *(&mut 1u32 as *mut u32 as *mut f32)`

error: transmute from a reference to a reference
--> $DIR/transmute.rs:181:37
--> $DIR/transmute.rs:171:37
|
LL | let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)`

error: aborting due to 44 previous errors
error: aborting due to 38 previous errors

12 changes: 12 additions & 0 deletions tests/ui/transmute_float_to_int.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[warn(clippy::transmute_float_to_int)]

fn float_to_int() {
let _: u32 = unsafe { std::mem::transmute(1f32) };
let _: i32 = unsafe { std::mem::transmute(1f32) };
let _: u64 = unsafe { std::mem::transmute(1f64) };
let _: i64 = unsafe { std::mem::transmute(1f64) };
let _: u64 = unsafe { std::mem::transmute(1.0) };
let _: u64 = unsafe { std::mem::transmute(-1.0) };
}

fn main() {}
40 changes: 40 additions & 0 deletions tests/ui/transmute_float_to_int.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
error: transmute from a `f32` to a `u32`
--> $DIR/transmute_float_to_int.rs:4:27
|
LL | let _: u32 = unsafe { std::mem::transmute(1f32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f32.to_bits()`
|
= note: `-D clippy::transmute-float-to-int` implied by `-D warnings`

error: transmute from a `f32` to a `i32`
--> $DIR/transmute_float_to_int.rs:5:27
|
LL | let _: i32 = unsafe { std::mem::transmute(1f32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f32.to_bits() as i32`

error: transmute from a `f64` to a `u64`
--> $DIR/transmute_float_to_int.rs:6:27
|
LL | let _: u64 = unsafe { std::mem::transmute(1f64) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f64.to_bits()`

error: transmute from a `f64` to a `i64`
--> $DIR/transmute_float_to_int.rs:7:27
|
LL | let _: i64 = unsafe { std::mem::transmute(1f64) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1f64.to_bits() as i64`

error: transmute from a `f64` to a `u64`
--> $DIR/transmute_float_to_int.rs:8:27
|
LL | let _: u64 = unsafe { std::mem::transmute(1.0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1.0f64.to_bits()`

error: transmute from a `f64` to a `u64`
--> $DIR/transmute_float_to_int.rs:9:27
|
LL | let _: u64 = unsafe { std::mem::transmute(-1.0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-1.0f64).to_bits()`

error: aborting due to 6 previous errors

0 comments on commit 23c03e4

Please sign in to comment.