-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Disable cast_lossless
when casting to u128 from any (u)int type
#12496
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
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 |
I'm not sure this should have been turned off. To expand on what @Alexendoo wrote: This lint guards against two possible changes which would make the cast lossless.
While For instance, take this code. type Id = u128;
fn from_database_id(x: u64) -> Id {
x as Id
} If |
Your concern is valid, but I think cast_possible_truncation would fire in that case (playground). (I should have mentioned this in #12492.) |
If anything that triggers |
I am willing to believe that, but is that an argument for keeping things the way they were? Could you explain? |
It was to say that it's consistent with the other My view of it is the lint suggests replacing There are lints to catch these potentially unexpected casts, but they're all in For the suggestion of replacing But also changes to the type of |
Thank you very much for explaining. I think this is the main point where we disagree:
I agree this could happen, but it feels remote. To me, a change the type of the expression seems far more likely, and flagging such possibilities is where the lint really provides value. Having said that, this is not a hill I'd want to die on. So thank you again for taking the time to write #12496 (comment). |
Thanks, there's a while until the next meeting so let's just go with a poll since it's split: |
@Alexendoo Thank you for opening the poll. @blyxyas Thank you for reviewing this PR. @Jacherr Thank you jumping on my issue and resolving it. I'm sorry that your contribution will likely be reverted. |
Lint casts to `u128` in `cast_lossless` Reverts #12496 per https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Should.20.60as.20u128.60.20trigger.20cast_lossless Also changes the lint messages and refactors the suggestion production - Fixes #12695 changelog: [`cast_lossless`]: lint casts to `u128`
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