Skip to content

Handle negative literals in cast overflow warning #48535

Closed

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.

Activity

  1. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    on Feb 25, 2018
  2. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    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.
    on Oct 18, 2019
  3. added a commit that references this issue on Mar 8, 2021
    0ee2f4c
  4. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    and removed
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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