Skip to content
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

Merged
merged 1 commit into from Mar 20, 2024

Conversation

Jacherr
Copy link
Contributor

@Jacherr Jacherr commented Mar 15, 2024

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

@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2024

r? @blyxyas

rustbot has assigned @blyxyas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 15, 2024
@Alexendoo
Copy link
Member

I think the lint still applies here, the message could be updated but replacing a lossless cast with from is still a good suggestion

The type of the RHS could be changed in the future for example to a lossy cast

Copy link
Member

@blyxyas blyxyas left a 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))))
Copy link
Member

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.

Suggested change
&& (matches!(cast_to.kind(), ty::Int(IntTy::I128)) || matches!(cast_to.kind(), ty::Uint(UintTy::U128))))
&& matches!(cast_to.kind(), ty::Uint(UintTy::U128))

Copy link
Member

@blyxyas blyxyas left a 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?

@Jacherr Jacherr changed the title Disable cast_lossless when casting to u128/i128 from any (u)int type Disable cast_lossless when casting to u128 from any (u)int type Mar 20, 2024
@blyxyas
Copy link
Member

blyxyas commented Mar 20, 2024

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 20, 2024

📌 Commit 477108d has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Mar 20, 2024

⌛ Testing commit 477108d with merge 34766a6...

@bors
Copy link
Collaborator

bors commented Mar 20, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: blyxyas
Pushing 34766a6 to master...

@bors bors merged commit 34766a6 into rust-lang:master Mar 20, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cast_lossless should not warn about casts to u128
5 participants