From 72c7c4e7acdcefc46904a06f11cff3e0a92f96bb Mon Sep 17 00:00:00 2001 From: A5 Pickle Date: Wed, 10 Jul 2024 16:03:29 -0500 Subject: [PATCH] solana: add clippy.toml --- solana/Cargo.toml | 7 ++++++- solana/clippy.toml | 7 +++++++ solana/programs/matching-engine/src/composite/mod.rs | 2 +- .../src/processor/admin/propose/auction_parameters.rs | 2 +- .../matching-engine/src/processor/admin/propose/mod.rs | 8 ++++---- .../src/processor/auction/execute_fast_order/cctp.rs | 2 +- .../src/processor/auction/execute_fast_order/local.rs | 2 +- .../src/processor/auction/execute_fast_order/mod.rs | 2 +- .../src/processor/auction/history/add_entry.rs | 2 +- .../src/processor/auction/history/create_new.rs | 2 +- .../src/processor/auction/offer/place_initial/cctp.rs | 4 ++-- .../src/processor/auction/prepare_order_response/cctp.rs | 8 ++++---- .../src/processor/fast_fill/reserve_sequence/mod.rs | 2 +- solana/programs/matching-engine/src/utils/admin.rs | 4 ++-- .../token-router/src/processor/redeem_fill/cctp.rs | 4 ++-- 15 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 solana/clippy.toml diff --git a/solana/Cargo.toml b/solana/Cargo.toml index 7bd4089f..ac70d145 100644 --- a/solana/Cargo.toml +++ b/solana/Cargo.toml @@ -59,7 +59,12 @@ incremental = false codegen-units = 1 [workspace.lints.clippy] -correctness = { level = "warn"} +correctness = { priority = -1, level = "warn"} + +### See clippy.toml. +unnecessary_lazy_evaluations = "allow" + +or_fun_call = "allow" arithmetic_side_effects = "deny" as_conversions = "deny" diff --git a/solana/clippy.toml b/solana/clippy.toml new file mode 100644 index 00000000..b06464d4 --- /dev/null +++ b/solana/clippy.toml @@ -0,0 +1,7 @@ +### We prefer lazy evaluation to save on compute units. +disallowed-methods = [ + { path = "std::option::Option::and", reason = "prefer `and_then` for lazy evaluation" }, + { path = "std::option::Option::map_or", reason = "prefer `map_or_else` for lazy evaluation" }, + { path = "std::option::Option::ok_or", reason = "prefer `ok_or_else` for lazy evaluation" }, + { path = "std::option::Option::unwrap_or", reason = "prefer `unwrap_or_else` for lazy evaluation" }, +] \ No newline at end of file diff --git a/solana/programs/matching-engine/src/composite/mod.rs b/solana/programs/matching-engine/src/composite/mod.rs index ead36dcc..b43d0421 100644 --- a/solana/programs/matching-engine/src/composite/mod.rs +++ b/solana/programs/matching-engine/src/composite/mod.rs @@ -276,7 +276,7 @@ pub struct FastOrderPath<'info> { let message = LiquidityLayerMessage::try_from(vaa.payload()).unwrap(); let order = message .fast_market_order() - .ok_or(MatchingEngineError::NotFastMarketOrder)?; + .ok_or_else(|| MatchingEngineError::NotFastMarketOrder)?; require_eq!( path.to_endpoint.chain, order.target_chain(), diff --git a/solana/programs/matching-engine/src/processor/admin/propose/auction_parameters.rs b/solana/programs/matching-engine/src/processor/admin/propose/auction_parameters.rs index 992cd384..1d2ed939 100644 --- a/solana/programs/matching-engine/src/processor/admin/propose/auction_parameters.rs +++ b/solana/programs/matching-engine/src/processor/admin/propose/auction_parameters.rs @@ -41,7 +41,7 @@ pub fn propose_auction_parameters( .custodian .auction_config_id .checked_add(1) - .ok_or(MatchingEngineError::U32Overflow)?; + .ok_or_else(|| MatchingEngineError::U32Overflow)?; let action = ProposalAction::UpdateAuctionParameters { id, parameters }; super::propose( diff --git a/solana/programs/matching-engine/src/processor/admin/propose/mod.rs b/solana/programs/matching-engine/src/processor/admin/propose/mod.rs index 29ebe075..132412c2 100644 --- a/solana/programs/matching-engine/src/processor/admin/propose/mod.rs +++ b/solana/programs/matching-engine/src/processor/admin/propose/mod.rs @@ -28,7 +28,7 @@ fn propose(accounts: Propose, action: ProposalAction, proposal_bump_seed: u8) -> custodian .next_proposal_id .checked_add(1) - .ok_or(MatchingEngineError::U64Overflow)?; + .ok_or_else(|| MatchingEngineError::U64Overflow)?; let slot_proposed_at = Clock::get().unwrap().slot; @@ -38,18 +38,18 @@ fn propose(accounts: Propose, action: ProposalAction, proposal_bump_seed: u8) -> // Arbitrary set for fast testing. let slot_enact_delay = slot_proposed_at .checked_add(8) - .ok_or(MatchingEngineError::U64Overflow)?; + .ok_or_else(|| MatchingEngineError::U64Overflow)?; } else if #[cfg(feature = "testnet")] { let _ = epoch_schedule; // Arbitrary set to roughly 10 seconds (10 seconds / 0.4 seconds per slot) for // faster testing. let slot_enact_delay = slot_proposed_at .checked_add(25) - .ok_or(MatchingEngineError::U64Overflow)?; + .ok_or_else(|| MatchingEngineError::U64Overflow)?; } else { let slot_enact_delay = slot_proposed_at .checked_add(epoch_schedule.slots_per_epoch) - .ok_or(MatchingEngineError::U64Overflow)?; + .ok_or_else(|| MatchingEngineError::U64Overflow)?; } } diff --git a/solana/programs/matching-engine/src/processor/auction/execute_fast_order/cctp.rs b/solana/programs/matching-engine/src/processor/auction/execute_fast_order/cctp.rs index 4f8c1078..9a502507 100644 --- a/solana/programs/matching-engine/src/processor/auction/execute_fast_order/cctp.rs +++ b/solana/programs/matching-engine/src/processor/auction/execute_fast_order/cctp.rs @@ -188,7 +188,7 @@ pub fn handle_execute_fast_order_cctp( token_program.to_account_info(), token::CloseAccount { account: auction_custody_token.to_account_info(), - destination: beneficiary.unwrap_or(payer.to_account_info()), + destination: beneficiary.unwrap_or_else(|| payer.to_account_info()), authority: custodian.to_account_info(), }, &[Custodian::SIGNER_SEEDS], diff --git a/solana/programs/matching-engine/src/processor/auction/execute_fast_order/local.rs b/solana/programs/matching-engine/src/processor/auction/execute_fast_order/local.rs index d2f2703c..eb736d58 100644 --- a/solana/programs/matching-engine/src/processor/auction/execute_fast_order/local.rs +++ b/solana/programs/matching-engine/src/processor/auction/execute_fast_order/local.rs @@ -140,7 +140,7 @@ pub fn execute_fast_order_local(ctx: Context) -> Result<( token_program.to_account_info(), token::CloseAccount { account: auction_custody_token.to_account_info(), - destination: beneficiary.unwrap_or(ctx.accounts.payer.to_account_info()), + destination: beneficiary.unwrap_or_else(|| ctx.accounts.payer.to_account_info()), authority: custodian.to_account_info(), }, &[Custodian::SIGNER_SEEDS], diff --git a/solana/programs/matching-engine/src/processor/auction/execute_fast_order/mod.rs b/solana/programs/matching-engine/src/processor/auction/execute_fast_order/mod.rs index b4269487..86f8516c 100644 --- a/solana/programs/matching-engine/src/processor/auction/execute_fast_order/mod.rs +++ b/solana/programs/matching-engine/src/processor/auction/execute_fast_order/mod.rs @@ -148,7 +148,7 @@ fn prepare_order_execution<'info>( // Add it to the reimbursement. deposit_and_fee = deposit_and_fee .checked_add(init_auction_fee) - .ok_or(MatchingEngineError::U64Overflow)?; + .ok_or_else(|| MatchingEngineError::U64Overflow)?; } } diff --git a/solana/programs/matching-engine/src/processor/auction/history/add_entry.rs b/solana/programs/matching-engine/src/processor/auction/history/add_entry.rs index 002cd8cb..d49ac2a0 100644 --- a/solana/programs/matching-engine/src/processor/auction/history/add_entry.rs +++ b/solana/programs/matching-engine/src/processor/auction/history/add_entry.rs @@ -103,7 +103,7 @@ fn handle_add_auction_history_entry( // Update the history account with this new entry's vaa timestamp if it is less than the min or // greater than the max. let auction = &ctx.accounts.auction; - if auction.vaa_timestamp < history.min_timestamp.unwrap_or(u32::MAX) { + if auction.vaa_timestamp < history.min_timestamp.unwrap_or_else(|| u32::MAX) { history.min_timestamp = auction.vaa_timestamp.into(); } if auction.vaa_timestamp > history.max_timestamp.unwrap_or_default() { diff --git a/solana/programs/matching-engine/src/processor/auction/history/create_new.rs b/solana/programs/matching-engine/src/processor/auction/history/create_new.rs index d37a98d8..6fa33a73 100644 --- a/solana/programs/matching-engine/src/processor/auction/history/create_new.rs +++ b/solana/programs/matching-engine/src/processor/auction/history/create_new.rs @@ -37,7 +37,7 @@ pub struct CreateNewAuctionHistory<'info> { .id .checked_add(1) .map(|new_id| new_id.to_be_bytes()) - .ok_or(MatchingEngineError::U32Overflow)?, + .ok_or_else(|| MatchingEngineError::U32Overflow)?, ], bump, )] diff --git a/solana/programs/matching-engine/src/processor/auction/offer/place_initial/cctp.rs b/solana/programs/matching-engine/src/processor/auction/offer/place_initial/cctp.rs index 91c905b4..2f04aaf4 100644 --- a/solana/programs/matching-engine/src/processor/auction/offer/place_initial/cctp.rs +++ b/solana/programs/matching-engine/src/processor/auction/offer/place_initial/cctp.rs @@ -55,7 +55,7 @@ pub struct PlaceInitialOfferCctp<'info> { let message = LiquidityLayerMessage::try_from(fast_vaa.payload()).unwrap(); let order = message .fast_market_order() - .ok_or(MatchingEngineError::InvalidPayloadId)?; + .ok_or_else(|| MatchingEngineError::InvalidPayloadId)?; let curr_time = Clock::get().unwrap().unix_timestamp; @@ -200,6 +200,6 @@ pub fn place_initial_offer_cctp( ), amount_in .checked_add(security_deposit) - .ok_or(MatchingEngineError::U64Overflow)?, + .ok_or_else(|| MatchingEngineError::U64Overflow)?, ) } diff --git a/solana/programs/matching-engine/src/processor/auction/prepare_order_response/cctp.rs b/solana/programs/matching-engine/src/processor/auction/prepare_order_response/cctp.rs index 3f270f5f..88778eba 100644 --- a/solana/programs/matching-engine/src/processor/auction/prepare_order_response/cctp.rs +++ b/solana/programs/matching-engine/src/processor/auction/prepare_order_response/cctp.rs @@ -51,12 +51,12 @@ pub struct PrepareOrderResponseCctp<'info> { let finalized_msg = LiquidityLayerMessage::try_from(finalized_vaa.payload()).unwrap(); let deposit = finalized_msg .deposit() - .ok_or(MatchingEngineError::InvalidPayloadId)?; + .ok_or_else(|| MatchingEngineError::InvalidPayloadId)?; let deposit_msg = LiquidityLayerDepositMessage::try_from(deposit.payload()) .map_err(|_| error!(MatchingEngineError::InvalidDepositMessage))?; let slow_order_response = deposit_msg .slow_order_response() - .ok_or(MatchingEngineError::InvalidDepositPayloadId)?; + .ok_or_else(|| MatchingEngineError::InvalidDepositPayloadId)?; true } @@ -72,7 +72,7 @@ pub struct PrepareOrderResponseCctp<'info> { .unwrap(); let order = message .fast_market_order() - .ok_or(MatchingEngineError::InvalidPayloadId)?; + .ok_or_else(|| MatchingEngineError::InvalidPayloadId)?; order.redeemer_message_len().into() }), @@ -193,7 +193,7 @@ fn handle_prepare_order_response_cctp( let message = LiquidityLayerDepositMessage::try_from(deposit.payload()).unwrap(); let order_response = message .slow_order_response() - .ok_or(MatchingEngineError::InvalidPayloadId)?; + .ok_or_else(|| MatchingEngineError::InvalidPayloadId)?; let fast_vaa = ctx.accounts.fast_order_path.fast_vaa.load_unchecked(); let order = LiquidityLayerMessage::try_from(fast_vaa.payload()) diff --git a/solana/programs/matching-engine/src/processor/fast_fill/reserve_sequence/mod.rs b/solana/programs/matching-engine/src/processor/fast_fill/reserve_sequence/mod.rs index 4b7d61ca..94bdf408 100644 --- a/solana/programs/matching-engine/src/processor/fast_fill/reserve_sequence/mod.rs +++ b/solana/programs/matching-engine/src/processor/fast_fill/reserve_sequence/mod.rs @@ -70,7 +70,7 @@ fn set_reserved_sequence_data( // Now uptick sequencer's value. If this errors out, we have problems. *next_sequence = next_sequence .checked_add(1) - .ok_or(MatchingEngineError::U64Overflow)?; + .ok_or_else(|| MatchingEngineError::U64Overflow)?; // Emit an event to help auction participants track the fast fill sequence so they can more // easily execute local orders. diff --git a/solana/programs/matching-engine/src/utils/admin.rs b/solana/programs/matching-engine/src/utils/admin.rs index 5670b838..b5fc34bc 100644 --- a/solana/programs/matching-engine/src/utils/admin.rs +++ b/solana/programs/matching-engine/src/utils/admin.rs @@ -19,7 +19,7 @@ pub(crate) fn handle_add_cctp_router_endpoint( args: AddCctpRouterEndpointArgs, router_endpoint_bump: Option, ) -> Result<()> { - let bump = router_endpoint_bump.unwrap_or(router_endpoint.bump); + let bump = router_endpoint_bump.unwrap_or_else(|| router_endpoint.bump); let AddCctpRouterEndpointArgs { chain, @@ -67,7 +67,7 @@ pub(crate) fn handle_add_local_router_endpoint( token_router_custody_token: &Account, router_endpoint_bump: Option, ) -> Result<()> { - let bump = router_endpoint_bump.unwrap_or(router_endpoint.bump); + let bump = router_endpoint_bump.unwrap_or_else(|| router_endpoint.bump); router_endpoint.set_inner(RouterEndpoint { bump, diff --git a/solana/programs/token-router/src/processor/redeem_fill/cctp.rs b/solana/programs/token-router/src/processor/redeem_fill/cctp.rs index 175567b7..eadf1af2 100644 --- a/solana/programs/token-router/src/processor/redeem_fill/cctp.rs +++ b/solana/programs/token-router/src/processor/redeem_fill/cctp.rs @@ -276,12 +276,12 @@ fn try_compute_prepared_fill_size(fill_vaa: &LiquidityLayerVaa) -> Result let deposit = msg .deposit() - .ok_or(error!(TokenRouterError::InvalidPayloadId))?; + .ok_or_else(|| error!(TokenRouterError::InvalidPayloadId))?; let msg = LiquidityLayerDepositMessage::try_from(deposit.payload()) .map_err(|_| TokenRouterError::InvalidDepositMessage)?; let fill = msg .fill() - .ok_or(TokenRouterError::InvalidDepositPayloadId)?; + .ok_or_else(|| TokenRouterError::InvalidDepositPayloadId)?; Ok(PreparedFill::compute_size( fill.redeemer_message_len().into(),