RS.CLIPPY.UNNECESSARY_CAST

Cast to the same type, e.g., `x as i32` where `x: i32`

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: unnecessary_cast. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for casts to the same type, casts of int literals to integer types, casts of float literals to float types, and casts between raw pointers that don't change type or constness.

Why is this bad?

It's just unnecessary.

Known problems

When the expression on the left is a function call, the lint considers the return type to be a type alias if it's aliased through a use statement (like use std::io::Result as IoResult). It will not lint such cases.

This check will only work on primitive types without any intermediate references: raw pointers and trait objects may or may not work.

Example

let _ = 2i32 as i32;
let _ = 0.5 as f32;

Better:

let _ = 2_i32;
let _ = 0.5_f32;