From 0e34553e3606e258d37cc641ca3b1e68bcb61fc1 Mon Sep 17 00:00:00 2001 From: Noa Resare Date: Wed, 2 Aug 2023 17:06:44 +0100 Subject: [PATCH] Clippy cleanups (#144) This updates the clippy::integer_arithmetic to its new name arithmetic_side_effects and addresses some trivial cases. --- ssh-cipher/src/chacha20poly1305.rs | 2 +- ssh-cipher/src/lib.rs | 4 ++-- ssh-encoding/src/lib.rs | 2 +- ssh-key/src/certificate/unix_time.rs | 2 +- ssh-key/src/fingerprint/randomart.rs | 13 +++++++------ ssh-key/src/lib.rs | 2 +- ssh-key/src/public/ssh_format.rs | 4 +--- ssh-key/src/signature.rs | 6 +++--- 8 files changed, 17 insertions(+), 18 deletions(-) diff --git a/ssh-cipher/src/chacha20poly1305.rs b/ssh-cipher/src/chacha20poly1305.rs index aab2df2..751db88 100644 --- a/ssh-cipher/src/chacha20poly1305.rs +++ b/ssh-cipher/src/chacha20poly1305.rs @@ -39,7 +39,7 @@ impl ChaCha20Poly1305 { /// /// [PROTOCOL.chacha20poly1305]: https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.chacha20poly1305?annotate=HEAD pub fn new(key: &[u8], nonce: &[u8]) -> Result { - #[allow(clippy::integer_arithmetic)] + #[allow(clippy::arithmetic_side_effects)] if key.len() != KEY_SIZE * 2 { return Err(Error::KeySize); } diff --git a/ssh-cipher/src/lib.rs b/ssh-cipher/src/lib.rs index bec5a60..3fc77e9 100644 --- a/ssh-cipher/src/lib.rs +++ b/ssh-cipher/src/lib.rs @@ -7,7 +7,7 @@ )] #![forbid(unsafe_code)] #![warn( - clippy::integer_arithmetic, + clippy::arithmetic_side_effects, clippy::panic, clippy::panic_in_result_fn, clippy::unwrap_used, @@ -201,7 +201,7 @@ impl Cipher { /// Compute the length of padding necessary to pad the given input to /// the block size. - #[allow(clippy::integer_arithmetic)] + #[allow(clippy::arithmetic_side_effects)] pub fn padding_len(self, input_size: usize) -> usize { match input_size % self.block_size() { 0 => 0, diff --git a/ssh-encoding/src/lib.rs b/ssh-encoding/src/lib.rs index 42920b0..08b7aa5 100644 --- a/ssh-encoding/src/lib.rs +++ b/ssh-encoding/src/lib.rs @@ -7,7 +7,7 @@ )] #![forbid(unsafe_code)] #![warn( - clippy::integer_arithmetic, + clippy::arithmetic_side_effects, clippy::panic, clippy::panic_in_result_fn, clippy::unwrap_used, diff --git a/ssh-key/src/certificate/unix_time.rs b/ssh-key/src/certificate/unix_time.rs index 02cc84b..b7a919f 100644 --- a/ssh-key/src/certificate/unix_time.rs +++ b/ssh-key/src/certificate/unix_time.rs @@ -59,7 +59,7 @@ impl UnixTime { } /// Get the current time as a Unix timestamp. - #[cfg(all(feature = "std"))] + #[cfg(feature = "std")] pub fn now() -> Result { SystemTime::now().try_into() } diff --git a/ssh-key/src/fingerprint/randomart.rs b/ssh-key/src/fingerprint/randomart.rs index 9db7a72..0879329 100644 --- a/ssh-key/src/fingerprint/randomart.rs +++ b/ssh-key/src/fingerprint/randomart.rs @@ -12,7 +12,7 @@ use core::fmt; const WIDTH: usize = 17; const HEIGHT: usize = 9; const VALUES: &[u8; 17] = b" .o+=*BOX@%&#/^SE"; -const NVALUES: usize = VALUES.len() - 1; +const NVALUES: u8 = VALUES.len() as u8 - 1; type Field = [[u8; WIDTH]; HEIGHT]; @@ -25,7 +25,8 @@ pub(super) struct Randomart<'a> { impl<'a> Randomart<'a> { /// Create new "randomart" from the given fingerprint. - #[allow(clippy::integer_arithmetic)] + // TODO: Remove this when the pipeline toolchain is updated beyond 1.69 + #[allow(clippy::arithmetic_side_effects)] pub(super) fn new(header: &'a str, fingerprint: Fingerprint) -> Self { let mut field = Field::default(); let mut x = WIDTH / 2; @@ -48,16 +49,16 @@ impl<'a> Randomart<'a> { x = x.min(WIDTH.saturating_sub(1)); y = y.min(HEIGHT.saturating_sub(1)); - if field[y][x] < NVALUES as u8 - 2 { - field[y][x] += 1; + if field[y][x] < NVALUES - 2 { + field[y][x] = field[y][x].saturating_add(1); } byte >>= 2; } } - field[HEIGHT / 2][WIDTH / 2] = NVALUES as u8 - 1; - field[y][x] = NVALUES as u8; + field[HEIGHT / 2][WIDTH / 2] = NVALUES - 1; + field[y][x] = NVALUES; Self { header, diff --git a/ssh-key/src/lib.rs b/ssh-key/src/lib.rs index 278730a..e50d13d 100644 --- a/ssh-key/src/lib.rs +++ b/ssh-key/src/lib.rs @@ -7,7 +7,7 @@ )] #![forbid(unsafe_code)] #![warn( - clippy::integer_arithmetic, + clippy::arithmetic_side_effects, clippy::mod_module_files, clippy::panic, clippy::panic_in_result_fn, diff --git a/ssh-key/src/public/ssh_format.rs b/ssh-key/src/public/ssh_format.rs index 1665f91..efc723a 100644 --- a/ssh-key/src/public/ssh_format.rs +++ b/ssh-key/src/public/ssh_format.rs @@ -111,9 +111,7 @@ impl<'a> SshFormat<'a> { /// rather than this estimate. #[cfg(feature = "alloc")] fn base64_len_approx(input_len: usize) -> usize { - // TODO(tarcieri): checked arithmetic - #[allow(clippy::integer_arithmetic)] - ((((input_len * 4) / 3) + 3) & !3) + (((input_len.saturating_mul(4)) / 3).saturating_add(3)) & !3 } /// Parse a segment of the public key. diff --git a/ssh-key/src/signature.rs b/ssh-key/src/signature.rs index 4723242..6081255 100644 --- a/ssh-key/src/signature.rs +++ b/ssh-key/src/signature.rs @@ -414,7 +414,7 @@ impl Verifier for public::SkEd25519 { let signature_bytes = &signature.as_bytes()[..signature_len]; let flags_and_counter = &signature.as_bytes()[signature_len..]; - #[allow(clippy::integer_arithmetic)] + #[allow(clippy::arithmetic_side_effects)] let mut signed_data = Vec::with_capacity((2 * Sha256::output_size()) + SK_ED25519_SIGNATURE_TRAILER_SIZE); signed_data.extend(Sha256::digest(self.application())); @@ -451,7 +451,7 @@ impl TryFrom<&p256::ecdsa::Signature> for Signature { fn try_from(signature: &p256::ecdsa::Signature) -> Result { let (r, s) = signature.split_bytes(); - #[allow(clippy::integer_arithmetic)] + #[allow(clippy::arithmetic_side_effects)] let mut data = Vec::with_capacity(32 * 2 + 4 * 2 + 2); Mpint::from_positive_bytes(&r)?.encode(&mut data)?; @@ -473,7 +473,7 @@ impl TryFrom<&p384::ecdsa::Signature> for Signature { fn try_from(signature: &p384::ecdsa::Signature) -> Result { let (r, s) = signature.split_bytes(); - #[allow(clippy::integer_arithmetic)] + #[allow(clippy::arithmetic_side_effects)] let mut data = Vec::with_capacity(48 * 2 + 4 * 2 + 2); Mpint::from_positive_bytes(&r)?.encode(&mut data)?;