From d4ddf8d7e812a759b3a471d8b0138ef6e1d39e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= <4142+huitseeker@users.noreply.github.com> Date: Sun, 25 Apr 2021 16:51:01 -0400 Subject: [PATCH] Simplify some Option / Result / ? operator patterns (#2920) * Simplify some Option / Result / ? operator patterns When they identically match a combinator on those types. Tool-aided by [comby-rust](https://github.com/huitseeker/comby-rust). * adjust review comments Co-authored-by: Shawn Tabrizi --- polkadot/bridges/modules/ethereum/src/validators.rs | 5 +---- .../node/core/approval-voting/src/approval_checking.rs | 5 +---- polkadot/node/network/approval-distribution/src/lib.rs | 3 +-- .../network/availability-distribution/src/tests/mod.rs | 3 +-- polkadot/node/network/availability-recovery/src/lib.rs | 5 +---- polkadot/node/network/statement-distribution/src/lib.rs | 7 +------ 6 files changed, 6 insertions(+), 22 deletions(-) diff --git a/polkadot/bridges/modules/ethereum/src/validators.rs b/polkadot/bridges/modules/ethereum/src/validators.rs index 7ec22a4439..df384cb696 100644 --- a/polkadot/bridges/modules/ethereum/src/validators.rs +++ b/polkadot/bridges/modules/ethereum/src/validators.rs @@ -188,10 +188,7 @@ impl<'a> Validators<'a> { finalized_blocks: &[(HeaderId, Option)], ) -> Option { // if we haven't finalized any blocks, no changes may be finalized - let newest_finalized_id = match finalized_blocks.last().map(|(id, _)| id) { - Some(last_finalized_id) => last_finalized_id, - None => return None, - }; + let newest_finalized_id = finalized_blocks.last().map(|(id, _)| id)?; let oldest_finalized_id = finalized_blocks .first() .map(|(id, _)| id) diff --git a/polkadot/node/core/approval-voting/src/approval_checking.rs b/polkadot/node/core/approval-voting/src/approval_checking.rs index 79ef03c7bf..f5b5f53533 100644 --- a/polkadot/node/core/approval-voting/src/approval_checking.rs +++ b/polkadot/node/core/approval-voting/src/approval_checking.rs @@ -317,10 +317,7 @@ pub fn tranches_to_approve( tranches_with_gaps_filled .scan(Some(initial_state), |state, (tranche, assignments)| { // The `Option` here is used for early exit. - let s = match state.take() { - None => return None, - Some(s) => s, - }; + let s = state.take()?; let clock_drift = s.clock_drift(no_show_duration); let drifted_tick_now = tick_now.saturating_sub(clock_drift); diff --git a/polkadot/node/network/approval-distribution/src/lib.rs b/polkadot/node/network/approval-distribution/src/lib.rs index 441ee502b0..7c69ac1363 100644 --- a/polkadot/node/network/approval-distribution/src/lib.rs +++ b/polkadot/node/network/approval-distribution/src/lib.rs @@ -463,8 +463,7 @@ impl State { if !range.is_empty() && !blocks.is_empty() { self.blocks_by_number .range(range) - .map(|(_number, hashes)| hashes) - .flatten() + .flat_map(|(_number, hashes)| hashes) .for_each(|hash| { if let Some(entry) = blocks.get_mut(hash) { entry.known_by.remove(&peer_id); diff --git a/polkadot/node/network/availability-distribution/src/tests/mod.rs b/polkadot/node/network/availability-distribution/src/tests/mod.rs index 77b47e31e7..b914a0cc0b 100644 --- a/polkadot/node/network/availability-distribution/src/tests/mod.rs +++ b/polkadot/node/network/availability-distribution/src/tests/mod.rs @@ -101,8 +101,7 @@ fn check_fetch_retry() { let valid_candidate_hashes: HashSet<_> = state.cores .get(&state.relay_chain[1]) .iter() - .map(|v| v.iter()) - .flatten() + .flat_map(|v| v.iter()) .filter_map(|c| { match c { CoreState::Occupied(core) => Some(core.candidate_hash), diff --git a/polkadot/node/network/availability-recovery/src/lib.rs b/polkadot/node/network/availability-recovery/src/lib.rs index 75cd274bba..f8eab8689a 100644 --- a/polkadot/node/network/availability-recovery/src/lib.rs +++ b/polkadot/node/network/availability-recovery/src/lib.rs @@ -142,10 +142,7 @@ impl RequestFromBackersPhase { ); loop { // Pop the next backer, and proceed to next phase if we're out. - let validator_index = match self.shuffled_backers.pop() { - None => return Err(RecoveryError::Unavailable), - Some(i) => i, - }; + let validator_index = self.shuffled_backers.pop().ok_or_else(|| RecoveryError::Unavailable)?; // Request data. let (req, res) = OutgoingRequest::new( diff --git a/polkadot/node/network/statement-distribution/src/lib.rs b/polkadot/node/network/statement-distribution/src/lib.rs index ab1068f2b7..97d527ebbb 100644 --- a/polkadot/node/network/statement-distribution/src/lib.rs +++ b/polkadot/node/network/statement-distribution/src/lib.rs @@ -1308,12 +1308,7 @@ async fn handle_incoming_message<'a>( ctx, req_sender, metrics, - ).await; - - let statement = match statement { - None => return None, - Some(statement) => statement, - }; + ).await?; match active_head.check_useful_or_unknown(statement.clone()) { Ok(()) => {},