Skip to content

Commit

Permalink
Move transmute_float_to_int to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
magurotuna authored and flip1995 committed Mar 2, 2021
1 parent acedc7b commit e7d2393
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 44 deletions.
49 changes: 5 additions & 44 deletions clippy_lints/src/transmute/mod.rs
@@ -1,4 +1,5 @@
mod crosspointer_transmute;
mod transmute_float_to_int;
mod transmute_int_to_bool;
mod transmute_int_to_char;
mod transmute_int_to_float;
Expand Down Expand Up @@ -390,52 +391,12 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
if triggered {
return;
}
let triggered = transmute_float_to_int::check(cx, e, from_ty, to_ty, args, const_context);
if triggered {
return;
}

match (&from_ty.kind(), &to_ty.kind()) {
(ty::Float(float_ty), ty::Int(_) | ty::Uint(_)) if !const_context => span_lint_and_then(
cx,
TRANSMUTE_FLOAT_TO_INT,
e.span,
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|diag| {
let mut expr = &args[0];
let mut arg = sugg::Sugg::hir(cx, expr, "..");

if let ExprKind::Unary(UnOp::Neg, 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
};

diag.span_suggestion(
e.span,
"consider using",
arg.to_string(),
Applicability::Unspecified,
);
},
),
(ty::Adt(from_adt, from_substs), ty::Adt(to_adt, to_substs)) => {
if from_adt.did != to_adt.did ||
!COLLECTIONS.iter().any(|path| match_def_path(cx, to_adt.did, path)) {
Expand Down
66 changes: 66 additions & 0 deletions clippy_lints/src/transmute/transmute_float_to_int.rs
@@ -0,0 +1,66 @@
use super::TRANSMUTE_FLOAT_TO_INT;
use crate::utils::{span_lint_and_then, sugg};
use if_chain::if_chain;
use rustc_ast as ast;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, UnOp};
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_middle::ty::Ty;

/// Checks for `transmute_float_to_int` lint.
/// Returns `true` if it's triggered, otherwise returns `false`.
pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
e: &'tcx Expr<'_>,
from_ty: Ty<'tcx>,
to_ty: Ty<'tcx>,
args: &'tcx [Expr<'_>],
const_context: bool,
) -> bool {
match (&from_ty.kind(), &to_ty.kind()) {
(ty::Float(float_ty), ty::Int(_) | ty::Uint(_)) if !const_context => {
span_lint_and_then(
cx,
TRANSMUTE_FLOAT_TO_INT,
e.span,
&format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
|diag| {
let mut expr = &args[0];
let mut arg = sugg::Sugg::hir(cx, expr, "..");

if let ExprKind::Unary(UnOp::Neg, 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
};

diag.span_suggestion(e.span, "consider using", arg.to_string(), Applicability::Unspecified);
},
);
true
},
_ => false,
}
}

0 comments on commit e7d2393

Please sign in to comment.