Simplify some Option / Result / ? operator patterns (#8653)

* Simplify some Option / Result / ? operator patterns

When those match a combinator exactly.

Tool-aided by [comby-rust](https://github.com/huitseeker/comby-rust).

* adjust after review

* adjust post-review
This commit is contained in:
François Garillot
2021-04-23 09:36:10 -04:00
committed by GitHub
parent 541692c4a8
commit 052be8bbef
37 changed files with 77 additions and 188 deletions
+1 -4
View File
@@ -163,10 +163,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
keep_alive: bool,
) -> Result<T::Balance, Error<T, I>> {
let details = match Asset::<T, I>::get(id) {
Some(details) => details,
None => return Err(Error::<T, I>::Unknown),
};
let details = Asset::<T, I>::get(id).ok_or_else(|| Error::<T, I>::Unknown)?;
ensure!(!details.is_frozen, Error::<T, I>::Frozen);
let account = Account::<T, I>::get(id, who);
+1 -4
View File
@@ -490,10 +490,7 @@ mod tests {
let pre_runtime_digests = header.digest.logs.iter().filter_map(|d| d.as_pre_runtime());
let seals = header.digest.logs.iter().filter_map(|d| d.as_seal());
let author = match AuthorGiven::find_author(pre_runtime_digests) {
None => return Err("no author"),
Some(author) => author,
};
let author = AuthorGiven::find_author(pre_runtime_digests).ok_or_else(|| "no author")?;
for (id, seal) in seals {
if id == TEST_ID {
+1 -4
View File
@@ -127,10 +127,7 @@ where
}
let amount = token.calculate_amount(metadata);
let new_value = match self.gas_left.checked_sub(amount) {
None => None,
Some(val) => Some(val),
};
let new_value = self.gas_left.checked_sub(amount);
// We always consume the gas even if there is not enough gas.
self.gas_left = new_value.unwrap_or_else(Zero::zero);
+1 -5
View File
@@ -26,16 +26,12 @@ pub fn migrate<T: Config>() -> Weight {
Some(version) if version == PalletVersion::new(3, 0, 0) => {
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
let _ = <CurrentSchedule<T>>::translate::<u32, _>(|version| {
if let Some(version) = version {
Some(Schedule {
version.map(|version| Schedule {
version: version.saturating_add(1),
// Default limits were not decreased. Therefore it is OK to overwrite
// the schedule with the new defaults.
.. Default::default()
})
} else {
None
}
});
}
_ => (),
+1 -4
View File
@@ -407,10 +407,7 @@ where
);
// Check what happened after enaction of the verdict.
let alive_contract_info = match new_contract_info.map_err(|_| IsTombstone)? {
None => return Err(IsTombstone),
Some(contract) => contract,
};
let alive_contract_info = new_contract_info.map_err(|_| IsTombstone)?.ok_or_else(|| IsTombstone)?;
// Compute how much would the fee per block be with the *updated* balance.
let total_balance = T::Currency::total_balance(account);
+3 -12
View File
@@ -1653,10 +1653,7 @@ impl<T: Config> Module<T> {
// To decode the enum variant we only need the first byte.
let mut buf = [0u8; 1];
let key = <Preimages<T>>::hashed_key_for(proposal_hash);
let bytes = match sp_io::storage::read(&key, &mut buf, 0) {
Some(bytes) => bytes,
None => return Err(Error::<T>::NotImminent.into()),
};
let bytes = sp_io::storage::read(&key, &mut buf, 0).ok_or_else(|| Error::<T>::NotImminent)?;
// The value may be smaller that 1 byte.
let mut input = &buf[0..buf.len().min(bytes as usize)];
@@ -1684,10 +1681,7 @@ impl<T: Config> Module<T> {
// * at most 5 bytes to decode a `Compact<u32>`
let mut buf = [0u8; 6];
let key = <Preimages<T>>::hashed_key_for(proposal_hash);
let bytes = match sp_io::storage::read(&key, &mut buf, 0) {
Some(bytes) => bytes,
None => return Err(Error::<T>::PreimageMissing.into()),
};
let bytes = sp_io::storage::read(&key, &mut buf, 0).ok_or_else(|| Error::<T>::PreimageMissing)?;
// The value may be smaller that 6 bytes.
let mut input = &buf[0..buf.len().min(bytes as usize)];
@@ -1761,10 +1755,7 @@ impl<T: Config> Module<T> {
fn decode_compact_u32_at(key: &[u8]) -> Option<u32> {
// `Compact<u32>` takes at most 5 bytes.
let mut buf = [0u8; 5];
let bytes = match sp_io::storage::read(&key, &mut buf, 0) {
Some(bytes) => bytes,
None => return None,
};
let bytes = sp_io::storage::read(&key, &mut buf, 0)?;
// The value may be smaller than 5 bytes.
let mut input = &buf[0..buf.len().min(bytes as usize)];
match codec::Compact::<u32>::decode(&mut input) {
+2 -10
View File
@@ -518,21 +518,13 @@ impl<T: Config> Module<T> {
None
} else {
let session_index =
if let Some(session_id) = Self::session_for_set(set_id - 1) {
session_id
} else {
return Err(Error::<T>::InvalidEquivocationProof.into());
};
Self::session_for_set(set_id - 1).ok_or_else(|| Error::<T>::InvalidEquivocationProof)?;
Some(session_index)
};
let set_id_session_index =
if let Some(session_id) = Self::session_for_set(set_id) {
session_id
} else {
return Err(Error::<T>::InvalidEquivocationProof.into());
};
Self::session_for_set(set_id).ok_or_else(|| Error::<T>::InvalidEquivocationProof)?;
// check that the session id for the membership proof is within the
// bounds of the set id reported in the equivocation.
+2 -5
View File
@@ -2736,11 +2736,8 @@ impl<T: Config> Convert<T::AccountId, Option<Exposure<T::AccountId, BalanceOf<T>
for ExposureOf<T>
{
fn convert(validator: T::AccountId) -> Option<Exposure<T::AccountId, BalanceOf<T>>> {
if let Some(active_era) = <Module<T>>::active_era() {
Some(<Module<T>>::eras_stakers(active_era.index, &validator))
} else {
None
}
<Module<T>>::active_era()
.map(|active_era| <Module<T>>::eras_stakers(active_era.index, &validator))
}
}
@@ -77,10 +77,8 @@ impl<T: FullCodec, G: StorageValue<T>> storage::StorageValue<T> for G {
let key = Self::storage_value_final_key();
// attempt to get the length directly.
let maybe_old = match unhashed::get_raw(&key) {
Some(old_data) => Some(O::decode(&mut &old_data[..]).map_err(|_| ())?),
None => None,
};
let maybe_old = unhashed::get_raw(&key)
.map(|old_data| O::decode(&mut &old_data[..]).map_err(|_| ())).transpose()?;
let maybe_new = f(maybe_old);
if let Some(new) = maybe_new.as_ref() {
new.using_encoded(|d| unhashed::put_raw(&key, d));