Skip to content

Commit ea89514

Browse files
authored
Lint improvements (#72)
Expands the number of lints applied to the code, particularly clippy lints, and fixes any current violations.
1 parent e5abe06 commit ea89514

File tree

6 files changed

+52
-22
lines changed

6 files changed

+52
-22
lines changed

src/aead/chacha20.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ pub struct Chacha20Poly1305;
1919
impl Tls13AeadAlgorithm for Chacha20Poly1305 {
2020
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
2121
Box::new(Tls13Cipher(
22-
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref()).unwrap(),
22+
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref())
23+
.expect("key should be valid"),
2324
iv,
2425
))
2526
}
2627

2728
fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
2829
Box::new(Tls13Cipher(
29-
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref()).unwrap(),
30+
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref())
31+
.expect("key should be valid"),
3032
iv,
3133
))
3234
}
@@ -48,14 +50,16 @@ impl Tls13AeadAlgorithm for Chacha20Poly1305 {
4850
impl Tls12AeadAlgorithm for Chacha20Poly1305 {
4951
fn encrypter(&self, key: AeadKey, iv: &[u8], _: &[u8]) -> Box<dyn MessageEncrypter> {
5052
Box::new(Tls12Cipher(
51-
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref()).unwrap(),
53+
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref())
54+
.expect("key should be valid"),
5255
Iv::copy(iv),
5356
))
5457
}
5558

