]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/transmute/transmute_float_to_int.rs
Merge commit 'fdb84cbfd25908df5683f8f62388f663d9260e39' into clippyup
[rust.git] / src / tools / clippy / clippy_lints / src / transmute / transmute_float_to_int.rs
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;
5 use rustc_ast as ast;
6 use rustc_errors::Applicability;
7 use rustc_hir::{Expr, ExprKind, UnOp};
8 use rustc_lint::LateContext;
9 use rustc_middle::ty::{self, Ty};
10
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>,
15     e: &'tcx Expr<'_>,
16     from_ty: Ty<'tcx>,
17     to_ty: Ty<'tcx>,
18     mut arg: &'tcx Expr<'_>,
19     const_context: bool,
20 ) -> bool {
21     match (&from_ty.kind(), &to_ty.kind()) {
22         (ty::Float(float_ty), ty::Int(_) | ty::Uint(_)) if !const_context => {
23             span_lint_and_then(
24                 cx,
25                 TRANSMUTE_FLOAT_TO_INT,
26                 e.span,
27                 &format!("transmute from a `{}` to a `{}`", from_ty, to_ty),
28                 |diag| {
29                     let mut sugg = sugg::Sugg::hir(cx, arg, "..");
30
31                     if let ExprKind::Unary(UnOp::Neg, inner_expr) = &arg.kind {
32                         arg = 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                         if let ExprKind::Lit(lit) = &arg.kind;
39                         if let ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) = lit.node;
40                         then {
41                             let op = format!("{}{}", sugg, float_ty.name_str()).into();
42                             match sugg {
43                                 sugg::Sugg::MaybeParen(_) => sugg = sugg::Sugg::MaybeParen(op),
44                                 _ => sugg = sugg::Sugg::NonParen(op)
45                             }
46                         }
47                     }
48
49                     sugg = sugg::Sugg::NonParen(format!("{}.to_bits()", sugg.maybe_par()).into());
50
51                     // cast the result of `to_bits` if `to_ty` is signed
52                     sugg = if let ty::Int(int_ty) = to_ty.kind() {
53                         sugg.as_ty(int_ty.name_str().to_string())
54                     } else {
55                         sugg
56                     };
57
58                     diag.span_suggestion(e.span, "consider using", sugg, Applicability::Unspecified);
59                 },
60             );
61             true
62         },
63         _ => false,
64     }
65 }