Description
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:
- The
overflowing_literals
lint is split into a separate lint alongsideoverflowing_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 theoverflowing_literals
lint on. - 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
Labels
Type
Projects
Status