Skip to content

Incorrect overflowing literals error with saturating_sub #63733

Closed
@bjorn3

Description

@bjorn3
fn main() {
    assert_eq!(i8::min_value(), -128i8);
    println!("{}", -128i8.saturating_sub(-1));
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error: literal out of range for `i8`
 --> src/main.rs:3:21
  |
3 |     println!("{}", -128i8.saturating_sub(-1));
  |                     ^^^^^
  |
  = note: #[deny(overflowing_literals)] on by default

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

-128 is not out of range for i8, however the lint claims it is. This doesn't happen when printing a plain -128i8 without .saturating_sub. It also doesn't happen when the -128i8 is passed as arg to .saturating_sub.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.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