Skip to content

Handle negative literals in cast overflow warning #48535

Closed
@estebank

Description

@estebank

#48432 introduced a warning note explaining why a given literal falls outside of the size of the expected type.

In the case of negative literals, the output can be confusing:

warning: literal out of range for i8
  --> $DIR/type-overflow.rs:34:17
   |
34 |     let fail = -0b1111_1111i8; //~WARNING literal out of range for i8
   |                 ^^^^^^^^^^^^^ help: consider using `i16` instead: `0b1111_1111i16`
   |
   = note: the literal `0b1111_1111i8` (decimal `255`) does not fit into an `i8` and will become `-1i8`

fail will end up with the value 1, as the binary literal evaluates to -1, but it is negated again. The warning should be extended to catch this case and either modify the existing note/label, or add an extra note explaining that the value is being affected further by the outer -, resulting in the final value 1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.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