Skip to content

Commit

Permalink
solana: add clippy.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
a5-pickle committed Jul 10, 2024
1 parent d23d6f0 commit 72c7c4e
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 23 deletions.
7 changes: 6 additions & 1 deletion solana/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions solana/clippy.toml
Original file line number Diff line number Diff line change
@@ -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" },
]
2 changes: 1 addition & 1 deletion solana/programs/matching-engine/src/composite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)?;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub fn execute_fast_order_local(ctx: Context<ExecuteFastOrderLocal>) -> 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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
}),
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions solana/programs/matching-engine/src/utils/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn handle_add_cctp_router_endpoint(
args: AddCctpRouterEndpointArgs,
router_endpoint_bump: Option<u8>,
) -> 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,
Expand Down Expand Up @@ -67,7 +67,7 @@ pub(crate) fn handle_add_local_router_endpoint(
token_router_custody_token: &Account<token::TokenAccount>,
router_endpoint_bump: Option<u8>,
) -> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,12 @@ fn try_compute_prepared_fill_size(fill_vaa: &LiquidityLayerVaa) -> Result<usize>

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(),
Expand Down

0 comments on commit 72c7c4e

Please sign in to comment.