Clippy lints / Complexity
Clippy lint · Complexity · checked with Clippy 0.1.99
clippy::unnecessary_cast: casting integer literal to `i32` is unnecessary
What it meansChecks 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. It is in the complexity group and warns by default.
What you see
warning: casting integer literal to `i32` is unnecessary --> main.rs:3:9 | 3 | let _ = 2i32 as i32; | ^^^^^^^^^^^ help: try: `2_i32` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#unnecessary_cast = note: `#[warn(clippy::unnecessary_cast)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
It’s just unnecessary.
Code that does something simple in a complicated way. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let _ = 2i32 as i32;
let _ = 0.5 as f32;
Write this instead
let _ = 2_i32;
let _ = 0.5_f32;
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::unnecessary_cast)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
unnecessary_cast = "allow"One run
cargo clippy -- -A clippy::unnecessary-castThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
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;
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_argumentsChecks for functions with too many parameters
- clone_on_copyusing `clone` on type `u64` which implements the `Copy` trait
- type_complexityChecks for types used in structs, parameters and let declarations above a certain complexity threshold
- transmutes_expressible_as_ptr_castsChecks for transmutes that could be a pointer cast
- unit_argChecks for passing a unit value as an argument to a function without using a unit literal (())
- unnecessary_filter_mapthis `.filter_map(..)` can be written more simply using `.filter(..)`
- unnecessary_find_mapthis `.find_map(..)` can be written more simply using `.find(..)`
- unnecessary_first_then_checkunnecessary use of `first().is_none()` to check if slice is empty
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_argumentsChecks for functions with too many parametersRead it next →Did this answer your question?