Skip to content

Commit 8aafd69

Browse files
committed
lints: enable and address clippy::manual_let_else
1 parent 9c91ec0 commit 8aafd69

5 files changed

Lines changed: 59 additions & 106 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ trivial_numeric_casts = "warn"
2424
unused_import_braces = "warn"
2525
unused_extern_crates = "warn"
2626
unused_qualifications = "warn"
27+
28+
[lints.clippy]
29+
manual_let_else = "warn"

src/callbacks.rs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ impl AlpnCallbackConfig {
6464
///
6565
/// Returns the selected ALPN, or None, or an error.
6666
pub fn invoke(&self, offer: &[u8]) -> Result<Option<Vec<u8>>, Error> {
67-
let callback = match self.cb {
68-
Some(callback) => callback,
69-
None => {
70-
return Ok(None);
71-
}
67+
let Some(callback) = self.cb else {
68+
return Ok(None);
7269
};
7370

7471
let ssl = SslCallbackContext::ssl_ptr();
@@ -115,11 +112,8 @@ pub struct CertCallbackConfig {
115112

116113
impl CertCallbackConfig {
117114
pub fn invoke(&self) -> Result<(), Error> {
118-
let callback = match self.cb {
119-
Some(callback) => callback,
120-
None => {
121-
return Ok(());
122-
}
115+
let Some(callback) = self.cb else {
116+
return Ok(());
123117
};
124118
let ssl = SslCallbackContext::ssl_ptr();
125119

@@ -150,11 +144,8 @@ pub struct ServerNameCallbackConfig {
150144

151145
impl ServerNameCallbackConfig {
152146
pub fn invoke(&self) -> Result<(), Error> {
153-
let callback = match self.cb {
154-
Some(callback) => callback,
155-
None => {
156-
return Ok(());
157-
}
147+
let Some(callback) = self.cb else {
148+
return Ok(());
158149
};
159150

160151
let ssl = SslCallbackContext::ssl_ptr();
@@ -192,11 +183,8 @@ pub fn invoke_session_new_callback(
192183
callback: SSL_CTX_new_session_cb,
193184
sess: Arc<NotThreadSafe<SSL_SESSION>>,
194185
) -> bool {
195-
let callback = match callback {
196-
Some(callback) => callback,
197-
None => {
198-
return false;
199-
}
186+
let Some(callback) = callback else {
187+
return false;
200188
};
201189

202190
let ssl = SslCallbackContext::ssl_ptr();
@@ -215,12 +203,7 @@ pub fn invoke_session_get_callback(
215203
callback: SSL_CTX_sess_get_cb,
216204
id: &[u8],
217205
) -> Option<Arc<NotThreadSafe<SSL_SESSION>>> {
218-
let callback = match callback {
219-
Some(callback) => callback,
220-
None => {
221-
return None;
222-
}
223-
};
206+
let callback = callback?;
224207

225208
let ssl_ptr = SslCallbackContext::ssl_ptr();
226209
let mut copy = 1;
@@ -244,11 +227,8 @@ pub fn invoke_session_remove_callback(
244227
ssl_ctx: *mut SSL_CTX,
245228
sess: Arc<NotThreadSafe<SSL_SESSION>>,
246229
) {
247-
let callback = match callback {
248-
Some(callback) => callback,
249-
None => {
250-
return;
251-
}
230+
let Some(callback) = callback else {
231+
return;
252232
};
253233

254234
let sess_ptr = Arc::into_raw(sess) as *mut SSL_SESSION;

src/conf.rs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ impl SslConfigCtx {
2626
}
2727

2828
pub(super) fn cmd(&mut self, cmd: &str, value: Option<&str>) -> c_int {
29-
let command = match self.supported_command(cmd) {
30-
Some(command) => command,
31-
None => {
32-
return -2; // "A return value of -2 means option is not recognised."
33-
}
29+
let Some(command) = self.supported_command(cmd) else {
30+
return -2; // "A return value of -2 means option is not recognised."
3431
};
3532

3633
match (command.action)(self, value) {
@@ -85,9 +82,8 @@ impl SslConfigCtx {
8582
}
8683

8784
fn min_protocol(&mut self, proto: Option<&str>) -> Result<ActionResult, Error> {
88-
let ver = match Self::parse_protocol_version(proto) {
89-
Some(ver) => ver,
90-
None => return Err(Error::bad_data("unrecognized protocol version")),
85+
let Some(ver) = Self::parse_protocol_version(proto) else {
86+
return Err(Error::bad_data("unrecognized protocol version"));
9187
};
9288

9389
Ok(match &self.state {
@@ -105,9 +101,8 @@ impl SslConfigCtx {
105101
}
106102

107103
fn max_protocol(&mut self, proto: Option<&str>) -> Result<ActionResult, Error> {
108-
let ver = match Self::parse_protocol_version(proto) {
109-
Some(ver) => ver,
110-
None => return Err(Error::bad_data("unrecognized protocol version")),
104+
let Some(ver) = Self::parse_protocol_version(proto) else {
105+
return Err(Error::bad_data("unrecognized protocol version"));
111106
};
112107

113108
Ok(match &self.state {
@@ -132,9 +127,8 @@ impl SslConfigCtx {
132127
State::ApplyingToCtx(ctx) => ctx.get().verify_mode,
133128
};
134129

135-
let raw_mode = match raw_mode {
136-
Some(raw_mode) => raw_mode,
137-
None => return Ok(ActionResult::ValueRequired),
130+
let Some(raw_mode) = raw_mode else {
131+
return Ok(ActionResult::ValueRequired);
138132
};
139133

140134
if !self.flags.is_server() && !self.flags.is_client() {
@@ -176,9 +170,8 @@ impl SslConfigCtx {
176170
}
177171

178172
fn certificate(&mut self, path: Option<&str>) -> Result<ActionResult, Error> {
179-
let path = match path {
180-
Some(path) => path,
181-
None => return Ok(ActionResult::ValueRequired),
173+
let Some(path) = path else {
174+
return Ok(ActionResult::ValueRequired);
182175
};
183176
let cert_chain = use_cert_chain_file(path)?;
184177

@@ -201,9 +194,8 @@ impl SslConfigCtx {
201194
}
202195

203196
fn private_key(&mut self, path: Option<&str>) -> Result<ActionResult, Error> {
204-
let path = match path {
205-
Some(path) => path,
206-
None => return Ok(ActionResult::ValueRequired),
197+
let Some(path) = path else {
198+
return Ok(ActionResult::ValueRequired);
207199
};
208200
let key = use_private_key_file(path, FILETYPE_PEM)?;
209201

@@ -221,9 +213,8 @@ impl SslConfigCtx {
221213
}
222214

223215
fn verify_ca_path(&mut self, path: Option<&str>) -> Result<ActionResult, Error> {
224-
let path = match path {
225-
Some(path) => path,
226-
None => return Ok(ActionResult::ValueRequired),
216+
let Some(path) = path else {
217+
return Ok(ActionResult::ValueRequired);
227218
};
228219

229220
match &self.state {
@@ -242,9 +233,8 @@ impl SslConfigCtx {
242233
}
243234

244235
fn verify_ca_file(&mut self, path: Option<&str>) -> Result<ActionResult, Error> {
245-
let path = match path {
246-
Some(path) => path,
247-
None => return Ok(ActionResult::ValueRequired),
236+
let Some(path) = path else {
237+
return Ok(ActionResult::ValueRequired);
248238
};
249239

250240
match &self.state {
@@ -280,9 +270,8 @@ impl SslConfigCtx {
280270
}
281271

282272
fn options(&mut self, opts: Option<&str>) -> Result<ActionResult, Error> {
283-
let opts = match opts {
284-
Some(path) => path,
285-
None => return Ok(ActionResult::ValueRequired),
273+
let Some(opts) = opts else {
274+
return Ok(ActionResult::ValueRequired);
286275
};
287276

288277
for part in opts.split(',').map(|part| part.trim()) {

src/entry.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,10 @@ entry! {
426426
let ctx = try_clone_arc!(ctx);
427427
let slice = try_slice!(protos, c_uint_into_usize(protos_len));
428428

429-
let alpn = match crate::parse_alpn(slice) {
430-
Some(alpn) => alpn,
431-
None => {
432-
// nb. openssl doesn't add anything to the error stack
433-
// in this case.
434-
return Error::bad_data("invalid alpn protocols").raise().into();
435-
}
429+
let Some(alpn) = crate::parse_alpn(slice) else {
430+
// nb. openssl doesn't add anything to the error stack
431+
// in this case.
432+
return Error::bad_data("invalid alpn protocols").raise().into();
436433
};
437434

438435
ctx.get_mut().set_alpn_offer(alpn);
@@ -766,18 +763,14 @@ entry! {
766763
pub fn _SSL_new(ctx: *mut SSL_CTX) -> *mut SSL {
767764
let ctx = try_clone_arc!(ctx);
768765

769-
let ssl = match crate::Ssl::new(ctx.clone(), ctx.get()).ok() {
770-
Some(ssl) => ssl,
771-
None => return ptr::null_mut(),
766+
let Some(ssl) = crate::Ssl::new(ctx.clone(), ctx.get()).ok() else {
767+
return ptr::null_mut();
772768
};
773769

774770
let out = to_arc_mut_ptr(NotThreadSafe::new(ssl));
775-
let ex_data = match ExData::new_ssl(out) {
776-
None => {
777-
_SSL_free(out);
778-
return ptr::null_mut();
779-
}
780-
Some(ex_data) => ex_data,
771+
let Some(ex_data) = ExData::new_ssl(out) else {
772+
_SSL_free(out);
773+
return ptr::null_mut();
781774
};
782775

783776
// safety: we just made this object, the pointer must be valid.
@@ -939,13 +932,10 @@ entry! {
939932
let ssl = try_clone_arc!(ssl);
940933
let slice = try_slice!(protos, c_uint_into_usize(protos_len));
941934

942-
let alpn = match crate::parse_alpn(slice) {
943-
Some(alpn) => alpn,
944-
None => {
945-
// nb. openssl doesn't add anything to the error stack
946-
// in this case.
947-
return Error::bad_data("invalid alpn protocols").raise().into();
948-
}
935+
let Some(alpn) = crate::parse_alpn(slice) else {
936+
// nb. openssl doesn't add anything to the error stack
937+
// in this case.
938+
return Error::bad_data("invalid alpn protocols").raise().into();
949939
};
950940

951941
ssl.get_mut().set_alpn_offer(alpn);
@@ -1770,11 +1760,8 @@ entry! {
17701760
let ptr = unsafe { ptr::read(pp) };
17711761
let slice = try_slice!(ptr, length);
17721762

1773-
let (sess, rest) = match SSL_SESSION::decode(slice) {
1774-
Some(r) => r,
1775-
None => {
1776-
return Error::bad_data("cannot decode SSL_SESSION").raise().into();
1777-
}
1763+
let Some((sess, rest)) = SSL_SESSION::decode(slice) else {
1764+
return Error::bad_data("cannot decode SSL_SESSION").raise().into();
17781765
};
17791766
let consumed_bytes = slice.len() - rest.len();
17801767

src/lib.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,9 +1086,8 @@ impl Ssl {
10861086

10871087
fn invoke_accepted_callbacks(&mut self) -> Result<(), error::Error> {
10881088
// called on transition from `Accepting` -> `Accepted`
1089-
let accepted = match &self.conn {
1090-
ConnState::Accepted(accepted) => accepted,
1091-
_ => unreachable!(),
1089+
let ConnState::Accepted(accepted) = &self.conn else {
1090+
unreachable!();
10921091
};
10931092

10941093
self.server_name = accepted
@@ -1160,9 +1159,8 @@ impl Ssl {
11601159
let cache = self.ctx.get_mut().caches.get_server();
11611160
config.session_storage = cache.clone();
11621161

1163-
let accepted = match mem::replace(&mut self.conn, ConnState::Nothing) {
1164-
ConnState::Accepted(accepted) => accepted,
1165-
_ => unreachable!(),
1162+
let ConnState::Accepted(accepted) = mem::replace(&mut self.conn, ConnState::Nothing) else {
1163+
unreachable!();
11661164
};
11671165

11681166
// TODO: send alert
@@ -1241,9 +1239,8 @@ impl Ssl {
12411239
}
12421240

12431241
fn try_io(&mut self) -> Result<(), error::Error> {
1244-
let bio = match self.bio.as_mut() {
1245-
Some(bio) => bio,
1246-
None => return Ok(()), // investigate OpenSSL behaviour without a BIO
1242+
let Some(bio) = self.bio.as_mut() else {
1243+
return Ok(()); // investigate OpenSSL behaviour without a BIO
12471244
};
12481245

12491246
match &mut self.conn {
@@ -1338,23 +1335,20 @@ impl Ssl {
13381335
}
13391336

13401337
fn init_peer_cert(&mut self) {
1341-
let conn = match self.conn() {
1342-
Some(conn) => conn,
1343-
None => return,
1338+
let Some(conn) = self.conn() else {
1339+
return;
13441340
};
13451341

1346-
let certs = match conn.peer_certificates() {
1347-
Some(certs) => certs,
1348-
None => return,
1342+
let Some(certs) = conn.peer_certificates() else {
1343+
return;
13491344
};
13501345

13511346
let mut stack = x509::OwnedX509Stack::empty();
13521347
let mut peer_cert = None;
13531348

13541349
for (i, cert) in certs.iter().enumerate() {
1355-
let converted = match x509::OwnedX509::parse_der(cert.as_ref()) {
1356-
Some(converted) => converted,
1357-
None => return,
1350+
let Some(converted) = x509::OwnedX509::parse_der(cert.as_ref()) else {
1351+
return;
13581352
};
13591353

13601354
if i == 0 {

0 commit comments

Comments
 (0)