-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Have a lint against usize-to-u64 casts (or, against *all* integer casts) #9231
Comments
@rustbot claim |
Can someone please reopen? The bug is not fixed (tested on playground). |
Yup, sorry there has been a misunderstanding, which is why it was closed. |
@rustbot claim |
Hey @xFrednet, I believe there has been no active development on this issue for the last couple of months, right? Do I understand correctly that the approach would be to create a new lint that warns on integer casts using |
Hey @gernot-ohner, you should be able to claim this, since there hasn't been any activity to my knowledge. For the correct approach, it depends a bit on how @RalfJung would like to use the new lint. My understanding, is that they want to restrict all integer casts. Looking at our lint list I noticed cast_lossless which sounds like a lint, that should trigger on this. But it doesn't (See Playground) and it's also interesting, that it doesn't even trigger on the example from the lint description. So, I think this sounds like a false positive. It probably makes sense to first look at the code of that lint, and see why it doesn't trigger on the example from the lint description and the example from this issue description. |
@rustbot claim |
|
@Jarcho Thank you for the input. So, would you recommend adding a new lint? |
I think a restriction lint for all integer casts would be the best way. It feels strange to make a lint for only the one cast and the catch-all lint could be made to not trigger when any of the other related lints do in order to avoid the duplicate messages. We could also add the case to |
So |
If the lang team wants to limit the maximum size of Bonus points in the far off future when someone who hopefully isn't me gets to curse the idiots who couldn't conceive of the need for more 64 bits worth of byte-addressable storage. |
If we assume that we keep adding more larger Having an integer type with no static upper bound means that reasoning about it for conversions is difficult. |
I don't think there should be a separate #![deny(
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_precision_loss,
clippy::cast_ptr_alignment,
clippy::cast_sign_loss,
clippy::char_lit_as_u8,
clippy::checked_conversions,
clippy::fn_to_numeric_cast,
clippy::fn_to_numeric_cast_with_truncation,
clippy::ptr_as_ptr,
clippy::unnecessary_cast,
clippy::useless_conversion
)]
const CHUNK_LEN: u32 = 1234567;
extern "C" {
fn foo(p: *mut u8, len: u32);
}
fn foo_wrapper(slice: &mut [u8]) {
// Clippy should warn that this is potentially truncating, e.g. if `usize` is 16 bits,
// but it doesn't.
for chunk in slice.chunks_mut(CHUNK_LEN as usize) {
#[allow(clippy::cast_possible_truncation)]
let chunk_len = chunk.len() as u32;
unsafe { foo(chunk.as_mut_ptr(), chunk_len) }
}
} With In other words, |
In my code bases, I write two functions that make it clear that we only support targets where usize is 32 or 64 bits:
This is the only place where we'd need to add Perhaps, though, we could have directives to tell clippy our minimum and maximum
|
As a Rust beginner, I wanted to share my experience using Rust for operating system development. While Rust isn't ideal for every aspect of OS development, its tooling, especially Clippy, has been invaluable for catching bugs early. Our OS supports only 64-bit ISAs, so We use pedantic Clippy to ensure our code is as bug-free as possible. However, we've encountered an issue: Clippy generates warnings for Currently, we cannot selectively allow Please let me know if this issue is related enough to be included under the above-mentioned one, or if I should create a separate issue for it. Cheers. |
What it does
I would like clippy to lint against all integer casts. So I have set:
However, I just by accident noticed that this does not lint against usize-to-u64 casts. I guess
cast_possible_truncation
says "this cannot truncate because we don't have more than 64bit pointer size", and "cast_lossless" says "ah this might be lossy on platforms with pointers larger than 64bit", and then neither of them does anything.I would be happy to either have one of these lints also trigger on usize-to-u64 casts, or to have a new lint against all integer casts.
Lint Name
cast_integer
Category
pedantic
Advantage
Integer casts are subtle and should be done via
From
/TryFrom
, never viaas
, so I want to rule out all of them in my codebase.Drawbacks
No response
Example
Could be written as:
The text was updated successfully, but these errors were encountered: