Cast to the type the expression already has

ID

rust.maintainability.unnecessary_cast

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Redundant Code

Language

Rust

Description

Detects an as cast whose destination is the same numeric type the operand is already declared with, so the conversion has no effect.

Rationale

A no-op cast is noise that a reader has to stop and check, and it reads as a claim that the two sides have different types. It also hides later drift: if the operand’s type is widened one day, the cast quietly narrows it back instead of failing to compile and pointing at the place that needs attention.

The following code illustrates the pattern detected by this rule:

pub fn totals(count: u32, len: usize, ratio: f64, delta: i32, marker: u8) -> f64 {
    // FLAGGED: Cast to the type the expression already has
    let a = count as u32;
    // FLAGGED: Cast to the type the expression already has
    let b = len as usize;
    // FLAGGED: Cast to the type the expression already has
    let c = ratio as f64;
    // FLAGGED: Cast to the type the expression already has
    let d = delta as i32;
    // FLAGGED: Cast to the type the expression already has
    let e = marker as u8;
    a as f64 + b as f64 + c + d as f64 + e as f64
}

Remediation

Delete the cast. When it is there to pin the type of an inferred value, state the type on the binding instead (let n: u32 = …​;), which keeps the intent and still fails the build if the source type changes.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved