Skip to content

Commit 42a1557

Browse files
authored
all: fix clippy warnings and add CI check (#191)
It's nice to fix these, and they make the code simpler in many cases. Fixes #185.
1 parent e3d310c commit 42a1557

File tree

10 files changed

+70
-58
lines changed

10 files changed

+70
-58
lines changed

.github/workflows/test.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,24 @@ jobs:
115115
with:
116116
command: fmt
117117
args: --all -- --check
118+
119+
clippy:
120+
name: Clippy
121+
runs-on: ubuntu-latest
122+
steps:
123+
- name: Checkout sources
124+
uses: actions/checkout@v2
125+
with:
126+
persist-credentials: false
127+
- name: Install rust toolchain
128+
uses: actions-rs/toolchain@v1
129+
with:
130+
toolchain: stable
131+
override: true
132+
default: true
133+
components: clippy
134+
- name: Check formatting
135+
uses: actions-rs/cargo@v1
136+
with:
137+
command: clippy
138+
args: --workspace

build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ fn main() {
1717
let dest_path = out_dir.join("version.rs");
1818
let mut f = File::create(&dest_path).expect("Could not create file");
1919
let pkg_version = env!("CARGO_PKG_VERSION");
20-
write!(
20+
writeln!(
2121
&mut f,
22-
r#"const RUSTLS_FFI_VERSION: &'static str = "rustls-ffi/{}/rustls/{}";
23-
"#,
22+
r#"const RUSTLS_FFI_VERSION: &str = "rustls-ffi/{}/rustls/{}";"#,
2423
pkg_version, RUSTLS_CRATE_VERSION
2524
)
2625
.expect("Could not write file");

src/cipher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl rustls_certified_key {
178178
};
179179
let certified_key = Arc::into_raw(Arc::new(*certified_key)) as *const _;
180180
*certified_key_out = certified_key;
181-
return rustls_result::Ok
181+
rustls_result::Ok
182182
}
183183
}
184184

@@ -228,7 +228,7 @@ impl rustls_certified_key {
228228
new_key.ocsp = None;
229229
}
230230
*cloned_key_out = ArcCastPtr::to_const_ptr(new_key);
231-
return rustls_result::Ok
231+
rustls_result::Ok
232232
}
233233
}
234234

src/client.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ impl rustls_client_config_builder {
132132
ffi_panic_boundary! {
133133
let cipher_suites: &[*const rustls_supported_ciphersuite] = try_slice!(cipher_suites, cipher_suites_len);
134134
let mut cs_vec: Vec<SupportedCipherSuite> = Vec::new();
135-
for &cs in cipher_suites.into_iter() {
135+
for &cs in cipher_suites.iter() {
136136
let cs = try_ref_from_ptr!(cs);
137137
match ALL_CIPHER_SUITES.iter().find(|&acs| cs.eq(acs)) {
138-
Some(scs) => cs_vec.push(scs.clone()),
138+
Some(scs) => cs_vec.push(*scs),
139139
None => return InvalidParameter,
140140
}
141141
}
@@ -157,7 +157,7 @@ impl rustls_client_config_builder {
157157
Err(_) => return rustls_result::InvalidParameter,
158158
};
159159
let config_builder = ClientConfigBuilder {
160-
base: base,
160+
base,
161161
verifier: Arc::new(NoneVerifier),
162162
cert_resolver: None,
163163
alpn_protocols: vec![],
@@ -230,18 +230,15 @@ impl rustls::client::ServerCertVerifier for Verifier {
230230
) -> Result<ServerCertVerified, rustls::Error> {
231231
let cb = self.callback;
232232
let dns_name: &str = match server_name {
233-
rustls::ServerName::DnsName(n) => n.as_ref().into(),
233+
rustls::ServerName::DnsName(n) => n.as_ref(),
234234
_ => return Err(rustls::Error::General("unknown name type".to_string())),
235235
};
236236
let dns_name: rustls_str = match dns_name.try_into() {
237237
Ok(r) => r,
238238
Err(NulByte {}) => return Err(rustls::Error::General("NUL byte in SNI".to_string())),
239239
};
240240

241-
let intermediates: Vec<_> = intermediates
242-
.into_iter()
243-
.map(|cert| cert.as_ref())
244-
.collect();
241+
let intermediates: Vec<_> = intermediates.iter().map(|cert| cert.as_ref()).collect();
245242

246243
let intermediates = rustls_slice_slice_bytes {
247244
inner: &*intermediates,
@@ -250,7 +247,7 @@ impl rustls::client::ServerCertVerifier for Verifier {
250247
let params = rustls_verify_server_cert_params {
251248
end_entity_cert_der: end_entity.as_ref().into(),
252249
intermediate_certs_der: &intermediates,
253-
dns_name: dns_name.into(),
250+
dns_name,
254251
ocsp_response: ocsp_response.into(),
255252
};
256253
let userdata = userdata_get().map_err(|_| {
@@ -312,7 +309,7 @@ impl rustls_client_config_builder {
312309
None => return rustls_result::InvalidParameter,
313310
};
314311

315-
let verifier: Verifier = Verifier{callback: callback};
312+
let verifier: Verifier = Verifier{callback};
316313
config_builder.verifier = Arc::new(verifier);
317314
rustls_result::Ok
318315
}

src/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl rustls_connection {
248248
};
249249
match guard.try_drop() {
250250
Ok(()) => result,
251-
Err(_) => return rustls_result::Panic,
251+
Err(_) => rustls_result::Panic,
252252
}
253253
}
254254
}

src/error.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ impl rustls_result {
4747
#[no_mangle]
4848
pub extern "C" fn rustls_result_is_cert_error(result: rustls_result) -> bool {
4949
match result_to_error(&result) {
50-
Either::Error(e) => match e {
51-
Error::InvalidCertificateData(_) => true,
52-
Error::InvalidCertificateEncoding => true,
53-
Error::InvalidCertificateSignature => true,
54-
Error::InvalidCertificateSignatureType => true,
55-
Error::InvalidSct(_) => true,
56-
_ => false,
57-
},
50+
Either::Error(e) => matches!(
51+
e,
52+
Error::InvalidCertificateData(_)
53+
| Error::InvalidCertificateEncoding
54+
| Error::InvalidCertificateSignature
55+
| Error::InvalidCertificateSignatureType
56+
| Error::InvalidSct(_)
57+
),
5858
_ => false,
5959
}
6060
}
@@ -241,27 +241,27 @@ pub(crate) enum Either {
241241
Error(rustls::Error),
242242
}
243243

244-
impl Into<Either> for String {
245-
fn into(self) -> Either {
246-
Either::String(self)
244+
impl From<String> for Either {
245+
fn from(s: String) -> Either {
246+
Either::String(s)
247247
}
248248
}
249249

250-
impl Into<Either> for &str {
251-
fn into(self) -> Either {
252-
Either::String(self.to_string())
250+
impl From<&str> for Either {
251+
fn from(s: &str) -> Either {
252+
Either::String(s.to_string())
253253
}
254254
}
255255

256-
impl Into<Either> for webpki::Error {
257-
fn into(self) -> Either {
258-
Either::String(self.to_string())
256+
impl From<webpki::Error> for Either {
257+
fn from(e: webpki::Error) -> Either {
258+
Either::String(e.to_string())
259259
}
260260
}
261261

262-
impl Into<Either> for rustls::Error {
263-
fn into(self) -> Either {
264-
Either::Error(self)
262+
impl From<rustls::Error> for Either {
263+
fn from(e: rustls::Error) -> Either {
264+
Either::Error(e)
265265
}
266266
}
267267

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![crate_type = "staticlib"]
22
#![allow(non_camel_case_types)]
3+
#![allow(clippy::not_unsafe_ptr_arg_deref)]
34
use crate::rslice::rustls_str;
45
use libc::{c_void, size_t};
56
use std::cell::RefCell;
67
use std::mem;
78
use std::sync::Arc;
8-
use std::thread::AccessError;
99

1010
pub mod cipher;
1111
pub mod client;
@@ -99,7 +99,7 @@ impl UserdataGuard {
9999
},
100100
)
101101
})
102-
.unwrap_or_else(|_: AccessError| Err(UserdataError::AccessError))
102+
.unwrap_or(Err(UserdataError::AccessError))
103103
}
104104
}
105105

@@ -410,14 +410,14 @@ where
410410
unsafe { F::cast_mut_ptr(from).as_mut() }
411411
}
412412

413-
pub(crate) fn try_box_from<'a, F, T>(from: *mut F) -> Option<Box<T>>
413+
pub(crate) fn try_box_from<F, T>(from: *mut F) -> Option<Box<T>>
414414
where
415415
F: BoxCastPtr<RustType = T>,
416416
{
417417
F::to_box(from)
418418
}
419419

420-
pub(crate) fn try_arc_from<'a, F, T>(from: *const F) -> Option<Arc<T>>
420+
pub(crate) fn try_arc_from<F, T>(from: *const F) -> Option<Arc<T>>
421421
where
422422
F: ArcCastPtr<RustType = T>,
423423
{
@@ -496,7 +496,7 @@ macro_rules! try_callback {
496496
/// not need to be freed.
497497
#[no_mangle]
498498
pub extern "C" fn rustls_version() -> rustls_str<'static> {
499-
return rustls_str::from_str_unchecked(RUSTLS_FFI_VERSION);
499+
rustls_str::from_str_unchecked(RUSTLS_FFI_VERSION)
500500
}
501501

502502
#[test]

src/rslice.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ pub extern "C" fn rustls_slice_slice_bytes_len(input: *const rustls_slice_slice_
7777
/// pointer is NULL, or n is greater than the length of the
7878
/// rustls_slice_slice_bytes, returns rustls_slice_bytes{NULL, 0}.
7979
#[no_mangle]
80-
pub extern "C" fn rustls_slice_slice_bytes_get<'a>(
81-
input: *const rustls_slice_slice_bytes<'a>,
80+
pub extern "C" fn rustls_slice_slice_bytes_get(
81+
input: *const rustls_slice_slice_bytes,
8282
n: size_t,
83-
) -> rustls_slice_bytes<'a> {
83+
) -> rustls_slice_bytes {
8484
let input: &rustls_slice_slice_bytes = unsafe {
8585
match input.as_ref() {
8686
Some(c) => c,

src/server.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ impl rustls_server_config_builder {
121121
ffi_panic_boundary! {
122122
let cipher_suites: &[*const rustls_supported_ciphersuite] = try_slice!(cipher_suites, cipher_suites_len);
123123
let mut cs_vec: Vec<SupportedCipherSuite> = Vec::new();
124-
for &cs in cipher_suites.into_iter() {
124+
for &cs in cipher_suites.iter() {
125125
let cs = try_ref_from_ptr!(cs);
126126
match ALL_CIPHER_SUITES.iter().find(|&acs| cs.eq(acs)) {
127-
Some(scs) => cs_vec.push(scs.clone()),
127+
Some(scs) => cs_vec.push(*scs),
128128
None => return InvalidParameter,
129129
}
130130
}
@@ -387,7 +387,7 @@ pub extern "C" fn rustls_server_connection_get_sni_hostname(
387387
if len > write_buf.len() {
388388
return rustls_result::InsufficientSize;
389389
}
390-
write_buf[..len].copy_from_slice(&sni_hostname.as_bytes());
390+
write_buf[..len].copy_from_slice(sni_hostname.as_bytes());
391391
*out_n = len;
392392
rustls_result::Ok
393393
}
@@ -497,7 +497,7 @@ impl ResolvesServerCert for ClientHelloResolver {
497497
fn resolve(&self, client_hello: ClientHello) -> Option<Arc<CertifiedKey>> {
498498
let sni_name: &str = {
499499
match client_hello.server_name() {
500-
Some(c) => c.into(),
500+
Some(c) => c,
501501
None => "",
502502
}
503503
};

src/session.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl SessionStoreBroker {
107107
) {
108108
rustls_result::Ok => {
109109
data.set_len(out_n);
110-
return Some(data);
110+
Some(data)
111111
}
112112
_ => None,
113113
}
@@ -122,12 +122,7 @@ impl SessionStoreBroker {
122122
Ok(u) => u,
123123
Err(_) => return false,
124124
};
125-
unsafe {
126-
match cb(userdata, &key, &value) {
127-
rustls_result::Ok => true,
128-
_ => false,
129-
}
130-
}
125+
unsafe { matches!(cb(userdata, &key, &value), rustls_result::Ok) }
131126
}
132127
}
133128

@@ -137,11 +132,11 @@ impl rustls::server::StoresServerSessions for SessionStoreBroker {
137132
}
138133

139134
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
140-
return self.retrieve(key, false);
135+
self.retrieve(key, false)
141136
}
142137

143138
fn take(&self, key: &[u8]) -> Option<Vec<u8>> {
144-
return self.retrieve(key, true);
139+
self.retrieve(key, true)
145140
}
146141

147142
fn can_cache(&self) -> bool {
@@ -155,7 +150,7 @@ impl rustls::client::StoresClientSessions for SessionStoreBroker {
155150
}
156151

157152
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
158-
return self.retrieve(key, false);
153+
self.retrieve(key, false)
159154
}
160155
}
161156

0 commit comments

Comments
 (0)