]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/transmute/transmute_float_to_int.rs
Rollup merge of #83092 - petrochenkov:qspan, r=estebank
[rust.git] / clippy_lints / src / transmute / transmute_float_to_int.rs
1 use super::TRANSMUTE_FLOAT_TO_INT;
2 use crate::utils::{span_lint_and_then, sugg};
3 use if_chain::if_chain;
4 use rustc_ast as ast;
5 use rustc_errors::Applicability;
6 use rustc_hir::{Expr, ExprKind, UnOp};
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::{self, Ty};
9
10 /// Checks for `transmute_float_to_int` lint.
11 /// Returns `true` if it's triggered, otherwise returns `false`.
12 pub(super) fn check<'tcx>(
13     cx: &LateContext<'tcx>,
14     e: &'tcx Expr<'_>,
15     from_ty: Ty<'tcx>,
16     to_ty: Ty<'tcx>,
17     args: &'tcx [Expr<'_>],
18     const_context: bool,
19 ) -> bool {
20     match (&from_ty.kind(), &to_ty.kind()) {
21         (ty::Float(float_ty), ty::Int(_) | ty::Uint(_)) if !const_context => {
22             span_lint_and_then(
23                 cx,
24                 TRANSMUTE_FLOAT_TO_INT,
25                 e.span,
26                 &format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
27                 |diag| {
28                     let mut expr = &args[0];
29                     let mut arg = sugg::Sugg::hir(cx, expr, "..");
30
31                     if let ExprKind::Unary(UnOp::Neg, inner_expr) = &expr.kind {
32                         expr = &inner_expr;
33                     }
34
35                     if_chain! {
36                         // if the expression is a float literal and it is unsuffixed then
37                         // add a suffix so the suggestion is valid and unambiguous
38                         let op = format!("{}{}", arg, float_ty.name_str()).into();
39                         if let ExprKind::Lit(lit) = &expr.kind;
40                         if let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node;
41                         then {
42                             match arg {
43                                 sugg::Sugg::MaybeParen(_) => arg = sugg::Sugg::MaybeParen(op),
44                                 _ => arg = sugg::Sugg::NonParen(op)
45                             }
46                         }
47                     }
48
49                     arg = sugg::Sugg::NonParen(format!("{}.to_bits()", arg.maybe_par()).into());
50
51                     // cast the result of `to_bits` if `to_ty` is signed
52                     arg = if let ty::Int(int_ty) = to_ty.kind() {
53                         arg.as_ty(int_ty.name_str().to_string())
54                     } else {
55                         arg
56                     };
57
58                     diag.span_suggestion(e.span, "consider using", arg.to_string(), Applicability::Unspecified);
59                 },
60             );
61             true
62         },
63         _ => false,
64     }
65 }