clippy::cast_possible_truncation
is inconsistent in recognizing where values will never be truncated
#12721
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-positive
Issue: The lint was triggered on code it shouldn't have
Summary
In some expressions, the lint recognizes that the u16 that is being casted to u8 will always be a valid u8 but in other expressions it does not. I'm not sure whether this is just a limitation of the algorithm used to check for valid u8 internally or an actual bug, but even if it is a limitation of the algorithm it could be useful to others to expand the capabilities of the lint.
Lint Name
cast_possible_truncation
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
This clearly will never be an invalid u8, since the & operation with 0x00_ff means only the last 8 bits can be nonzero. Interestingly, a similar case where a value is bitshifted does result in the lint not triggering, so maybe it's possible to also do that for cases like this.
Where lint works correctly:
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: