-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #4889 - krishna-veerareddy:issue-3993-float-to-int-tran…
…smute, r=llogiq Add lint to detect transmutes from float to integer Add lint that detects transmutation from a float to an integer and suggests usage of `{f32, f64}.to_bits()` instead. Fixes #3993 changelog: Add lint `transmute_float_to_int`
- Loading branch information
Showing
7 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|