2 use rustc_lint::LateContext;
3 use rustc_middle::ty::{self, FloatTy, Ty};
5 use crate::utils::{is_isize_or_usize, span_lint};
7 use super::{utils, CAST_POSSIBLE_TRUNCATION};
9 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
10 let msg = match (cast_from.is_integral(), cast_to.is_integral()) {
12 let from_nbits = utils::int_ty_to_nbits(cast_from, cx.tcx);
13 let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);
15 let (should_lint, suffix) = match (is_isize_or_usize(cast_from), is_isize_or_usize(cast_to)) {
16 (true, true) | (false, false) => (to_nbits < from_nbits, ""),
20 " on targets with 64-bit wide pointers"
25 (false, true) => (from_nbits == 64, " on targets with 32-bit wide pointers"),
33 "casting `{}` to `{}` may truncate the value{}",
34 cast_from, cast_to, suffix,
39 format!("casting `{}` to `{}` may truncate the value", cast_from, cast_to)
43 if matches!(cast_from.kind(), &ty::Float(FloatTy::F64))
44 && matches!(cast_to.kind(), &ty::Float(FloatTy::F32))
46 "casting `f64` to `f32` may truncate the value".to_string()
53 span_lint(cx, CAST_POSSIBLE_TRUNCATION, expr.span, &msg);