Skip to content

cast_possible_wrap for convertion from u8 to isize #9337

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

Closed
quarthex opened this issue Aug 15, 2022 · 4 comments · Fixed by #10564
Closed

cast_possible_wrap for convertion from u8 to isize #9337

quarthex opened this issue Aug 15, 2022 · 4 comments · Fixed by #10564
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have L-pedantic Lint: Belongs in the pedantic lint group

Comments

@quarthex
Copy link

Summary

The lint cast_possible_wrap is triggered by a conversion from u8 to isize.

The displayed message is:

casting `u8` to `isize` may wrap around the value on targets with 32-bit wide pointers

On 16/32/64-bits platforms, the message is wrong. An 8-bit unsigned value fits in a 16/32/64-bits signed register.

Reproducer

#![warn(clippy::cast_possible_wrap)]
const A: u8 = 0;
const B: isize = A as isize;

Version

rustc 1.63.0 (4b91a6ea7 2022-08-08)
binary: rustc
commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f
commit-date: 2022-08-08
host: x86_64-unknown-linux-gnu
release: 1.63.0
LLVM version: 14.0.5

Additional Labels

@rustbot label +I-false-positive +L-pedantic

@quarthex quarthex added the C-bug Category: Clippy is not doing the correct thing label Aug 15, 2022
@rustbot rustbot added I-false-positive Issue: The lint was triggered on code it shouldn't have L-pedantic Lint: Belongs in the pedantic lint group labels Aug 15, 2022
@quarthex
Copy link
Author

The lint applies on 8-bits platforms, though.

@asquared31415
Copy link
Contributor

Rust will never support platforms where u/isize is smaller than 16 bits and this lint is always incorrect. In fact isize::from(u8) exists, which means it definitely is a lossless conversion.

@quarthex
Copy link
Author

@asquared31415 If it want to be considered as a language targeted for embedded development, Rust should be able to produce code for such platforms. In my company, we are trying to deal with shortages of microcontrollers such as STM32 (mainly reserved by the car industry) and we will use TinyAVR micrcontrollers instead.
So, saying that Rust will never support 8-bit platforms is a little dramatic from my point of view.

@asquared31415
Copy link
Contributor

Unfortunately Rust has already made guarantees that are incompatible with that. It's well documented that usize is at least 16 bits large in The Reference and changing that would also require breaking changes to the standard library, namely the removal of the isize::from(u8) impl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have L-pedantic Lint: Belongs in the pedantic lint group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants