New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable cast_lossless
when casting to u128 from any (u)int type
#12496
Conversation
I think the lint still applies here, the message could be updated but replacing a lossless cast with The type of the RHS could be changed in the future for example to a lossy cast |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meow meow meow =^w^=
// If source is bool, still lint due to the lint message differing (refers to style) | ||
if in_constant(cx, expr.hir_id) | ||
|| (!cast_from.is_bool() | ||
&& (matches!(cast_to.kind(), ty::Int(IntTy::I128)) || matches!(cast_to.kind(), ty::Uint(UintTy::U128)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Casting from a u128
to a i128
could be dangerous. u128
is the only data type that we can be sure that they won't cause an error.
&& (matches!(cast_to.kind(), ty::Int(IntTy::I128)) || matches!(cast_to.kind(), ty::Uint(UintTy::U128)))) | |
&& matches!(cast_to.kind(), ty::Uint(UintTy::U128)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay, I actually viewed this change the day you committed it but forgot to approve it 😅
LGTM, thanks! ❤️ Could you squash the commits into one?
cast_lossless
when casting to u128/i128 from any (u)int typecast_lossless
when casting to u128 from any (u)int type
@bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fixes #12492
Disables
cast_lossless
when casting to u128 from any int or uint type. The lint states that when casting to any int type, there can potentially be lossy behaviour if the source type ever exceeds the size of the destination type in the future, which is impossible with a destination of u128.It's possible this is a bit of a niche edge case which is better addressed by just disabling the lint in code, but I personally couldn't think of any good reason to still lint in this specific case - maybe except if the source is a bool, for readability reasons :).
changelog: FP:
cast_lossless
: disable lint when casting to u128 from any (u)int type