Skip to content

Conversation

@tomcur
Copy link
Member

@tomcur tomcur commented Jan 15, 2026

clippy::cast_possible_wrap checks for casting an unsigned integer to a signed integer of the same size. If the value is too large to fit in the signed integer, it will wrap around to negative numbers. Such casts will usually be used when doing some math, so scrutiny of the cast is probably welcome. It's similar to the already-enabled clippy::cast_possible_truncation.

See linebender/vello#1364 for an example where the lint is useful.

The similar lint clippy::cast_sign_loss more or less does the opposite, triggering for casts from signed to unsigned numeric types. Floats are included in this lint, as they do a saturating cast, making it more likely to have false positives: it triggers 65 times on vello_common currently. Perhaps this one should not be added to the canonical lint set (and I have not proposed adding it here).

For the interested reader, rust-lang/rust-clippy#9231 has some further discussion of these lints.

`clippy::cast_possible_wrap` checks for casting an unsigned integer to a
signed integer of the same size. If the value is too large to fit in the
signed integer, it will wrap around to negative numbers. Such casts will
usually be used when doing some math, so scrutiny of the cast is
probably welcome. It's similar to the already-enabled
`clippy::cast_possible_truncation`.

See linebender/vello#1364 for an example where
the lint is useful.

The similar lint `clippy::cast_sign_loss` more or less does the
opposite, triggering for casts from signed to unsigned numeric types.
Floats are included in this lint, as they do a saturating cast, making
it more likely to have false positives: it triggers 65 times on
`vello_common` currently. Perhaps this one should not be added to the
canonical lint set.
Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

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

The important reason for my approval here is that uN::cast_signed are now a more idiomatic way to represent the cases where these semantics are desired.

Copy link
Member

@xStrom xStrom left a comment

Choose a reason for hiding this comment

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

In theory this should detect cases that require extra attention, so it's fine.

The classic Clippy fear is there, that it will be super noisy. However, I ran this against the Xilem repo and didn't get a single hit, Kurbo only got two hits. So perhaps it's not too noisy after all. Let's try it out!

@DJMcNab
Copy link
Member

DJMcNab commented Jan 16, 2026

See also rust-lang/rust-clippy#16407

@tomcur tomcur merged commit 24b9349 into linebender:main Jan 16, 2026
2 checks passed
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