Skip to content

overflowing_literals should have an option for "ignore signed overflows" #99195

Open
@clarfonthey

Description

@clarfonthey

I have recently been working with code where using the two's complement form of signed literals is useful, but this doesn't mesh very well with the overflowing_literals lint.

Here's a simplified example:

let good_signed = 0b_1111_1111i8;
let bad_signed = 0b_111_111_111i8;
let good_unsigned = 0b_1111_1111u8;
let bad_unsigned = 0b_111_111_111u8;

Right now, with the overflowing_literals lint, good_signed, bad_signed, and bad_unsigned will all be marked as overflowing. To avoid good_signed being marked as invalid, I had code that I blanket marked as #[allow(overflowing_literals)], but this caused me to miss literals like bad_signed and bad_unsigned, which overflow regardless of their sign.

I'm not sure what the best strategy here is. I see two possible solutions:

  1. The overflowing_literals lint is split into a separate lint alongside overflowing_literals lint, where this new lint warns on integer literals that would not overflow if they were unsigned, but do overflow because they're signed. This can be selectively allowed in code while still being able to keep the overflowing_literals lint on.
  2. Instead of splitting the lint, no lint is thrown at all in the case where a signed integer overflows to a negative number because it's written in two's complement form. Or, alternatively, the lint is only triggered if the integer is written in base-10, but not if it's written in hexadecimal, octal, or binary.

Either way, the current configuration of the lint seems overly restrictive.

After searching, I decided to file a separate issue instead of including this analysis in #53628. If this is seen as too similar to the existing ticket, I'm fine with also closing this in favour of that one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    Idea

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions