|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::ty::is_isize_or_usize; |
| 3 | +use rustc_hir::Expr; |
| 4 | +use rustc_lint::LateContext; |
| 5 | +use rustc_middle::ty::{self, Ty}; |
| 6 | + |
| 7 | +use super::CAST_PTR_SIZED_INT; |
| 8 | + |
| 9 | +/// Checks for casts between pointer-sized integer types (`usize`/`isize`) and |
| 10 | +/// fixed-size integer types where the behavior depends on the target architecture. |
| 11 | +/// |
| 12 | +/// Some casts are always safe and are NOT linted: |
| 13 | +/// - `u8`/`u16` → `usize`: always fits (usize is at least 16-bit) |
| 14 | +/// - `i8`/`i16` → `isize`: always fits (isize is at least 16-bit) |
| 15 | +/// - `usize` → `u64`/`u128`: always fits (usize is at most 64-bit) |
| 16 | +/// - `isize` → `i64`/`i128`: always fits (isize is at most 64-bit) |
| 17 | +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, cast_from: Ty<'tcx>, cast_to: Ty<'tcx>) { |
| 18 | + // Only consider integer-to-integer casts. |
| 19 | + if !cast_from.is_integral() || !cast_to.is_integral() { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + let from_is_ptr_sized = is_isize_or_usize(cast_from); |
| 24 | + let to_is_ptr_sized = is_isize_or_usize(cast_to); |
| 25 | + |
| 26 | + // We only care about casts where exactly one side is pointer-sized. |
| 27 | + if from_is_ptr_sized == to_is_ptr_sized { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // Identify which side is the pointer-sized type and which is the fixed-size type. |
| 32 | + let (ptr_sized_ty, fixed_ty, fixed_bits_opt, direction) = if from_is_ptr_sized { |
| 33 | + (cast_from, cast_to, fixed_type_bits(cast_to), "to") |
| 34 | + } else { |
| 35 | + (cast_to, cast_from, fixed_type_bits(cast_from), "from") |
| 36 | + }; |
| 37 | + |
| 38 | + let Some(fixed_bits) = fixed_bits_opt else { |
| 39 | + // If the non-pointer side is not a fixed-size integer, bail out. |
| 40 | + return; |
| 41 | + }; |
| 42 | + |
| 43 | + // If this cast is always safe regardless of target pointer width, don't lint. |
| 44 | + if is_always_safe_cast( |
| 45 | + from_is_ptr_sized, |
| 46 | + fixed_bits, |
| 47 | + cast_from.is_signed(), |
| 48 | + cast_to.is_signed(), |
| 49 | + ) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + let msg = format!("casting `{cast_from}` to `{cast_to}`: will always truncate"); |
| 54 | + |
| 55 | + span_lint_and_then(cx, CAST_PTR_SIZED_INT, expr.span, msg, |diag| { |
| 56 | + let help_msg = format!( |
| 57 | + "`{ptr_sized_ty}` varies in size depending on the target, \ |
| 58 | + so casting {direction} `{fixed_ty}` may produce different results across platforms" |
| 59 | + ); |
| 60 | + diag.help(help_msg); |
| 61 | + diag.help("consider using `TryFrom` or `TryInto` for explicit fallible conversions"); |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +/// Returns the bit width of a fixed-size integer type, or None if not a fixed-size int. |
| 66 | +fn fixed_type_bits(ty: Ty<'_>) -> Option<u64> { |
| 67 | + match ty.kind() { |
| 68 | + ty::Int(int_ty) => int_ty.bit_width(), |
| 69 | + ty::Uint(uint_ty) => uint_ty.bit_width(), |
| 70 | + _ => None, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// Determines if a cast between pointer-sized and fixed-size integers is always safe. |
| 75 | +/// |
| 76 | +/// Always safe casts (no architecture dependency): |
| 77 | +/// - Small fixed → ptr-sized: u8/i8/u16/i16 → usize/isize (ptr-sized is at least 16-bit) |
| 78 | +/// - Ptr-sized → large fixed: usize/isize → u64/i64/u128/i128 (ptr-sized is at most 64-bit) |
| 79 | +/// |
| 80 | +/// NOT safe (depends on architecture): |
| 81 | +/// - Large fixed → ptr-sized: u32/u64/etc → usize (may truncate on smaller ptr widths) |
| 82 | +/// - Ptr-sized → small fixed: usize → u8/u16/u32 (may truncate on larger ptr widths) |
| 83 | +fn is_always_safe_cast(from_is_ptr_sized: bool, fixed_bits: u64, from_signed: bool, to_signed: bool) -> bool { |
| 84 | + // Note: sign-change issues are handled by a separate lint (cast_sign_loss). Here we |
| 85 | + // only reason about whether the numeric magnitude will always fit regardless of |
| 86 | + // the target pointer width. |
| 87 | + |
| 88 | + if from_is_ptr_sized { |
| 89 | + // Casting from pointer-sized -> fixed-size: |
| 90 | + // - Pointer-sized integers (usize/isize) are at most 64 bits. |
| 91 | + // - A fixed-size target with >= 64 bits can always hold the magnitude of a pointer-sized value, but |
| 92 | + // we must respect signedness: |
| 93 | + // * isize -> i64/i128 is safe (from_signed == true and to_signed && fixed_bits >= 64) |
| 94 | + // * usize -> u64/u128 is safe (from_signed == false and !to_signed && fixed_bits >= 64) |
| 95 | + if fixed_bits < 64 { |
| 96 | + return false; |
| 97 | + } |
| 98 | + if to_signed { |
| 99 | + // Target is signed: safe only if source is signed (isize -> i64) |
| 100 | + from_signed && fixed_bits >= 64 |
| 101 | + } else { |
| 102 | + // Target is unsigned: safe only if source is unsigned (usize -> u64) |
| 103 | + !from_signed && fixed_bits >= 64 |
| 104 | + } |
| 105 | + } else if from_signed == to_signed { |
| 106 | + // Casting from fixed-size -> pointer-sized: |
| 107 | + // - Pointer-sized integers are at least 16 bits. |
| 108 | + // - Small fixed-size types (<= 16 bits) always fit in the smallest pointer width. |
| 109 | + // Same signedness: small fixed types (<=16 bits) are always safe. |
| 110 | + fixed_bits <= 16 |
| 111 | + } else { |
| 112 | + // Sign change: only the case unsigned small -> signed ptr is considered safe here. |
| 113 | + fixed_bits <= 16 && !from_signed |
| 114 | + } |
| 115 | +} |
0 commit comments