Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lint to detect transmutes from float to integer #4889

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ Released 2018-09-13
[`too_many_lines`]: https://rust-lang.github.io/rust-clippy/master/index.html#too_many_lines
[`toplevel_ref_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#toplevel_ref_arg
[`transmute_bytes_to_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_bytes_to_str
[`transmute_float_to_int`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_float_to_int
[`transmute_int_to_bool`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_bool
[`transmute_int_to_char`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_char
[`transmute_int_to_float`]: https://rust-lang.github.io/rust-clippy/master/index.html#transmute_int_to_float
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.

[There are 339 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are 340 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)

We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:

Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
&trait_bounds::TYPE_REPETITION_IN_BOUNDS,
&transmute::CROSSPOINTER_TRANSMUTE,
&transmute::TRANSMUTE_BYTES_TO_STR,
&transmute::TRANSMUTE_FLOAT_TO_INT,
&transmute::TRANSMUTE_INT_TO_BOOL,
&transmute::TRANSMUTE_INT_TO_CHAR,
&transmute::TRANSMUTE_INT_TO_FLOAT,
Expand Down Expand Up @@ -1586,6 +1587,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
LintId::of(&mutex_atomic::MUTEX_INTEGER),
LintId::of(&needless_borrow::NEEDLESS_BORROW),
LintId::of(&path_buf_push_overwrite::PATH_BUF_PUSH_OVERWRITE),
LintId::of(&transmute::TRANSMUTE_FLOAT_TO_INT),
LintId::of(&use_self::USE_SELF),
]);
}
Expand Down
67 changes: 67 additions & 0 deletions clippy_lints/src/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,28 @@ declare_clippy_lint! {
"transmutes from an integer to a float"
}

declare_clippy_lint! {
/// **What it does:** Checks for transmutes from a float to an integer.
///
/// **Why is this bad?** Transmutes are dangerous and error-prone, whereas `to_bits` is intuitive
/// and safe.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// unsafe {
/// let _: u32 = std::mem::transmute(1f32);
/// }
///
/// // should be:
/// let _: u32 = 1f32.to_bits();
/// ```
pub TRANSMUTE_FLOAT_TO_INT,
nursery,
"transmutes from a float to an integer"
}

declare_clippy_lint! {
/// **What it does:** Checks for transmutes from a pointer to a pointer, or
/// from a reference to a reference.
Expand Down Expand Up @@ -254,6 +276,7 @@ declare_lint_pass!(Transmute => [
TRANSMUTE_BYTES_TO_STR,
TRANSMUTE_INT_TO_BOOL,
TRANSMUTE_INT_TO_FLOAT,
TRANSMUTE_FLOAT_TO_INT,
UNSOUND_COLLECTION_TRANSMUTE,
]);

Expand Down Expand Up @@ -520,6 +543,50 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
);
},
),
(&ty::Float(float_ty), &ty::Int(_)) | (&ty::Float(float_ty), &ty::Uint(_)) => span_lint_and_then(
cx,
TRANSMUTE_FLOAT_TO_INT,
e.span,
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|db| {
let mut expr = &args[0];
let mut arg = sugg::Sugg::hir(cx, expr, "..");

if let ExprKind::Unary(UnOp::UnNeg, inner_expr) = &expr.kind {
expr = &inner_expr;
}

if_chain! {
// if the expression is a float literal and it is unsuffixed then
// add a suffix so the suggestion is valid and unambiguous
let op = format!("{}{}", arg, float_ty.name_str()).into();
if let ExprKind::Lit(lit) = &expr.kind;
if let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node;
then {
match arg {
sugg::Sugg::MaybeParen(_) => arg = sugg::Sugg::MaybeParen(op),
_ => arg = sugg::Sugg::NonParen(op)
}
}
}

arg = sugg::Sugg::NonParen(format!("{}.to_bits()", arg.maybe_par()).into());

// cast the result of `to_bits` if `to_ty` is signed
arg = if let ty::Int(int_ty) = to_ty.kind {
arg.as_ty(int_ty.name_str().to_string())
} else {
arg
};

db.span_suggestion(
e.span,
"consider using",
arg.to_string(),
Applicability::Unspecified,
);
},
),
(&ty::Adt(ref from_adt, ref from_substs), &ty::Adt(ref to_adt, ref to_substs)) => {
if from_adt.did != to_adt.did ||
!COLLECTIONS.iter().any(|path| match_def_path(cx, to_adt.did, path)) {
Expand Down
9 changes: 8 additions & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use lint::Lint;
pub use lint::LINT_LEVELS;

// begin lint list, do not remove this comment, it’s used in `update_lints`
pub const ALL_LINTS: [Lint; 339] = [
pub const ALL_LINTS: [Lint; 340] = [
Lint {
name: "absurd_extreme_comparisons",
group: "correctness",
Expand Down Expand Up @@ -1953,6 +1953,13 @@ pub const ALL_LINTS: [Lint; 339] = [
deprecation: None,
module: "transmute",
},
Lint {
name: "transmute_float_to_int",
group: "nursery",
desc: "transmutes from a float to an integer",
deprecation: None,
module: "transmute",
},
Lint {
name: "transmute_int_to_bool",
group: "complexity",
Expand Down
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