Skip to content

overflowing_literals for signed overflow should recommend cast_signed() rather than as #160403

Description

@tgross35

Code

fn main() {
    let _: i32 = 0xC020660B;
}


https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=2fdce59d2c87bae32e0270d3468da8d1

Current output

error: literal out of range for `i32`
 --> src/main.rs:2:18
  |
2 |     let _: i32 = 0xC020660B;
  |                  ^^^^^^^^^^
  |
  = note: the literal `0xC020660B` (decimal `3223348747`) does not fit into the type `i32` and will become `-1071618549i32`
  = help: consider using the type `u32` instead
  = note: `#[deny(overflowing_literals)]` on by default
help: to use as a negative number (decimal `-1071618549`), consider using the type `u32` for the literal and cast it to `i32`
  |
2 |     let _: i32 = 0xC020660Bu32 as i32;
  |                            ++++++++++

Desired output

Recommend cast_signed() rather than the as cast. It has been around for a little while and is guarantees the conversion is same-size without loss of data.

Rust Version

1.99.0-nightly 2025-08-01

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions