From bde5ab3b54d3183241a900d20154525984f2206b Mon Sep 17 00:00:00 2001 From: nanocryk <6422796+nanocryk@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:08:26 +0100 Subject: [PATCH] avoid expect/expect_err (#455) --- pallets/pooled-staking/src/candidate.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pallets/pooled-staking/src/candidate.rs b/pallets/pooled-staking/src/candidate.rs index 364951352..13d9aedca 100644 --- a/pallets/pooled-staking/src/candidate.rs +++ b/pallets/pooled-staking/src/candidate.rs @@ -163,9 +163,11 @@ impl Candidates { stake: new_stake.0, }; - let pos = list - .binary_search(&entry) - .expect_err("Candidate should be present at most once in the list."); + // Candidate should not appear in the list, we're instead searching where + // to insert it. + let Err(pos) = list.binary_search(&entry) else { + return Err(Error::::InconsistentState); + }; if pos >= T::EligibleCandidatesBufferSize::get() as usize { None @@ -176,7 +178,8 @@ impl Candidates { list.insert(pos, entry.clone()); list.truncate(T::EligibleCandidatesBufferSize::get() as usize) }) - .expect("list is truncated using the vec bound"); + // This should not occur as we truncate the list above. + .ok_or(Error::::InconsistentState)?; Some(pos as u32) }