Skip to content

clippy: fix cast_lossless lint - #71

Open
xtqqczze wants to merge 1 commit into
rust-lang:mainfrom
xtqqczze:clippy/cast_lossless
Open

clippy: fix cast_lossless lint#71
xtqqczze wants to merge 1 commit into
rust-lang:mainfrom
xtqqczze:clippy/cast_lossless

Conversation

@xtqqczze

Copy link
Copy Markdown

@xtqqczze
xtqqczze force-pushed the clippy/cast_lossless branch from 8cd43ba to 32b7469 Compare July 24, 2026 13:47
@WaffleLapkin

Copy link
Copy Markdown
Member

Is there any motivation behind this?

@xtqqczze

Copy link
Copy Markdown
Author

The change avoids some as casts, making it clear for people reading the code that these conversions are lossless.

Comment thread Cargo.toml
rust-version = "1.77"

[workspace.lints.clippy]
cast_lossless = "warn"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the number of other as casts here anyway, I don't see much value in this. The lint is pedantic for a reason; if you want to "fix" a pedantic lint I think you need better justification than what it says in the lint, since that wasn't enough to make it warn-by-default even in clippy.

Comment thread src/lib.rs
Comment on lines -218 to 220
let full = (x as u128).wrapping_mul(y as u128);
let full = u128::from(x).wrapping_mul(u128::from(y));
let lo = full as u64;
let hi = (full >> 64) as u64;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to change something here, I think a much better change would to stop needing all the explicit widening and narrowing in the first place.

The fix for the ases here isn't from, IMHO, but something more like

let (lo, hi) = x.carrying_mul(y, 0);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants