Skip to content

Commit 65fca4d

Browse files
committed
refactor: Apply 2024 edition clippy improvements (guard clauses)
- Collapse nested if-let statements using 2024 edition guard clause syntax - Convert 38 instances from nested if-let to single if-let with && conditions - Improves code readability and embraces 2024 edition improvements Files modified: - crates/bssh-russh/src/client/mod.rs (7 fixes) - crates/bssh-russh/src/server/session.rs (12 fixes) - crates/bssh-russh/src/client/session.rs (9 fixes) - crates/bssh-russh/src/server/mod.rs (2 fixes) - crates/bssh-russh/src/client/encrypted.rs (3 fixes) - crates/bssh-russh/src/server/encrypted.rs (2 fixes) - crates/bssh-russh/src/ssh_read.rs (1 fix) - crates/bssh-russh/src/keys/known_hosts.rs (1 fix) - crates/bssh-russh/src/kex/dh/mod.rs (1 fix) Example transformation: // Before if let Some(x) = y { if condition { // ... } } // After (2024 edition) if let Some(x) = y && condition { // ... }
1 parent 73b7857 commit 65fca4d

9 files changed

Lines changed: 76 additions & 114 deletions

File tree

crates/bssh-russh/src/client/encrypted.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -437,15 +437,14 @@ impl Session {
437437
let channel_num = map_err!(ChannelId::decode(&mut r))?;
438438
let data = map_err!(Bytes::decode(&mut r))?;
439439
let target = self.common.config.window_size;
440-
if let Some(ref mut enc) = self.common.encrypted {
441-
if enc.adjust_window_size(channel_num, &data, target)? {
440+
if let Some(ref mut enc) = self.common.encrypted
441+
&& enc.adjust_window_size(channel_num, &data, target)? {
442442
let next_window =
443443
client.adjust_window(channel_num, self.target_window_size);
444444
if next_window > 0 {
445445
self.target_window_size = next_window
446446
}
447447
}
448-
}
449448

450449
if let Some(chan) = self.channels.get(&channel_num) {
451450
let _ = chan.send(ChannelMsg::Data { data: data.clone() }).await;
@@ -459,15 +458,14 @@ impl Session {
459458
let extended_code = map_err!(u32::decode(&mut r))?;
460459
let data = map_err!(Bytes::decode(&mut r))?;
461460
let target = self.common.config.window_size;
462-
if let Some(ref mut enc) = self.common.encrypted {
463-
if enc.adjust_window_size(channel_num, &data, target)? {
461+
if let Some(ref mut enc) = self.common.encrypted
462+
&& enc.adjust_window_size(channel_num, &data, target)? {
464463
let next_window =
465464
client.adjust_window(channel_num, self.target_window_size);
466465
if next_window > 0 {
467466
self.target_window_size = next_window
468467
}
469468
}
470-
}
471469

472470
if let Some(chan) = self.channels.get(&channel_num) {
473471
let _ = chan
@@ -551,8 +549,8 @@ impl Session {
551549
}
552550
_ => {
553551
let wants_reply = map_err!(u8::decode(&mut r))?;
554-
if wants_reply == 1 {
555-
if let Some(ref mut enc) = self.common.encrypted {
552+
if wants_reply == 1
553+
&& let Some(ref mut enc) = self.common.encrypted {
556554
self.common.wants_reply = false;
557555
if let Some(ch) = enc.channels.get(&channel_num) {
558556
push_packet!(enc.write, {
@@ -561,7 +559,6 @@ impl Session {
561559
})
562560
}
563561
}
564-
}
565562
info!("Unknown channel request {req:?} {wants_reply:?}",);
566563
Ok(())
567564
}

crates/bssh-russh/src/client/mod.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -966,11 +966,10 @@ pub async fn connect<H: Handler + Send + 'static, A: tokio::net::ToSocketAddrs>(
966966
handler: H,
967967
) -> Result<Handle<H>, H::Error> {
968968
let socket = map_err!(tokio::net::TcpStream::connect(addrs).await)?;
969-
if config.as_ref().nodelay {
970-
if let Err(e) = socket.set_nodelay(true) {
969+
if config.as_ref().nodelay
970+
&& let Err(e) = socket.set_nodelay(true) {
971971
warn!("set_nodelay() failed: {e:?}");
972972
}
973-
}
974973

975974
connect_stream(config, socket, handler).await
976975
}
@@ -1211,8 +1210,8 @@ impl Session {
12111210
reading.set(start_reading(stream_read, buffer, opening_cipher));
12121211
}
12131212
() = &mut keepalive_timer => {
1214-
if let Some(ref mut enc) = self.common.encrypted {
1215-
if matches!(enc.state, EncryptedState::Authenticated) {
1213+
if let Some(ref mut enc) = self.common.encrypted
1214+
&& matches!(enc.state, EncryptedState::Authenticated) {
12161215
self.common.alive_timeouts = self.common.alive_timeouts.saturating_add(1);
12171216
if self.common.config.keepalive_max != 0 && self.common.alive_timeouts > self.common.config.keepalive_max {
12181217
debug!("Timeout, server not responding to keepalives");
@@ -1221,7 +1220,6 @@ impl Session {
12211220
sent_keepalive = true;
12221221
self.send_keepalive(true)?;
12231222
}
1224-
}
12251223
}
12261224
() = &mut inactivity_timer => {
12271225
debug!("timeout");
@@ -1263,15 +1261,14 @@ impl Session {
12631261
self.flush()?;
12641262
map_err!(self.common.packet_writer.flush_into(stream_write).await)?;
12651263

1266-
if let Some(ref mut enc) = self.common.encrypted {
1267-
if let EncryptedState::InitCompression = enc.state {
1264+
if let Some(ref mut enc) = self.common.encrypted
1265+
&& let EncryptedState::InitCompression = enc.state {
12681266
if enc.client_compression.is_deferred() {
12691267
enc.client_compression
12701268
.init_compress(self.common.packet_writer.compress());
12711269
}
12721270
enc.state = EncryptedState::Authenticated;
12731271
}
1274-
}
12751272

12761273
if self.common.received_data {
12771274
// Reset the number of failed keepalive attempts. We don't
@@ -1281,22 +1278,20 @@ impl Session {
12811278
// data from it.
12821279
self.common.alive_timeouts = 0;
12831280
}
1284-
if self.common.received_data || sent_keepalive {
1285-
if let (futures::future::Either::Right(ref mut sleep), Some(d)) = (
1281+
if (self.common.received_data || sent_keepalive)
1282+
&& let (futures::future::Either::Right(ref mut sleep), Some(d)) = (
12861283
keepalive_timer.as_mut().as_pin_mut(),
12871284
self.common.config.keepalive_interval,
12881285
) {
12891286
sleep.as_mut().reset(tokio::time::Instant::now() + d);
12901287
}
1291-
}
1292-
if !sent_keepalive {
1293-
if let (futures::future::Either::Right(ref mut sleep), Some(d)) = (
1288+
if !sent_keepalive
1289+
&& let (futures::future::Either::Right(ref mut sleep), Some(d)) = (
12941290
inactivity_timer.as_mut().as_pin_mut(),
12951291
self.common.config.inactivity_timeout,
12961292
) {
12971293
sleep.as_mut().reset(tokio::time::Instant::now() + d);
12981294
}
1299-
}
13001295
}
13011296

13021297
result
@@ -1528,15 +1523,14 @@ impl Session {
15281523
/// Flush the temporary cleartext buffer into the encryption
15291524
/// buffer. This does *not* flush to the socket.
15301525
fn flush(&mut self) -> Result<(), crate::Error> {
1531-
if let Some(ref mut enc) = self.common.encrypted {
1532-
if enc.flush(
1526+
if let Some(ref mut enc) = self.common.encrypted
1527+
&& enc.flush(
15331528
&self.common.config.as_ref().limits,
15341529
&mut self.common.packet_writer,
15351530
)? && !self.kex.active()
15361531
{
15371532
self.begin_rekey()?;
15381533
}
1539-
}
15401534
Ok(())
15411535
}
15421536

@@ -1581,8 +1575,8 @@ async fn reply<H: Handler>(
15811575

15821576
let is_kex_msg = pkt.buffer.first().cloned().map(is_kex_msg).unwrap_or(false);
15831577

1584-
if is_kex_msg {
1585-
if let SessionKexState::InProgress(kex) = session.kex.take() {
1578+
if is_kex_msg
1579+
&& let SessionKexState::InProgress(kex) = session.kex.take() {
15861580
let progress = kex.step(Some(pkt), &mut session.common.packet_writer)?;
15871581

15881582
match progress {
@@ -1652,7 +1646,6 @@ async fn reply<H: Handler>(
16521646

16531647
return Ok(());
16541648
}
1655-
}
16561649

16571650
session.client_read_encrypted(handler, pkt).await
16581651
}

crates/bssh-russh/src/client/session.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ impl Session {
111111
pix_height: u32,
112112
terminal_modes: &[(Pty, u32)],
113113
) -> Result<(), crate::Error> {
114-
if let Some(ref mut enc) = self.common.encrypted {
115-
if let Some(channel) = enc.channels.get(&channel) {
114+
if let Some(ref mut enc) = self.common.encrypted
115+
&& let Some(channel) = enc.channels.get(&channel) {
116116
push_packet!(enc.write, {
117117
map_err!(msg::CHANNEL_REQUEST.encode(&mut enc.write))?;
118118

@@ -137,7 +137,6 @@ impl Session {
137137
(Pty::TTY_OP_END as u8).encode(&mut enc.write)?;
138138
});
139139
}
140-
}
141140
Ok(())
142141
}
143142

@@ -150,8 +149,8 @@ impl Session {
150149
x11_authentication_cookie: &str,
151150
x11_screen_number: u32,
152151
) -> Result<(), crate::Error> {
153-
if let Some(ref mut enc) = self.common.encrypted {
154-
if let Some(channel) = enc.channels.get(&channel) {
152+
if let Some(ref mut enc) = self.common.encrypted
153+
&& let Some(channel) = enc.channels.get(&channel) {
155154
push_packet!(enc.write, {
156155
msg::CHANNEL_REQUEST.encode(&mut enc.write)?;
157156

@@ -164,7 +163,6 @@ impl Session {
164163
x11_screen_number.encode(&mut enc.write)?;
165164
});
166165
}
167-
}
168166
Ok(())
169167
}
170168

@@ -175,8 +173,8 @@ impl Session {
175173
variable_name: &str,
176174
variable_value: &str,
177175
) -> Result<(), crate::Error> {
178-
if let Some(ref mut enc) = self.common.encrypted {
179-
if let Some(channel) = enc.channels.get(&channel) {
176+
if let Some(ref mut enc) = self.common.encrypted
177+
&& let Some(channel) = enc.channels.get(&channel) {
180178
push_packet!(enc.write, {
181179
msg::CHANNEL_REQUEST.encode(&mut enc.write)?;
182180

@@ -187,7 +185,6 @@ impl Session {
187185
variable_value.encode(&mut enc.write)?;
188186
});
189187
}
190-
}
191188
Ok(())
192189
}
193190

@@ -196,8 +193,8 @@ impl Session {
196193
want_reply: bool,
197194
channel: ChannelId,
198195
) -> Result<(), crate::Error> {
199-
if let Some(ref mut enc) = self.common.encrypted {
200-
if let Some(channel) = enc.channels.get(&channel) {
196+
if let Some(ref mut enc) = self.common.encrypted
197+
&& let Some(channel) = enc.channels.get(&channel) {
201198
push_packet!(enc.write, {
202199
msg::CHANNEL_REQUEST.encode(&mut enc.write)?;
203200

@@ -206,7 +203,6 @@ impl Session {
206203
(want_reply as u8).encode(&mut enc.write)?;
207204
});
208205
}
209-
}
210206
Ok(())
211207
}
212208

@@ -216,8 +212,8 @@ impl Session {
216212
want_reply: bool,
217213
command: &[u8],
218214
) -> Result<(), crate::Error> {
219-
if let Some(ref mut enc) = self.common.encrypted {
220-
if let Some(channel) = enc.channels.get(&channel) {
215+
if let Some(ref mut enc) = self.common.encrypted
216+
&& let Some(channel) = enc.channels.get(&channel) {
221217
push_packet!(enc.write, {
222218
msg::CHANNEL_REQUEST.encode(&mut enc.write)?;
223219

@@ -228,14 +224,13 @@ impl Session {
228224
});
229225
return Ok(());
230226
}
231-
}
232227
error!("exec");
233228
Ok(())
234229
}
235230

236231
pub fn signal(&mut self, channel: ChannelId, signal: Sig) -> Result<(), crate::Error> {
237-
if let Some(ref mut enc) = self.common.encrypted {
238-
if let Some(channel) = enc.channels.get(&channel) {
232+
if let Some(ref mut enc) = self.common.encrypted
233+
&& let Some(channel) = enc.channels.get(&channel) {
239234
push_packet!(enc.write, {
240235
msg::CHANNEL_REQUEST.encode(&mut enc.write)?;
241236
channel.recipient_channel.encode(&mut enc.write)?;
@@ -244,7 +239,6 @@ impl Session {
244239
signal.name().encode(&mut enc.write)?;
245240
});
246241
}
247-
}
248242
Ok(())
249243
}
250244

@@ -254,8 +248,8 @@ impl Session {
254248
channel: ChannelId,
255249
name: &str,
256250
) -> Result<(), crate::Error> {
257-
if let Some(ref mut enc) = self.common.encrypted {
258-
if let Some(channel) = enc.channels.get(&channel) {
251+
if let Some(ref mut enc) = self.common.encrypted
252+
&& let Some(channel) = enc.channels.get(&channel) {
259253
push_packet!(enc.write, {
260254
msg::CHANNEL_REQUEST.encode(&mut enc.write)?;
261255

@@ -265,7 +259,6 @@ impl Session {
265259
name.encode(&mut enc.write)?;
266260
});
267261
}
268-
}
269262
Ok(())
270263
}
271264

@@ -277,8 +270,8 @@ impl Session {
277270
pix_width: u32,
278271
pix_height: u32,
279272
) -> Result<(), crate::Error> {
280-
if let Some(ref mut enc) = self.common.encrypted {
281-
if let Some(channel) = enc.channels.get(&channel) {
273+
if let Some(ref mut enc) = self.common.encrypted
274+
&& let Some(channel) = enc.channels.get(&channel) {
282275
push_packet!(enc.write, {
283276
msg::CHANNEL_REQUEST.encode(&mut enc.write)?;
284277

@@ -291,7 +284,6 @@ impl Session {
291284
pix_height.encode(&mut enc.write)?;
292285
});
293286
}
294-
}
295287
Ok(())
296288
}
297289

@@ -484,16 +476,15 @@ impl Session {
484476
channel: ChannelId,
485477
want_reply: bool,
486478
) -> Result<(), crate::Error> {
487-
if let Some(ref mut enc) = self.common.encrypted {
488-
if let Some(channel) = enc.channels.get(&channel) {
479+
if let Some(ref mut enc) = self.common.encrypted
480+
&& let Some(channel) = enc.channels.get(&channel) {
489481
push_packet!(enc.write, {
490482
msg::CHANNEL_REQUEST.encode(&mut enc.write)?;
491483
channel.recipient_channel.encode(&mut enc.write)?;
492484
"auth-agent-req@openssh.com".encode(&mut enc.write)?;
493485
(want_reply as u8).encode(&mut enc.write)?;
494486
});
495487
}
496-
}
497488
Ok(())
498489
}
499490

crates/bssh-russh/src/kex/dh/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,10 @@ impl<D: Digest> std::fmt::Debug for DhGroupKex<D> {
121121
pub(crate) fn biguint_to_mpint(biguint: &BigUint) -> Vec<u8> {
122122
let mut mpint = Vec::new();
123123
let bytes = biguint.to_bytes_be();
124-
if let Some(b) = bytes.first() {
125-
if b > &0x7f {
124+
if let Some(b) = bytes.first()
125+
&& b > &0x7f {
126126
mpint.push(0);
127127
}
128-
}
129128
mpint.extend(&bytes);
130129
mpint
131130
}

crates/bssh-russh/src/keys/known_hosts.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,10 @@ fn match_hostname(host: &str, pattern: &str) -> bool {
116116
let Some(Ok(hash)) = parts.next().map(|p| BASE64_MIME.decode(p.as_bytes())) else {
117117
continue;
118118
};
119-
if let Ok(hmac) = Hmac::<Sha1>::new_from_slice(&salt) {
120-
if hmac.chain_update(host).verify_slice(&hash).is_ok() {
119+
if let Ok(hmac) = Hmac::<Sha1>::new_from_slice(&salt)
120+
&& hmac.chain_update(host).verify_slice(&hash).is_ok() {
121121
return true;
122122
}
123-
}
124123
} else if host == entry {
125124
return true;
126125
}

crates/bssh-russh/src/server/encrypted.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -632,14 +632,13 @@ impl Session {
632632
let data = map_err!(Bytes::decode(r))?;
633633
let target = self.target_window_size;
634634

635-
if let Some(ref mut enc) = self.common.encrypted {
636-
if enc.adjust_window_size(channel_num, &data, target)? {
635+
if let Some(ref mut enc) = self.common.encrypted
636+
&& enc.adjust_window_size(channel_num, &data, target)? {
637637
let window = handler.adjust_window(channel_num, self.target_window_size);
638638
if window > 0 {
639639
self.target_window_size = window
640640
}
641641
}
642-
}
643642
self.flush()?;
644643
if let Some(ext) = ext {
645644
if let Some(chan) = self.channels.get(&channel_num) {
@@ -731,11 +730,10 @@ impl Session {
731730
let channel_num = map_err!(ChannelId::decode(r))?;
732731
let req_type = map_err!(String::decode(r))?;
733732
let wants_reply = map_err!(u8::decode(r))?;
734-
if let Some(ref mut enc) = self.common.encrypted {
735-
if let Some(channel) = enc.channels.get_mut(&channel_num) {
733+
if let Some(ref mut enc) = self.common.encrypted
734+
&& let Some(channel) = enc.channels.get_mut(&channel_num) {
736735
channel.wants_reply = wants_reply != 0;
737736
}
738-
}
739737
match req_type.as_str() {
740738
"pty-req" => {
741739
let term = map_err!(String::decode(r))?;

0 commit comments

Comments
 (0)