From 39615bd075cc330177b0ac5bb056ce77a4a107b4 Mon Sep 17 00:00:00 2001 From: behzad nouri Date: Thu, 31 Aug 2023 22:35:47 +0000 Subject: [PATCH] removes manual let...else (#33089) https://rust-lang.github.io/rust-clippy/master/index.html#/manual_let_else --- gossip/src/cluster_info.rs | 48 +++++++++++++++++-------------------- gossip/src/crds.rs | 30 ++++++----------------- runtime/src/bank_forks.rs | 10 ++++---- runtime/src/vote_account.rs | 18 +++++++------- 4 files changed, 42 insertions(+), 64 deletions(-) diff --git a/gossip/src/cluster_info.rs b/gossip/src/cluster_info.rs index 4ebfb4bfc..d5b2b447e 100644 --- a/gossip/src/cluster_info.rs +++ b/gossip/src/cluster_info.rs @@ -1107,19 +1107,18 @@ impl ClusterInfo { self.time_gossip_read_lock("gossip_read_push_vote", &self.stats.push_vote_read); (0..MAX_LOCKOUT_HISTORY as u8).find(|ix| { let vote = CrdsValueLabel::Vote(*ix, self_pubkey); - if let Some(vote) = gossip_crds.get::<&CrdsData>(&vote) { - match &vote { - CrdsData::Vote(_, prev_vote) => match prev_vote.slot() { - Some(prev_vote_slot) => prev_vote_slot == vote_slot, - None => { - error!("crds vote with no slots!"); - false - } - }, - _ => panic!("this should not happen!"), + let Some(vote) = gossip_crds.get::<&CrdsData>(&vote) else { + return false; + }; + let CrdsData::Vote(_, prev_vote) = &vote else { + panic!("this should not happen!"); + }; + match prev_vote.slot() { + Some(prev_vote_slot) => prev_vote_slot == vote_slot, + None => { + error!("crds vote with no slots!"); + false } - } else { - false } }) }; @@ -1153,11 +1152,10 @@ impl ClusterInfo { .time_gossip_read_lock("get_votes", &self.stats.get_votes) .get_votes(cursor) .map(|vote| { - let transaction = match &vote.value.data { - CrdsData::Vote(_, vote) => vote.transaction().clone(), - _ => panic!("this should not happen!"), + let CrdsData::Vote(_, vote) = &vote.value.data else { + panic!("this should not happen!"); }; - transaction + vote.transaction().clone() }) .collect(); self.stats.get_votes_count.add_relaxed(txs.len() as u64); @@ -1173,11 +1171,11 @@ impl ClusterInfo { .time_gossip_read_lock("get_votes", &self.stats.get_votes) .get_votes(cursor) .map(|vote| { - let transaction = match &vote.value.data { - CrdsData::Vote(_, vote) => vote.transaction().clone(), - _ => panic!("this should not happen!"), + let label = vote.value.label(); + let CrdsData::Vote(_, vote) = &vote.value.data else { + panic!("this should not happen!"); }; - (vote.value.label(), transaction) + (label, vote.transaction().clone()) }) .unzip(); self.stats.get_votes_count.add_relaxed(txs.len() as u64); @@ -3898,12 +3896,10 @@ mod tests { let gossip_crds = cluster_info.gossip.crds.read().unwrap(); let mut vote_slots = HashSet::new(); for label in labels { - match &gossip_crds.get::<&CrdsData>(&label).unwrap() { - CrdsData::Vote(_, vote) => { - assert!(vote_slots.insert(vote.slot().unwrap())); - } - _ => panic!("this should not happen!"), - } + let CrdsData::Vote(_, vote) = &gossip_crds.get::<&CrdsData>(&label).unwrap() else { + panic!("this should not happen!"); + }; + assert!(vote_slots.insert(vote.slot().unwrap())); } vote_slots.into_iter().collect() }; diff --git a/gossip/src/crds.rs b/gossip/src/crds.rs index 2e7e9bc00..a89cc8b37 100644 --- a/gossip/src/crds.rs +++ b/gossip/src/crds.rs @@ -549,9 +549,8 @@ impl Crds { self.entries.remove(&value.ordinal); // Remove the index from records associated with the value's pubkey. let pubkey = value.value.pubkey(); - let mut records_entry = match self.records.entry(pubkey) { - hash_map::Entry::Vacant(_) => panic!("this should not happen!"), - hash_map::Entry::Occupied(entry) => entry, + let hash_map::Entry::Occupied(mut records_entry) = self.records.entry(pubkey) else { + panic!("this should not happen!"); }; records_entry.get_mut().swap_remove(&index); if records_entry.get().is_empty() { @@ -905,10 +904,7 @@ mod tests { let other = NodeInstance::new(&mut rng, pubkey, now + k); let other = other.with_wallclock(now - 1); let other = make_crds_value(other); - match crds.insert(other, now, GossipRoute::LocalMessage) { - Ok(()) => (), - _ => panic!(), - } + assert_matches!(crds.insert(other, now, GossipRoute::LocalMessage), Ok(())); } } @@ -1148,10 +1144,7 @@ mod tests { ); for value in crds.get_epoch_slots(&mut Cursor(since)) { assert!(value.ordinal >= since); - match value.value.data { - CrdsData::EpochSlots(_, _) => (), - _ => panic!("not an epoch-slot!"), - } + assert_matches!(value.value.data, CrdsData::EpochSlots(_, _)); } let num_votes = crds .table @@ -1174,10 +1167,7 @@ mod tests { ); for value in crds.get_votes(&mut Cursor(since)) { assert!(value.ordinal >= since); - match value.value.data { - CrdsData::Vote(_, _) => (), - _ => panic!("not a vote!"), - } + assert_matches!(value.value.data, CrdsData::Vote(_, _)); } let num_entries = crds .table @@ -1224,16 +1214,10 @@ mod tests { crds.get_epoch_slots(&mut Cursor::default()).count() ); for vote in crds.get_votes(&mut Cursor::default()) { - match vote.value.data { - CrdsData::Vote(_, _) => (), - _ => panic!("not a vote!"), - } + assert_matches!(vote.value.data, CrdsData::Vote(_, _)); } for epoch_slots in crds.get_epoch_slots(&mut Cursor::default()) { - match epoch_slots.value.data { - CrdsData::EpochSlots(_, _) => (), - _ => panic!("not an epoch-slot!"), - } + assert_matches!(epoch_slots.value.data, CrdsData::EpochSlots(_, _)); } (num_nodes, num_votes, num_epoch_slots) } diff --git a/runtime/src/bank_forks.rs b/runtime/src/bank_forks.rs index 8f59d5431..b56f4d774 100644 --- a/runtime/src/bank_forks.rs +++ b/runtime/src/bank_forks.rs @@ -209,18 +209,16 @@ impl BankForks { pub fn remove(&mut self, slot: Slot) -> Option> { let bank = self.banks.remove(&slot)?; for parent in bank.proper_ancestors() { - let mut entry = match self.descendants.entry(parent) { - Entry::Vacant(_) => panic!("this should not happen!"), - Entry::Occupied(entry) => entry, + let Entry::Occupied(mut entry) = self.descendants.entry(parent) else { + panic!("this should not happen!"); }; entry.get_mut().remove(&slot); if entry.get().is_empty() && !self.banks.contains_key(&parent) { entry.remove_entry(); } } - let entry = match self.descendants.entry(slot) { - Entry::Vacant(_) => panic!("this should not happen!"), - Entry::Occupied(entry) => entry, + let Entry::Occupied(entry) = self.descendants.entry(slot) else { + panic!("this should not happen!"); }; if entry.get().is_empty() { entry.remove_entry(); diff --git a/runtime/src/vote_account.rs b/runtime/src/vote_account.rs index 6a919dd63..efbddb40e 100644 --- a/runtime/src/vote_account.rs +++ b/runtime/src/vote_account.rs @@ -198,15 +198,15 @@ impl VoteAccounts { return; }; if let Some(node_pubkey) = vote_account.node_pubkey() { - match Arc::make_mut(staked_nodes).entry(node_pubkey) { - Entry::Vacant(_) => panic!("this should not happen!"), - Entry::Occupied(mut entry) => match entry.get().cmp(&stake) { - Ordering::Less => panic!("subtraction value exceeds node's stake"), - Ordering::Equal => { - entry.remove_entry(); - } - Ordering::Greater => *entry.get_mut() -= stake, - }, + let Entry::Occupied(mut entry) = Arc::make_mut(staked_nodes).entry(node_pubkey) else { + panic!("this should not happen!"); + }; + match entry.get().cmp(&stake) { + Ordering::Less => panic!("subtraction value exceeds node's stake"), + Ordering::Equal => { + entry.remove_entry(); + } + Ordering::Greater => *entry.get_mut() -= stake, } } }