Skip to content

Commit

Permalink
Clippy cleanups (#144)
Browse files Browse the repository at this point in the history
This updates the clippy::integer_arithmetic to its new name
arithmetic_side_effects and addresses some trivial cases.
  • Loading branch information
nresare authored Aug 2, 2023
1 parent 3c4b5f3 commit 0e34553
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ssh-cipher/src/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
#[allow(clippy::integer_arithmetic)]
#[allow(clippy::arithmetic_side_effects)]
if key.len() != KEY_SIZE * 2 {
return Err(Error::KeySize);
}
Expand Down
4 changes: 2 additions & 2 deletions ssh-cipher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ssh-encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ssh-key/src/certificate/unix_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
SystemTime::now().try_into()
}
Expand Down
13 changes: 7 additions & 6 deletions ssh-key/src/fingerprint/randomart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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;
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ssh-key/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions ssh-key/src/public/ssh_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions ssh-key/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ impl Verifier<Signature> 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()));
Expand Down Expand Up @@ -451,7 +451,7 @@ impl TryFrom<&p256::ecdsa::Signature> for Signature {
fn try_from(signature: &p256::ecdsa::Signature) -> Result<Signature> {
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)?;
Expand All @@ -473,7 +473,7 @@ impl TryFrom<&p384::ecdsa::Signature> for Signature {
fn try_from(signature: &p384::ecdsa::Signature) -> Result<Signature> {
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)?;
Expand Down

0 comments on commit 0e34553

Please sign in to comment.