Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::cast_abs_to_unsigned: casting the result of `i32::abs()` to u32
What it meansChecks for usage of the abs() method that cast the result to unsigned. It is in the suspicious group and warns by default.
What you see
warning: casting the result of `i32::abs()` to u32 --> main.rs:4:14 | 4 | let y: u32 = x.abs() as u32; | ^^^^^^^^^^^^^^ help: replace with: `x.unsigned_abs()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#cast_abs_to_unsigned = note: `#[warn(clippy::cast_abs_to_unsigned)]` 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?
The unsigned_abs() method avoids panic when called on the MIN value.
Code that is most likely wrong or useless. 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 x: i32 = -42;
let y: u32 = x.abs() as u32;
Write this instead
let x: i32 = -42;
let y: u32 = x.unsigned_abs();
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::cast_abs_to_unsigned)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
cast_abs_to_unsigned = "allow"One run
cargo clippy -- -A clippy::cast-abs-to-unsignedThe [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.
Settings in clippy.toml
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Checks for usage of the abs() method that cast the result to unsigned.
Why is this bad?
The unsigned_abs() method avoids panic when called on the MIN value.
Example
let x: i32 = -42;
let y: u32 = x.abs() as u32;
Use instead:
let x: i32 = -42;
let y: u32 = x.unsigned_abs();
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- blanket_clippy_restriction_lints`clippy::restriction` is not meant to be enabled as a group
- block_scrutineeWarns when a match, if let, or while let scrutinee is wrapped in a block. This lint only triggers on the 2021 edition and older
- by_ref_peekable_peekcalling `.by_ref().peekable().peek()` will advance the underlying iterator and consume its first output
- cast_enum_constructorcast of an enum tuple constructor to an integer
- cast_enum_truncationcasting `main::E::X` to `u8` will truncate the value
- cast_nan_to_intcasting a known NaN to u64
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::blanket_clippy_restriction_lints`clippy::restriction` is not meant to be enabled as a groupRead it next →Did this answer your question?