1 use super::TRANSMUTE_FLOAT_TO_INT;
2 use clippy_utils::diagnostics::span_lint_and_then;
3 use clippy_utils::sugg;
4 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir::{Expr, ExprKind, UnOp};
8 use rustc_lint::LateContext;
9 use rustc_middle::ty::{self, Ty};
11 /// Checks for `transmute_float_to_int` lint.
12 /// Returns `true` if it's triggered, otherwise returns `false`.
13 pub(super) fn check<'tcx>(
14 cx: &LateContext<'tcx>,
18 args: &'tcx [Expr<'_>],
21 match (&from_ty.kind(), &to_ty.kind()) {
22 (ty::Float(float_ty), ty::Int(_) | ty::Uint(_)) if !const_context => {
25 TRANSMUTE_FLOAT_TO_INT,
27 &format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
29 let mut expr = &args[0];
30 let mut arg = sugg::Sugg::hir(cx, expr, "..");
32 if let ExprKind::Unary(UnOp::Neg, inner_expr) = &expr.kind {
37 // if the expression is a float literal and it is unsuffixed then
38 // add a suffix so the suggestion is valid and unambiguous
39 if let ExprKind::Lit(lit) = &expr.kind;
40 if let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node;
42 let op = format!("{}{}", arg, float_ty.name_str()).into();
44 sugg::Sugg::MaybeParen(_) => arg = sugg::Sugg::MaybeParen(op),
45 _ => arg = sugg::Sugg::NonParen(op)
50 arg = sugg::Sugg::NonParen(format!("{}.to_bits()", arg.maybe_par()).into());
52 // cast the result of `to_bits` if `to_ty` is signed
53 arg = if let ty::Int(int_ty) = to_ty.kind() {
54 arg.as_ty(int_ty.name_str().to_string())
59 diag.span_suggestion(e.span, "consider using", arg.to_string(), Applicability::Unspecified);