From 67455136a6fb1ed305a1fa66cf419fc8934a6c35 Mon Sep 17 00:00:00 2001 From: Jon C Date: Mon, 24 Jun 2024 21:38:21 +0200 Subject: [PATCH] Fix clippy --- .../program/tests/program_test/cookies.rs | 2 +- libraries/math/src/precise_number.rs | 2 +- .../type-length-value/derive/src/builder.rs | 6 +- libraries/type-length-value/src/state.rs | 2 +- single-pool/cli/src/main.rs | 2 +- token-group/example/tests/initialize_group.rs | 4 +- .../example/tests/initialize_member.rs | 2 +- .../example/tests/update_group_authority.rs | 2 +- .../example/tests/update_group_max_size.rs | 2 +- .../tests/borrow_obligation_liquidity.rs | 1 - .../tests/withdraw_obligation_collateral.rs | 1 - .../program/src/curve/constant_price.rs | 4 +- token-swap/program/src/curve/fees.rs | 17 ++-- token-swap/program/src/processor.rs | 92 +++++++++---------- token/cli/tests/command.rs | 4 +- .../tests/confidential_transfer_fee.rs | 2 +- token/program-2022-test/tests/reallocate.rs | 5 +- .../tests/token_group_initialize_member.rs | 4 +- token/program-2022/src/pod.rs | 10 +- token/transfer-hook/cli/src/main.rs | 2 +- 20 files changed, 82 insertions(+), 84 deletions(-) diff --git a/governance/program/tests/program_test/cookies.rs b/governance/program/tests/program_test/cookies.rs index 2b477de00ce..4965acbc830 100644 --- a/governance/program/tests/program_test/cookies.rs +++ b/governance/program/tests/program_test/cookies.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use { solana_program::{clock::UnixTimestamp, instruction::Instruction, pubkey::Pubkey}, solana_sdk::signature::Keypair, @@ -72,7 +73,6 @@ impl TokenOwnerRecordCookie { .unwrap_or(&self.token_owner) } - #[allow(dead_code)] pub fn clone_governance_delegate(&self) -> Keypair { clone_keypair(&self.governance_delegate) } diff --git a/libraries/math/src/precise_number.rs b/libraries/math/src/precise_number.rs index 6e9c10651e5..23d66af0e71 100644 --- a/libraries/math/src/precise_number.rs +++ b/libraries/math/src/precise_number.rs @@ -346,7 +346,7 @@ impl PreciseNumber { /// Based on testing around the limits, this base is the smallest value that /// provides an epsilon of 11 digits fn maximum_sqrt_base() -> Self { - Self::new(std::u128::MAX).unwrap() + Self::new(u128::MAX).unwrap() } /// Approximate the square root using Newton's method. Based on testing, diff --git a/libraries/type-length-value/derive/src/builder.rs b/libraries/type-length-value/derive/src/builder.rs index a11c5b6b109..15a9d384ea8 100644 --- a/libraries/type-length-value/derive/src/builder.rs +++ b/libraries/type-length-value/derive/src/builder.rs @@ -74,15 +74,15 @@ impl From<&SplBorshVariableLenPackBuilder> for TokenStream { let where_clause = &builder.where_clause; quote! { impl #generics spl_type_length_value::variable_len_pack::VariableLenPack for #ident #generics #where_clause { - fn pack_into_slice(&self, dst: &mut [u8]) -> Result<(), solana_program::program_error::ProgramError> { + fn pack_into_slice(&self, dst: &mut [u8]) -> Result<(), spl_type_length_value::solana_program::program_error::ProgramError> { borsh::to_writer(&mut dst[..], self).map_err(Into::into) } - fn unpack_from_slice(src: &[u8]) -> Result { + fn unpack_from_slice(src: &[u8]) -> Result { solana_program::borsh1::try_from_slice_unchecked(src).map_err(Into::into) } - fn get_packed_len(&self) -> Result { + fn get_packed_len(&self) -> Result { solana_program::borsh1::get_instance_packed_len(self).map_err(Into::into) } } diff --git a/libraries/type-length-value/src/state.rs b/libraries/type-length-value/src/state.rs index 8321ce76f74..8ca7c20a060 100644 --- a/libraries/type-length-value/src/state.rs +++ b/libraries/type-length-value/src/state.rs @@ -756,7 +756,7 @@ mod test { // correct due to the good discriminator length and zero length assert_eq!( get_discriminators_and_end_index(&[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]).unwrap(), - (vec![ArrayDiscriminator::try_from(1).unwrap()], 12) + (vec![ArrayDiscriminator::from(1)], 12) ); // correct since it's just uninitialized data assert_eq!( diff --git a/single-pool/cli/src/main.rs b/single-pool/cli/src/main.rs index 10aaa89afd6..b469edc37bb 100644 --- a/single-pool/cli/src/main.rs +++ b/single-pool/cli/src/main.rs @@ -282,7 +282,7 @@ async fn command_deposit(config: &Config, command_config: DepositCli) -> Command .into()); } - if stake.delegation.deactivation_epoch < std::u64::MAX { + if stake.delegation.deactivation_epoch < u64::MAX { return Err(format!( "Stake account {} is deactivating or deactivated", stake_account_address diff --git a/token-group/example/tests/initialize_group.rs b/token-group/example/tests/initialize_group.rs index a6461324eb8..07c650422a3 100644 --- a/token-group/example/tests/initialize_group.rs +++ b/token-group/example/tests/initialize_group.rs @@ -51,7 +51,7 @@ async fn test_initialize_group() { &group.pubkey(), &group_mint.pubkey(), &group_mint_authority.pubkey(), - group_state.update_authority.try_into().unwrap(), + group_state.update_authority.into(), group_state.max_size.into(), ); init_group_ix.accounts[2].is_signer = false; @@ -95,7 +95,7 @@ async fn test_initialize_group() { &group.pubkey(), &group_mint.pubkey(), &group_mint_authority.pubkey(), - group_state.update_authority.try_into().unwrap(), + group_state.update_authority.into(), group_state.max_size.into(), ), ], diff --git a/token-group/example/tests/initialize_member.rs b/token-group/example/tests/initialize_member.rs index b0f7193d3cc..6a0589e81e7 100644 --- a/token-group/example/tests/initialize_member.rs +++ b/token-group/example/tests/initialize_member.rs @@ -84,7 +84,7 @@ async fn test_initialize_group_member() { &group.pubkey(), &group_mint.pubkey(), &group_mint_authority.pubkey(), - group_state.update_authority.try_into().unwrap(), + group_state.update_authority.into(), group_state.max_size.into(), ), ], diff --git a/token-group/example/tests/update_group_authority.rs b/token-group/example/tests/update_group_authority.rs index 35c74ec8682..f74248cc3d2 100644 --- a/token-group/example/tests/update_group_authority.rs +++ b/token-group/example/tests/update_group_authority.rs @@ -65,7 +65,7 @@ async fn test_update_group_authority() { &group.pubkey(), &group_mint.pubkey(), &group_mint_authority.pubkey(), - group_state.update_authority.try_into().unwrap(), + group_state.update_authority.into(), group_state.max_size.into(), ), ], diff --git a/token-group/example/tests/update_group_max_size.rs b/token-group/example/tests/update_group_max_size.rs index 412335bfc18..52ba3a8f9e3 100644 --- a/token-group/example/tests/update_group_max_size.rs +++ b/token-group/example/tests/update_group_max_size.rs @@ -66,7 +66,7 @@ async fn test_update_group_max_size() { &group.pubkey(), &group_mint.pubkey(), &group_mint_authority.pubkey(), - group_state.update_authority.try_into().unwrap(), + group_state.update_authority.into(), group_state.max_size.into(), ), ], diff --git a/token-lending/program/tests/borrow_obligation_liquidity.rs b/token-lending/program/tests/borrow_obligation_liquidity.rs index 04cf626e884..848e58cb91e 100644 --- a/token-lending/program/tests/borrow_obligation_liquidity.rs +++ b/token-lending/program/tests/borrow_obligation_liquidity.rs @@ -18,7 +18,6 @@ use { processor::process_instruction, state::{FeeCalculation, INITIAL_COLLATERAL_RATIO}, }, - std::u64, }; #[tokio::test] diff --git a/token-lending/program/tests/withdraw_obligation_collateral.rs b/token-lending/program/tests/withdraw_obligation_collateral.rs index f7ebba720de..cfdc3d8b880 100644 --- a/token-lending/program/tests/withdraw_obligation_collateral.rs +++ b/token-lending/program/tests/withdraw_obligation_collateral.rs @@ -17,7 +17,6 @@ use { processor::process_instruction, state::INITIAL_COLLATERAL_RATIO, }, - std::u64, }; #[tokio::test] diff --git a/token-swap/program/src/curve/constant_price.rs b/token-swap/program/src/curve/constant_price.rs index 4f3f026320d..07f1762f1e8 100644 --- a/token-swap/program/src/curve/constant_price.rs +++ b/token-swap/program/src/curve/constant_price.rs @@ -217,8 +217,8 @@ impl CurveCalculator for ConstantPriceCurve { ) -> Option { let swap_token_b_value = swap_token_b_amount.checked_mul(self.token_b_price as u128)?; // special logic in case we're close to the limits, avoid overflowing u128 - let value = if swap_token_b_value.saturating_sub(std::u64::MAX.into()) - > (std::u128::MAX.saturating_sub(std::u64::MAX.into())) + let value = if swap_token_b_value.saturating_sub(u64::MAX.into()) + > (u128::MAX.saturating_sub(u64::MAX.into())) { swap_token_b_value .checked_div(2)? diff --git a/token-swap/program/src/curve/fees.rs b/token-swap/program/src/curve/fees.rs index b62cecf5cde..844a6342315 100644 --- a/token-swap/program/src/curve/fees.rs +++ b/token-swap/program/src/curve/fees.rs @@ -7,7 +7,6 @@ use { program_error::ProgramError, program_pack::{IsInitialized, Pack, Sealed}, }, - std::convert::TryFrom, }; /// Encapsulates all fee information and calculations for swap operations @@ -101,8 +100,8 @@ impl Fees { pub fn owner_withdraw_fee(&self, pool_tokens: u128) -> Option { calculate_fee( pool_tokens, - u128::try_from(self.owner_withdraw_fee_numerator).ok()?, - u128::try_from(self.owner_withdraw_fee_denominator).ok()?, + u128::from(self.owner_withdraw_fee_numerator), + u128::from(self.owner_withdraw_fee_denominator), ) } @@ -110,8 +109,8 @@ impl Fees { pub fn trading_fee(&self, trading_tokens: u128) -> Option { calculate_fee( trading_tokens, - u128::try_from(self.trade_fee_numerator).ok()?, - u128::try_from(self.trade_fee_denominator).ok()?, + u128::from(self.trade_fee_numerator), + u128::from(self.trade_fee_denominator), ) } @@ -119,8 +118,8 @@ impl Fees { pub fn owner_trading_fee(&self, trading_tokens: u128) -> Option { calculate_fee( trading_tokens, - u128::try_from(self.owner_trade_fee_numerator).ok()?, - u128::try_from(self.owner_trade_fee_denominator).ok()?, + u128::from(self.owner_trade_fee_numerator), + u128::from(self.owner_trade_fee_denominator), ) } @@ -159,8 +158,8 @@ impl Fees { pub fn host_fee(&self, owner_fee: u128) -> Option { calculate_fee( owner_fee, - u128::try_from(self.host_fee_numerator).ok()?, - u128::try_from(self.host_fee_denominator).ok()?, + u128::from(self.host_fee_numerator), + u128::from(self.host_fee_denominator), ) } diff --git a/token-swap/program/src/processor.rs b/token-swap/program/src/processor.rs index b46462ebf86..7fc63cb5a56 100644 --- a/token-swap/program/src/processor.rs +++ b/token-swap/program/src/processor.rs @@ -475,9 +475,9 @@ impl Processor { let result = token_swap .swap_curve() .swap( - to_u128(actual_amount_in)?, - to_u128(source_account.amount)?, - to_u128(dest_account.amount)?, + u128::from(actual_amount_in), + u128::from(source_account.amount), + u128::from(dest_account.amount), trade_direction, token_swap.fees(), ) @@ -562,7 +562,7 @@ impl Processor { result.owner_fee, swap_token_a_amount, swap_token_b_amount, - to_u128(pool_mint.supply)?, + u128::from(pool_mint.supply), trade_direction, RoundDirection::Floor, ) @@ -672,9 +672,9 @@ impl Processor { let token_a = Self::unpack_token_account(token_a_info, token_swap.token_program_id())?; let token_b = Self::unpack_token_account(token_b_info, token_swap.token_program_id())?; let pool_mint = Self::unpack_mint(pool_mint_info, token_swap.token_program_id())?; - let current_pool_mint_supply = to_u128(pool_mint.supply)?; + let current_pool_mint_supply = u128::from(pool_mint.supply); let (pool_token_amount, pool_mint_supply) = if current_pool_mint_supply > 0 { - (to_u128(pool_token_amount)?, current_pool_mint_supply) + (u128::from(pool_token_amount), current_pool_mint_supply) } else { (calculator.new_pool_supply(), calculator.new_pool_supply()) }; @@ -683,8 +683,8 @@ impl Processor { .pool_tokens_to_trading_tokens( pool_token_amount, pool_mint_supply, - to_u128(token_a.amount)?, - to_u128(token_b.amount)?, + u128::from(token_a.amount), + u128::from(token_b.amount), RoundDirection::Ceiling, ) .ok_or(SwapError::ZeroTradingTokens)?; @@ -794,22 +794,22 @@ impl Processor { } else { token_swap .fees() - .owner_withdraw_fee(to_u128(pool_token_amount)?) + .owner_withdraw_fee(u128::from(pool_token_amount)) .ok_or(SwapError::FeeCalculationFailure)? } } Err(_) => 0, }; - let pool_token_amount = to_u128(pool_token_amount)? + let pool_token_amount = u128::from(pool_token_amount) .checked_sub(withdraw_fee) .ok_or(SwapError::CalculationFailure)?; let results = calculator .pool_tokens_to_trading_tokens( pool_token_amount, - to_u128(pool_mint.supply)?, - to_u128(token_a.amount)?, - to_u128(token_b.amount)?, + u128::from(pool_mint.supply), + u128::from(token_a.amount), + u128::from(token_b.amount), RoundDirection::Floor, ) .ok_or(SwapError::ZeroTradingTokens)?; @@ -942,14 +942,14 @@ impl Processor { )?; let pool_mint = Self::unpack_mint(pool_mint_info, token_swap.token_program_id())?; - let pool_mint_supply = to_u128(pool_mint.supply)?; + let pool_mint_supply = u128::from(pool_mint.supply); let pool_token_amount = if pool_mint_supply > 0 { token_swap .swap_curve() .deposit_single_token_type( - to_u128(source_token_amount)?, - to_u128(swap_token_a.amount)?, - to_u128(swap_token_b.amount)?, + u128::from(source_token_amount), + u128::from(swap_token_a.amount), + u128::from(swap_token_b.amount), pool_mint_supply, trade_direction, token_swap.fees(), @@ -1067,14 +1067,14 @@ impl Processor { )?; let pool_mint = Self::unpack_mint(pool_mint_info, token_swap.token_program_id())?; - let pool_mint_supply = to_u128(pool_mint.supply)?; - let swap_token_a_amount = to_u128(swap_token_a.amount)?; - let swap_token_b_amount = to_u128(swap_token_b.amount)?; + let pool_mint_supply = u128::from(pool_mint.supply); + let swap_token_a_amount = u128::from(swap_token_a.amount); + let swap_token_b_amount = u128::from(swap_token_b.amount); let burn_pool_token_amount = token_swap .swap_curve() .withdraw_single_token_type_exact_out( - to_u128(destination_token_amount)?, + u128::from(destination_token_amount), swap_token_a_amount, swap_token_b_amount, pool_mint_supply, @@ -1250,10 +1250,6 @@ impl Processor { } } -fn to_u128(val: u64) -> Result { - val.try_into().map_err(|_| SwapError::ConversionFailure) -} - fn to_u64(val: u128) -> Result { val.try_into().map_err(|_| SwapError::ConversionFailure) } @@ -4758,9 +4754,9 @@ mod tests { .calculator .pool_tokens_to_trading_tokens( withdraw_amount - withdraw_fee, - pool_mint.base.supply.try_into().unwrap(), - swap_token_a.base.amount.try_into().unwrap(), - swap_token_b.base.amount.try_into().unwrap(), + pool_mint.base.supply.into(), + swap_token_a.base.amount.into(), + swap_token_b.base.amount.into(), RoundDirection::Floor, ) .unwrap(); @@ -4837,10 +4833,10 @@ mod tests { .swap_curve .calculator .pool_tokens_to_trading_tokens( - pool_fee_amount.try_into().unwrap(), - pool_mint.base.supply.try_into().unwrap(), - swap_token_a.base.amount.try_into().unwrap(), - swap_token_b.base.amount.try_into().unwrap(), + pool_fee_amount.into(), + pool_mint.base.supply.into(), + swap_token_a.base.amount.into(), + swap_token_b.base.amount.into(), RoundDirection::Floor, ) .unwrap(); @@ -6005,10 +6001,10 @@ mod tests { let pool_token_amount = accounts .swap_curve .withdraw_single_token_type_exact_out( - destination_a_amount.try_into().unwrap(), - swap_token_a.base.amount.try_into().unwrap(), - swap_token_b.base.amount.try_into().unwrap(), - pool_mint.base.supply.try_into().unwrap(), + destination_a_amount.into(), + swap_token_a.base.amount.into(), + swap_token_b.base.amount.into(), + pool_mint.base.supply.into(), TradeDirection::AtoB, &accounts.fees, ) @@ -6165,9 +6161,9 @@ mod tests { let actual_a_to_b_amount = a_to_b_amount - token_a_fee; let results = swap_curve .swap( - actual_a_to_b_amount.try_into().unwrap(), - token_a_amount.try_into().unwrap(), - token_b_amount.try_into().unwrap(), + actual_a_to_b_amount.into(), + token_a_amount.into(), + token_b_amount.into(), TradeDirection::AtoB, &fees, ) @@ -6201,9 +6197,9 @@ mod tests { .calculator .withdraw_single_token_type_exact_out( results.owner_fee, - token_a_amount.try_into().unwrap(), - token_b_amount.try_into().unwrap(), - initial_supply.try_into().unwrap(), + token_a_amount.into(), + token_b_amount.into(), + initial_supply.into(), TradeDirection::AtoB, RoundDirection::Floor, ) @@ -6243,9 +6239,9 @@ mod tests { let mut results = swap_curve .swap( - b_to_a_amount.try_into().unwrap(), - token_b_amount.try_into().unwrap(), - token_a_amount.try_into().unwrap(), + b_to_a_amount.into(), + token_b_amount.into(), + token_a_amount.into(), TradeDirection::BtoA, &fees, ) @@ -6290,9 +6286,9 @@ mod tests { .calculator .withdraw_single_token_type_exact_out( results.owner_fee, - token_a_amount.try_into().unwrap(), - token_b_amount.try_into().unwrap(), - initial_supply.try_into().unwrap(), + token_a_amount.into(), + token_b_amount.into(), + initial_supply.into(), TradeDirection::BtoA, RoundDirection::Floor, ) diff --git a/token/cli/tests/command.rs b/token/cli/tests/command.rs index a4c3dbe2066..1b66cfaa0e7 100644 --- a/token/cli/tests/command.rs +++ b/token/cli/tests/command.rs @@ -2492,7 +2492,7 @@ async fn transfer_fee(test_validator: &TestValidator, payer: &Keypair) { let extension = mint_state.get_extension::().unwrap(); assert_eq!( - Option::::try_from(extension.transfer_fee_config_authority).unwrap(), + Option::::from(extension.transfer_fee_config_authority), None, ); @@ -2516,7 +2516,7 @@ async fn transfer_fee(test_validator: &TestValidator, payer: &Keypair) { let extension = mint_state.get_extension::().unwrap(); assert_eq!( - Option::::try_from(extension.withdraw_withheld_authority).unwrap(), + Option::::from(extension.withdraw_withheld_authority), None, ); } diff --git a/token/program-2022-test/tests/confidential_transfer_fee.rs b/token/program-2022-test/tests/confidential_transfer_fee.rs index f45a4f259f4..161ee2c2a81 100644 --- a/token/program-2022-test/tests/confidential_transfer_fee.rs +++ b/token/program-2022-test/tests/confidential_transfer_fee.rs @@ -430,7 +430,7 @@ async fn confidential_transfer_initialize_and_update_mint() { ); assert_eq!( extension.auto_approve_new_accounts, - new_auto_approve_new_accounts.try_into().unwrap(), + new_auto_approve_new_accounts.into(), ); assert_eq!( extension.auditor_elgamal_pubkey, diff --git a/token/program-2022-test/tests/reallocate.rs b/token/program-2022-test/tests/reallocate.rs index 733dfd1aca0..95e95ee5163 100644 --- a/token/program-2022-test/tests/reallocate.rs +++ b/token/program-2022-test/tests/reallocate.rs @@ -16,7 +16,6 @@ use { }, spl_token_2022::{error::TokenError, extension::ExtensionType, state::Account}, spl_token_client::token::{ExtensionInitializationParams, TokenError as TokenClientError}, - std::convert::TryInto, test_case::test_case, }; @@ -162,8 +161,8 @@ async fn reallocate_without_current_extension_knowledge() { let mut context = TestContext::new().await; context .init_token_with_mint(vec![ExtensionInitializationParams::TransferFeeConfig { - transfer_fee_config_authority: COption::Some(Pubkey::new_unique()).try_into().unwrap(), - withdraw_withheld_authority: COption::Some(Pubkey::new_unique()).try_into().unwrap(), + transfer_fee_config_authority: COption::Some(Pubkey::new_unique()).into(), + withdraw_withheld_authority: COption::Some(Pubkey::new_unique()).into(), transfer_fee_basis_points: 250, maximum_fee: 10_000_000, }]) diff --git a/token/program-2022-test/tests/token_group_initialize_member.rs b/token/program-2022-test/tests/token_group_initialize_member.rs index 53413010032..b25a8a6331d 100644 --- a/token/program-2022-test/tests/token_group_initialize_member.rs +++ b/token/program-2022-test/tests/token_group_initialize_member.rs @@ -247,7 +247,7 @@ async fn success_initialize() { &TokenGroupMember { mint: member1_mint_keypair.pubkey(), group: group_mint_keypair.pubkey(), - member_number: 1.try_into().unwrap(), + member_number: 1.into(), } ); @@ -298,7 +298,7 @@ async fn success_initialize() { &TokenGroupMember { mint: member2_mint_keypair.pubkey(), group: group_mint_keypair.pubkey(), - member_number: 2.try_into().unwrap(), + member_number: 2.into(), } ); diff --git a/token/program-2022/src/pod.rs b/token/program-2022/src/pod.rs index 3bab7e297ea..bc7f80c7218 100644 --- a/token/program-2022/src/pod.rs +++ b/token/program-2022/src/pod.rs @@ -159,11 +159,17 @@ impl From for PodMultisig { /// COption stored as a Pod type #[repr(C, packed)] #[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] -pub struct PodCOption { +pub struct PodCOption +where + T: Pod + Default, +{ pub(crate) option: [u8; 4], pub(crate) value: T, } -impl PodCOption { +impl PodCOption +where + T: Pod + Default, +{ /// Represents that no value is stored in the option, like `Option::None` pub const NONE: [u8; 4] = [0; 4]; /// Represents that some value is stored in the option, like diff --git a/token/transfer-hook/cli/src/main.rs b/token/transfer-hook/cli/src/main.rs index 67721380aa8..5566fe7266a 100644 --- a/token/transfer-hook/cli/src/main.rs +++ b/token/transfer-hook/cli/src/main.rs @@ -631,7 +631,7 @@ mod test { ); let required_address = Pubkey::new_unique(); - let accounts = vec![AccountMeta::new_readonly(required_address, false)]; + let accounts = [AccountMeta::new_readonly(required_address, false)]; process_create_extra_account_metas( &rpc_client, &program_id,