Skip to content

Incorrect suggesetion in unnecessary_cast lint #8331

@sunfishcode

Description

@sunfishcode

Summary

Clippy suggests changing -1_i32 as i32 to 1_i32, which produces a different value. This happens with current stable and nightly Rust.

Reproducer

I tried this code:

fn main() {
    let _ = -1_i32 as i32;
}

I expected to see clippy suggest -1_i32.

Instead, this happened:

warning: casting integer literal to `i32` is unnecessary
 --> src/main.rs:2:13
  |
2 |     let _ = -1_i32 as i32;
  |             ^^^^^^^^^^^^^ help: try: `1_i32`
  = note: `#[warn(clippy::unnecessary_cast)]` on by default

Version

rustc 1.60.0-nightly (777bb86bc 2022-01-20)
binary: rustc
commit-hash: 777bb86bcdbc568be7cff6eeeaaf81a89b4aa50b
commit-date: 2022-01-20
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions