Skip to content

Commit

Permalink
Adds clippy lint to avoid unwrap_or_default on Results
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Sep 10, 2024
1 parent 08425fe commit 6e4123e
Show file tree
Hide file tree
Showing 18 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ disallowed-methods = [
{ path = "chrono::Utc::now", reason = "Do not use current date/time in code that must be deterministic" },
{ path = "namada_core::time::DateTimeUtc::now", reason = "Do not use current date/time in code that must be deterministic" },
{ path = "wasmtimer::std::Instant", reason = "Do not use current date/time in code that must be deterministic" },
{ path = "std::result::Result::unwrap_or_default", reason = "Do not silently ignore an error that could be caused by gas" },
]
allow-dbg-in-tests = true
allow-print-in-tests = true
1 change: 1 addition & 0 deletions crates/apps_lib/src/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,7 @@ pub async fn sign_genesis_tx(
if let Some(output_path) = output.as_ref() {
// If the output path contains existing signed txs, we append
// the newly signed txs to the file
#[allow(clippy::disallowed_methods)]
let mut prev_txs =
genesis::templates::read_transactions(output_path)
.unwrap_or_default();
Expand Down
1 change: 1 addition & 0 deletions crates/governance/src/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ where
if vote.is_validator() {
let vote_data = vote.data.clone();

#[allow(clippy::disallowed_methods)]
let validator_stake = PoS::read_validator_stake::<crate::Store<_>>(
storage, validator, epoch,
)
Expand Down
1 change: 1 addition & 0 deletions crates/governance/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl ProposalResult {
/// Return true if at least 2/3 of the total voting power voted and at least
/// two third of the non-abstained voting power voted nay.
/// Returns `false` if any arithmetic fails.
#[allow(clippy::disallowed_methods)]
pub fn two_thirds_nay_over_two_thirds_total(&self) -> bool {
(|| {
let two_thirds_power =
Expand Down
1 change: 1 addition & 0 deletions crates/node/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ where
};

if let Some(schedule_migration) = scheduled_migration.as_ref() {
#[allow(clippy::disallowed_methods)]
let current = state.get_block_height().unwrap_or_default();
if schedule_migration.height < current {
panic!(
Expand Down
1 change: 1 addition & 0 deletions crates/node/src/shell/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl Shell<storage::PersistentDB, Sha256Hasher> {
}
match self.syncing.as_ref() {
None => {
#[allow(clippy::disallowed_methods)]
if self.state.get_block_height().unwrap_or_default().0
< u64::from(req.snapshot.height)
{
Expand Down
1 change: 1 addition & 0 deletions crates/node/src/shell/testing/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ impl MockNode {
}

pub fn block_height(&self) -> BlockHeight {
#[allow(clippy::disallowed_methods)]
self.shell
.lock()
.unwrap()
Expand Down
1 change: 1 addition & 0 deletions crates/node/src/shims/abcipp_shim_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ pub mod shim {
let header = req.header;
FinalizeBlock {
header: BlockHeader {
#[allow(clippy::disallowed_methods)]
hash: Hash::try_from(header.app_hash.as_bytes())
.unwrap_or_default(),
time: DateTimeUtc::try_from(header.time).unwrap(),
Expand Down
4 changes: 3 additions & 1 deletion crates/proof_of_stake/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,14 @@ where

// Ensure TM stake updates properly with a debug_assert
if cfg!(debug_assertions) {
#[allow(clippy::disallowed_methods)]
let validator_vp = i64::try_from(validator_vp).unwrap_or_default();
debug_assert_eq!(
into_tm_voting_power(
params.tm_votes_per_token,
stake_from_deltas,
),
i64::try_from(validator_vp).unwrap_or_default(),
validator_vp
);
}

Expand Down
1 change: 1 addition & 0 deletions crates/proof_of_stake/src/tests/test_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ fn test_unslashed_bond_amount_aux(validators: Vec<GenesisValidator>) {
Epoch(0),
current_epoch + params.pipeline_len,
) {
#[allow(clippy::disallowed_methods)]
let amount = bond_amount(
&storage,
&BondId {
Expand Down
2 changes: 2 additions & 0 deletions crates/proof_of_stake/src/tests/test_slash_and_redel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,7 @@ fn test_slashed_bond_amount_aux(validators: Vec<GenesisValidator>) {

let pipeline_epoch = current_epoch + params.pipeline_len;

#[allow(clippy::disallowed_methods)]
let del_bond_amount = bond_amount(
&storage,
&BondId {
Expand All @@ -1648,6 +1649,7 @@ fn test_slashed_bond_amount_aux(validators: Vec<GenesisValidator>) {
)
.unwrap_or_default();

#[allow(clippy::disallowed_methods)]
let self_bond_amount = bond_amount(
&storage,
&BondId {
Expand Down
2 changes: 2 additions & 0 deletions crates/sdk/src/queries/vp/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub mod client_only_methods {
let balance = if response.data.is_empty() {
token::Amount::zero()
} else {
#[allow(clippy::disallowed_methods)]
token::Amount::try_from_slice(&response.data)
.unwrap_or_default()
};
Expand All @@ -107,6 +108,7 @@ pub mod client_only_methods {
let tokens = if response.data.is_empty() {
token::Amount::zero()
} else {
#[allow(clippy::disallowed_methods)]
token::Amount::try_from_slice(&response.data)
.unwrap_or_default()
};
Expand Down
4 changes: 4 additions & 0 deletions crates/sdk/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,10 +982,12 @@ pub async fn query_proposal_result<C: namada_io::Client + Sync>(

let is_author_pgf_steward =
is_steward(client, &proposal.author).await;
#[allow(clippy::disallowed_methods)]
let votes = query_proposal_votes(client, proposal_id)
.await
.unwrap_or_default();
let tally_type = proposal.get_tally_type(is_author_pgf_steward);
#[allow(clippy::disallowed_methods)]
let total_active_voting_power =
get_total_active_voting_power(client, tally_epoch)
.await
Expand All @@ -996,6 +998,7 @@ pub async fn query_proposal_result<C: namada_io::Client + Sync>(
for vote in votes {
match vote.is_validator() {
true => {
#[allow(clippy::disallowed_methods)]
let voting_power = get_validator_stake(
client,
tally_epoch,
Expand All @@ -1011,6 +1014,7 @@ pub async fn query_proposal_result<C: namada_io::Client + Sync>(
);
}
false => {
#[allow(clippy::disallowed_methods)]
let voting_power = get_bond_amount_at(
client,
&vote.delegator,
Expand Down
1 change: 1 addition & 0 deletions crates/sdk/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ pub async fn validate_transparent_fee<N: Namada>(
let fee_payer_address = Address::from(fee_payer);

let balance_key = balance_key(&args.fee_token, &fee_payer_address);
#[allow(clippy::disallowed_methods)]
let balance = rpc::query_storage_value::<_, token::Amount>(
context.client(),
&balance_key,
Expand Down
2 changes: 2 additions & 0 deletions crates/sdk/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3151,6 +3151,7 @@ async fn get_masp_fee_payment_amount<N: Namada>(
) -> Result<Option<MaspFeeData>> {
let fee_payer_address = Address::from(fee_payer);
let balance_key = balance_key(&args.fee_token, &fee_payer_address);
#[allow(clippy::disallowed_methods)]
let balance = rpc::query_storage_value::<_, token::Amount>(
context.client(),
&balance_key,
Expand Down Expand Up @@ -3492,6 +3493,7 @@ async fn construct_shielded_parts<N: Namada>(

// Get the decoded asset types used in the transaction to give offline
// wallet users more information
#[allow(clippy::disallowed_methods)]
let asset_types = used_asset_types(context, &shielded_parts.builder)
.await
.unwrap_or_default();
Expand Down
1 change: 1 addition & 0 deletions crates/shielded_token/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ pub fn is_amount_required(

for (asset_type, value) in gap.components() {
if *value > 0 && normed_delta[asset_type] > 0 {
#[allow(clippy::disallowed_methods)]
let signed_change_amt =
checked!(normed_delta[asset_type] - *value).unwrap_or_default();
let unsigned_change_amt = if signed_change_amt > 0 {
Expand Down
1 change: 1 addition & 0 deletions crates/shielded_token/src/masp/shielded_sync/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ where
panic_flag: PanicFlag::default(),
};

#[allow(clippy::disallowed_methods)]
let cache = ctx.utils.cache_load().await.unwrap_or_default();

Dispatcher {
Expand Down
12 changes: 9 additions & 3 deletions crates/tests/src/e2e/ibc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,19 @@ fn ibc_token_inflation() -> Result<()> {
let delegated = epoch + PIPELINE_LEN;
while epoch < delegated {
sleep(10);
epoch = get_epoch(&test, &rpc).unwrap_or_default();
#[allow(clippy::disallowed_methods)]
let new_epoch = get_epoch(&test, &rpc).unwrap_or_default();
epoch = new_epoch;
}
// inflation proposal on Namada
let start_epoch = propose_inflation(&test)?;
let mut epoch = get_epoch(&test, &rpc).unwrap();
// Vote
while epoch < start_epoch {
sleep(10);
epoch = get_epoch(&test, &rpc).unwrap_or_default();
#[allow(clippy::disallowed_methods)]
let new_epoch = get_epoch(&test, &rpc).unwrap_or_default();
epoch = new_epoch;
}
submit_votes(&test)?;

Expand Down Expand Up @@ -566,7 +570,9 @@ fn ibc_token_inflation() -> Result<()> {
let new_epoch = epoch + MASP_EPOCH_MULTIPLIER;
while epoch < new_epoch {
sleep(10);
epoch = get_epoch(&test, &rpc).unwrap_or_default();
#[allow(clippy::disallowed_methods)]
let new_epoch = get_epoch(&test, &rpc).unwrap_or_default();
epoch = new_epoch;
}

// Check balances
Expand Down

0 comments on commit 6e4123e

Please sign in to comment.