- match (cast_from.is_integral(), cast_to.is_integral()) {
- (true, false) => {
- let from_nbits = int_ty_to_nbits(cast_from, cx.tcx);
- let to_nbits = if let ty::Float(FloatTy::F32) = cast_to.sty {
- 32
- } else {
- 64
- };
- if is_isize_or_usize(cast_from) || from_nbits >= to_nbits {
- span_precision_loss_lint(cx, expr, cast_from, to_nbits == 64);
- }
- if from_nbits < to_nbits {
- span_lossless_lint(cx, expr, ex, cast_from, cast_to);
- }
- },
- (false, true) => {
- span_lint(
- cx,
- CAST_POSSIBLE_TRUNCATION,
- expr.span,
- &format!("casting {} to {} may truncate the value", cast_from, cast_to),
- );
- if !cast_to.is_signed() {
- span_lint(
- cx,
- CAST_SIGN_LOSS,
- expr.span,
- &format!("casting {} to {} may lose the sign of the value", cast_from, cast_to),
- );
- }
- },
- (true, true) => {
- check_loss_of_sign(cx, expr, ex, cast_from, cast_to);
- check_truncation_and_wrapping(cx, expr, cast_from, cast_to);
- check_lossless(cx, expr, ex, cast_from, cast_to);
- },
- (false, false) => {
- if let (&ty::Float(FloatTy::F64), &ty::Float(FloatTy::F32)) = (&cast_from.sty, &cast_to.sty) {
- span_lint(
- cx,
- CAST_POSSIBLE_TRUNCATION,
- expr.span,
- "casting f64 to f32 may truncate the value",
- );
- }
- if let (&ty::Float(FloatTy::F32), &ty::Float(FloatTy::F64)) = (&cast_from.sty, &cast_to.sty) {
- span_lossless_lint(cx, expr, ex, cast_from, cast_to);
- }
- },
- }