-
Notifications
You must be signed in to change notification settings - Fork 182
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
Replace more casts with safer conversions & enable cast-related lints. #445
Conversation
d29aa27
to
125a3e6
Compare
d9b4a3a
to
90b24d4
Compare
I have rebased this on top of #447 and extended it to address additional Clippy feedback from that change. |
f331ab0
to
67f3bd5
Compare
4008b07
to
7a89e48
Compare
usize and u32 both implement `From<u16>` so use u16 for the length.
#![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 | ||
)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to deny entire categories of lints (pedantic
, correctness
, complexity
, etc...) rather than specific ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, with the number of additional lints it may be better to enable them in clippy.toml
.
d06067f
to
4f8d78d
Compare
@@ -27,7 +27,9 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | |||
// Prevent overflow of u32 | |||
let chunk_size = usize::try_from(i32::MAX).expect("Windows does not support 16-bit targets"); | |||
for chunk in dest.chunks_mut(chunk_size) { | |||
let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr().cast::<c_void>(), chunk.len() as u32) }; | |||
#[allow(clippy::cast_possible_truncation)] | |||
let chunk_len = chunk.len() as u32; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can write it as chunk.len().try_into().expect("chunk size is bounded by i32::MAX")
. Compiler is able to properly remove such panic.
// The chunk can be smaller than buf's length, so we call to | ||
// JS to create a smaller view of buf without allocation. | ||
#[allow(clippy::cast_possible_truncation)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use .try_into().expect("..")
here same as in windows7.
Closing in favor of #510. |
No description provided.