1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::ty::is_isize_or_usize;
4 use rustc_lint::LateContext;
5 use rustc_middle::ty::{self, FloatTy, Ty};
7 use super::{utils, CAST_PRECISION_LOSS};
9 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
10 if !cast_from.is_integral() || cast_to.is_integral() {
14 let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
15 let to_nbits = if cast_to.kind() == &ty::Float(FloatTy::F32) {
21 if !(is_isize_or_usize(cast_from) || from_nbits >= to_nbits) {
25 let cast_to_f64 = to_nbits == 64;
26 let mantissa_nbits = if cast_to_f64 { 52 } else { 23 };
27 let arch_dependent = is_isize_or_usize(cast_from) && cast_to_f64;
28 let arch_dependent_str = "on targets with 64-bit wide pointers ";
29 let from_nbits_str = if arch_dependent {
31 } else if is_isize_or_usize(cast_from) {
34 utils::int_ty_to_nbits(cast_from, cx.tcx).to_string()
42 "casting `{0}` to `{1}` causes a loss of precision {2}(`{0}` is {3} bits wide, \
43 but `{1}`'s mantissa is only {4} bits wide)",
45 if cast_to_f64 { "f64" } else { "f32" },
46 if arch_dependent { arch_dependent_str } else { "" },