5659
fn decrypter(&self, key: AeadKey, iv: &[u8]) -> Box<dyn MessageDecrypter> {
5760
Box::new(Tls12Cipher(
58-
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref()).unwrap(),
61+
chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref())
62+
.expect("key should be valid"),
5963
Iv::copy(iv),
6064
))
6165
}
@@ -79,7 +83,7 @@ impl Tls12AeadAlgorithm for Chacha20Poly1305 {
7983
debug_assert_eq!(NONCE_LEN, iv.len());
8084
Ok(ConnectionTrafficSecrets::Chacha20Poly1305 {
8185
key,
82-
iv: Iv::new(iv[..].try_into().unwrap()),
86+
iv: Iv::new(iv[..].try_into().expect("conversion should succeed")),
8387
})
8488
}
8589
}
@@ -89,7 +93,7 @@ struct Tls13Cipher(chacha20poly1305::ChaCha20Poly1305, Iv);
8993
impl MessageEncrypter for Tls13Cipher {
9094
fn encrypt(
9195
&mut self,
92-
m: OutboundPlainMessage,
96+
m: OutboundPlainMessage<'_>,
9397
seq: u64,
9498
) -> Result<OutboundOpaqueMessage, rustls::Error> {
9599
let total_len = self.encrypted_payload_len(m.payload.len());
@@ -143,7 +147,7 @@ struct Tls12Cipher(chacha20poly1305::ChaCha20Poly1305, Iv);
143147
impl MessageEncrypter for Tls12Cipher {
144148
fn encrypt(
145149
&mut self,
146-
m: OutboundPlainMessage,
150+
m: OutboundPlainMessage<'_>,
147151
seq: u64,
148152
) -> Result<OutboundOpaqueMessage, rustls::Error> {
149153
let total_len = self.encrypted_payload_len(m.payload.len());

src/aead/gcm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ macro_rules! impl_gcm_tls13 {
6060
struct [<Tls13Cipher $name>]($aead, cipher::Iv);
6161

6262
impl MessageEncrypter for [<Tls13Cipher $name>] {
63-
fn encrypt(&mut self, m: OutboundPlainMessage, seq: u64) -> Result<OutboundOpaqueMessage, rustls::Error> {
63+
fn encrypt(&mut self, m: OutboundPlainMessage<'_>, seq: u64) -> Result<OutboundOpaqueMessage, rustls::Error> {
6464
let total_len = self.encrypted_payload_len(m.payload.len());
6565
let mut payload = PrefixedPayload::with_capacity(total_len);
6666

@@ -156,7 +156,7 @@ macro_rules! impl_gcm_tls12 {
156156

157157
#[cfg(feature = "tls12")]
158158
impl MessageEncrypter for [<Tls12Cipher $name Encrypter>] {
159-
fn encrypt(&mut self, m: OutboundPlainMessage, seq: u64) -> Result<OutboundOpaqueMessage, rustls::Error> {
159+
fn encrypt(&mut self, m: OutboundPlainMessage<'_>, seq: u64) -> Result<OutboundOpaqueMessage, rustls::Error> {
160160
let total_len = self.encrypted_payload_len(m.payload.len());
161161
let mut payload = PrefixedPayload::with_capacity(total_len);
162162

src/lib.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@
66
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
77
)]
88
#![warn(
9-
clippy::all,
10-
// TODO: clippy::pedantic,
119
clippy::alloc_instead_of_core,
10+
clippy::cast_lossless,
11+
clippy::cast_possible_truncation,
12+
clippy::cast_possible_wrap,
13+
clippy::cast_precision_loss,
14+
clippy::cast_sign_loss,
15+
clippy::checked_conversions,
16+
clippy::from_iter_instead_of_collect,
17+
clippy::missing_errors_doc,
18+
clippy::mod_module_files,
19+
clippy::implicit_saturating_sub,
20+
clippy::panic,
21+
clippy::panic_in_result_fn,
1222
clippy::std_instead_of_alloc,
13-
clippy::std_instead_of_core
23+
clippy::std_instead_of_core,
24+
clippy::unwrap_used,
25+
rust_2018_idioms,
26+
trivial_numeric_casts,
27+
unused_lifetimes
1428
)]
1529

1630
//! # Usage

src/quic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ impl PacketKey {
5959
Self {
6060
iv,
6161
suite,
62-
crypto: chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref()).unwrap(),
62+
crypto: chacha20poly1305::ChaCha20Poly1305::new_from_slice(key.as_ref())
63+
.expect("key should be valid"),
6364
}
6465
}
6566
}

src/sign.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,37 @@ where
6767
}
6868
}
6969

70+
/// Extract any supported key from the given DER input.
71+
///
72+
/// # Errors
73+
///
74+
/// Returns an error if the key couldn't be decoded.
7075
pub fn any_supported_type(der: &PrivateKeyDer<'_>) -> Result<Arc<dyn SigningKey>, rustls::Error> {
71-
let rsa = |_| RsaSigningKey::try_from(der).map(|x| Arc::new(x) as _);
72-
73-
rsa(())
76+
RsaSigningKey::try_from(der)
77+
.map(|x| Arc::new(x) as _)
7478
.or_else(|_| any_ecdsa_type(der))
7579
.or_else(|_| any_eddsa_type(der))
7680
}
7781

82+
/// Extract any supported ECDSA key from the given DER input.
83+
///
84+
/// # Errors
85+
///
86+
/// Returns an error if the key couldn't be decoded.
7887
pub fn any_ecdsa_type(der: &PrivateKeyDer<'_>) -> Result<Arc<dyn SigningKey>, rustls::Error> {
7988
let p256 = |_| EcdsaSigningKeyP256::try_from(der).map(|x| Arc::new(x) as _);
8089
let p384 = |_| EcdsaSigningKeyP384::try_from(der).map(|x| Arc::new(x) as _);
8190
p256(()).or_else(p384)
8291
}
8392

93+
/// Extract any supported EDDSA key from the given DER input.
94+
///
95+
/// # Errors
96+
///
97+
/// Returns an error if the key couldn't be decoded.
8498
pub fn any_eddsa_type(der: &PrivateKeyDer<'_>) -> Result<Arc<dyn SigningKey>, rustls::Error> {
85-
let ed25519 = |_| Ed25519SigningKey::try_from(der).map(|x| Arc::new(x) as _);
86-
8799
// TODO: Add support for Ed448
88-
89-
ed25519(())
100+
Ed25519SigningKey::try_from(der).map(|x| Arc::new(x) as _)
90101
}
91102

92103
pub mod ecdsa;

tests/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn integrate_client_builder_with_details_fake() {
4040
let rustls_client_config = dangerous_verifier.with_no_client_auth();
4141

4242
// RustCrypto is not fips
43-
assert_eq!(rustls_client_config.fips(), false);
43+
assert!(!rustls_client_config.fips());
4444
}
4545

4646
use rustls::DistinguishedName;
@@ -79,5 +79,5 @@ fn integrate_server_builder_with_details_fake() {
7979
dangerous_verifier.with_cert_resolver(Arc::new(server_cert_resolver));
8080

8181
// RustCrypto is not fips
82-
assert_eq!(rustls_client_config.fips(), false);
82+
assert!(!rustls_client_config.fips());
8383
}

0 commit comments

Comments
 (0)