Numeric cast to a narrower type may truncate

ID

rust.portability.cast_possible_truncation

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Numeric Conversion

Language

Rust

Description

This as cast narrows the value, so any bits above the destination width are discarded silently. Use try_into() and handle the error, or widen the destination type.

Rationale

This as cast narrows the value, so any bits above the destination width are discarded silently. Use try_into() and handle the error, or widen the destination type.

The following code illustrates the pattern detected by this rule:

pub fn casts(wide: u64, small: u8, byte_count: usize) {
    // FLAGGED: Numeric cast to a narrower type may truncate
    let a = wide as u8;
    // FLAGGED: Numeric cast to a narrower type may truncate
    let b = byte_count as u32;
    let c = small as u64;
    let d = small as u8;
    println!("{} {} {} {}", a, b, c, d);
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved