mc/clippy (#135)

* clippy pass 1

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>

* clippy pass 2

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-08-02 09:04:02 +02:00 committed by microwavedcola1
parent a9a03b97ac
commit 374c0eb136
14 changed files with 45 additions and 51 deletions

View File

@ -114,7 +114,7 @@ macro_rules! error_msg {
macro_rules! require_msg {
($invariant:expr, $($arg:tt)*) => {
if !($invariant) {
Err(error_msg!($($arg)*))?;
return Err(error_msg!($($arg)*));
}
};
}

View File

@ -25,7 +25,7 @@ pub fn account_expand(ctx: Context<AccountExpand>) -> Result<()> {
require_eq!(account_size, AccountSize::Small);
let new_space = MangoAccount::space(AccountSize::Large.try_into().unwrap());
let new_space = MangoAccount::space(AccountSize::Large);
let new_rent_minimum = Rent::get()?.minimum_balance(new_space);
let realloc_account = ctx.accounts.account.as_ref();
@ -52,7 +52,7 @@ pub fn account_expand(ctx: Context<AccountExpand>) -> Result<()> {
// expand dynamic content, e.g. to grow token positions, we need to slide serum3orders further later, and so on....
let mut account = ctx.accounts.account.load_mut()?;
account.expand_dynamic_content(AccountSize::Large.try_into().unwrap())?;
account.expand_dynamic_content(AccountSize::Large)?;
Ok(())
}

View File

@ -224,7 +224,7 @@ pub fn flash_loan_end<'key, 'accounts, 'remaining, 'info>(
vaults_with_banks[vault_index] = true;
let token_account_ai = &token_accounts[vault_index];
let token_account = Account::<TokenAccount>::try_from(&token_account_ai)?;
let token_account = Account::<TokenAccount>::try_from(token_account_ai)?;
// Ensure this bank/vault combination was mentioned in the Begin instruction:
// The Begin instruction only checks that End ends with the same vault accounts -

View File

@ -123,7 +123,7 @@ pub fn liq_token_bankruptcy(
if insurance_transfer > 0 {
// in the end, the liqee gets liab assets
liqee_liab_active = liab_bank.deposit(liqee_liab, liab_transfer)?;
remaining_liab_loss = -liqee_liab.native(&liab_bank);
remaining_liab_loss = -liqee_liab.native(liab_bank);
// move insurance assets into quote bank
let group_seeds = group_seeds!(group);

View File

@ -79,11 +79,11 @@ pub fn liq_token_with_token(
// The main complication here is that we can't keep the liqee_asset_position and liqee_liab_position
// borrows alive at the same time. Possibly adding get_mut_pair() would be helpful.
let (liqee_asset_position, liqee_asset_raw_index) = liqee.token_get(asset_token_index)?;
let liqee_assets_native = liqee_asset_position.native(&asset_bank);
let liqee_assets_native = liqee_asset_position.native(asset_bank);
require!(liqee_assets_native.is_positive(), MangoError::SomeError);
let (liqee_liab_position, liqee_liab_raw_index) = liqee.token_get(liab_token_index)?;
let liqee_liab_native = liqee_liab_position.native(&liab_bank);
let liqee_liab_native = liqee_liab_position.native(liab_bank);
require!(liqee_liab_native.is_negative(), MangoError::SomeError);
// TODO why sum of both tokens liquidation fees? Add comment

View File

@ -48,7 +48,7 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
Some(ai) => {
let mal: AccountLoaderDynamic<MangoAccount> =
AccountLoaderDynamic::try_from(&ai)?;
AccountLoaderDynamic::try_from(ai)?;
let mut ma = mal.load_mut()?;
ma.perp_execute_maker(
perp_market.perp_market_index,
@ -64,7 +64,7 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
fill.maker,
perp_market.perp_market_index as u64,
fill.price,
&ma.perp_find_account(perp_market.perp_market_index).unwrap(),
ma.perp_find_account(perp_market.perp_market_index).unwrap(),
&perp_market,
);
}
@ -77,7 +77,7 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
}
Some(ai) => {
let mal: AccountLoaderDynamic<MangoAccount> =
AccountLoaderDynamic::try_from(&ai)?;
AccountLoaderDynamic::try_from(ai)?;
let mut maker = mal.load_mut()?;
match mango_account_ais.iter().find(|ai| ai.key == &fill.taker) {
@ -87,7 +87,7 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
}
Some(ai) => {
let mal: AccountLoaderDynamic<MangoAccount> =
AccountLoaderDynamic::try_from(&ai)?;
AccountLoaderDynamic::try_from(ai)?;
let mut taker = mal.load_mut()?;
maker.perp_execute_maker(
@ -104,7 +104,7 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
fill.maker,
perp_market.perp_market_index as u64,
fill.price,
&maker
maker
.perp_find_account(perp_market.perp_market_index)
.unwrap(),
&perp_market,
@ -113,7 +113,7 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
fill.taker,
perp_market.perp_market_index as u64,
fill.price,
&taker
taker
.perp_find_account(perp_market.perp_market_index)
.unwrap(),
&perp_market,
@ -155,7 +155,7 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
}
Some(ai) => {
let mal: AccountLoaderDynamic<MangoAccount> =
AccountLoaderDynamic::try_from(&ai)?;
AccountLoaderDynamic::try_from(ai)?;
let mut ma = mal.load_mut()?;
ma.perp_remove_order(out.owner_slot as usize, out.quantity)?;

View File

@ -187,7 +187,7 @@ pub fn charge_maybe_fees(
// loan origination fees
let coin_token_account = account.token_get_mut(coin_bank.token_index)?.0;
let coin_token_native = coin_token_account.native(&coin_bank);
let coin_token_native = coin_token_account.native(coin_bank);
if coin_token_native.is_negative() {
let actualized_loan = coin_token_native.abs().min(maybe_actualized_coin_loan);
@ -212,7 +212,7 @@ pub fn charge_maybe_fees(
// loan origination fees
let pc_token_account = account.token_get_mut(pc_bank.token_index)?.0;
let pc_token_native = pc_token_account.native(&pc_bank);
let pc_token_native = pc_token_account.native(pc_bank);
if pc_token_native.is_negative() {
let actualized_loan = pc_token_native.abs().min(maybe_actualized_pc_loan);

View File

@ -113,7 +113,7 @@ pub fn token_deposit(ctx: Context<TokenDeposit>, amount: u64) -> Result<()> {
emit!(DepositLog {
mango_account: ctx.accounts.account.key(),
signer: ctx.accounts.token_authority.key(),
token_index: token_index,
token_index,
quantity: amount,
price: oracle_price.to_bits(),
});

View File

@ -11,7 +11,7 @@ use crate::error::*;
use crate::state::*;
/// Serum padding is "serum" + data + "padding"
fn strip_dex_padding<'a>(data: &[u8]) -> Result<&[u8]> {
fn strip_dex_padding(data: &[u8]) -> Result<&[u8]> {
require!(data.len() >= 12, MangoError::SomeError);
Ok(&data[5..data.len() - 7])
}
@ -117,7 +117,7 @@ pub fn load_open_orders_ref<'a>(
Ok(Ref::map(strip_dex_padding_ref(acc)?, bytemuck::from_bytes))
}
pub fn load_open_orders<'a>(acc: &impl AccountReader) -> Result<&serum_dex::state::OpenOrders> {
pub fn load_open_orders(acc: &impl AccountReader) -> Result<&serum_dex::state::OpenOrders> {
Ok(bytemuck::from_bytes(strip_dex_padding(acc.data())?))
}

View File

@ -97,7 +97,7 @@ pub struct Bank {
}
const_assert_eq!(
size_of::<Bank>(),
32 + 16 + 32 * 3 + 16 + 16 * 6 + 8 * 2 + 16 * 16 + 8 * 2 + 2 + 1 * 2 + 4 + 256
32 + 16 + 32 * 3 + 16 + 16 * 6 + 8 * 2 + 16 * 16 + 8 * 2 + 2 + 1 + 1 + 4 + 256
);
const_assert_eq!(size_of::<Bank>() % 8, 0);

View File

@ -64,7 +64,7 @@ impl<T: ?Sized> DerefOrBorrow<T> for &T {
impl<T: Sized> DerefOrBorrow<[T]> for Vec<T> {
fn deref_or_borrow(&self) -> &[T] {
&self
self
}
}
@ -76,13 +76,13 @@ impl<T: ?Sized> DerefOrBorrow<T> for &mut T {
impl<'a, T: ?Sized> DerefOrBorrow<T> for Ref<'a, T> {
fn deref_or_borrow(&self) -> &T {
&self
self
}
}
impl<'a, T: ?Sized> DerefOrBorrow<T> for RefMut<'a, T> {
fn deref_or_borrow(&self) -> &T {
&self
self
}
}
@ -153,7 +153,7 @@ impl<'info, D: DynamicAccountType> AccountLoaderDynamic<'info, D> {
}
/// Returns a Ref to the account data structure for reading.
pub fn load_fixed<'a>(&'a self) -> Result<Ref<'a, D::Fixed>> {
pub fn load_fixed(&self) -> Result<Ref<D::Fixed>> {
let data = self.acc_info.try_borrow_data()?;
let fixed = Ref::map(data, |d| {
bytemuck::from_bytes(&d[8..8 + size_of::<D::Fixed>()])
@ -161,10 +161,9 @@ impl<'info, D: DynamicAccountType> AccountLoaderDynamic<'info, D> {
Ok(fixed)
}
#[allow(clippy::type_complexity)]
/// Returns a Ref to the account data structure for reading.
pub fn load<'a>(
&'a self,
) -> Result<DynamicAccount<D::Header, Ref<'a, D::Fixed>, Ref<'a, [u8]>>> {
pub fn load(&self) -> Result<DynamicAccount<D::Header, Ref<D::Fixed>, Ref<[u8]>>> {
let data = self.acc_info.try_borrow_data()?;
let header = D::Header::from_bytes(&data[8 + size_of::<D::Fixed>()..])?;
let (_, data) = Ref::map_split(data, |d| d.split_at(8));
@ -176,9 +175,8 @@ impl<'info, D: DynamicAccountType> AccountLoaderDynamic<'info, D> {
})
}
pub fn load_init<'a>(
&'a self,
) -> Result<DynamicAccount<D::Header, RefMut<'a, D::Fixed>, RefMut<'a, [u8]>>> {
#[allow(clippy::type_complexity)]
pub fn load_init(&self) -> Result<DynamicAccount<D::Header, RefMut<D::Fixed>, RefMut<[u8]>>> {
if !self.acc_info.is_writable {
return Err(ErrorCode::AccountNotMutable.into());
}
@ -202,9 +200,8 @@ impl<'info, D: DynamicAccountType> AccountLoaderDynamic<'info, D> {
}
/// Returns a Ref to the account data structure for reading.
pub fn load_mut<'a>(
&'a self,
) -> Result<DynamicAccount<D::Header, RefMut<'a, D::Fixed>, RefMut<'a, [u8]>>> {
#[allow(clippy::type_complexity)]
pub fn load_mut(&self) -> Result<DynamicAccount<D::Header, RefMut<D::Fixed>, RefMut<[u8]>>> {
if !self.acc_info.is_writable {
return Err(ErrorCode::AccountNotMutable.into());
}

View File

@ -35,7 +35,7 @@ pub struct Group {
pub reserved: [u8; 256],
}
const_assert_eq!(size_of::<Group>(), 32 * 5 + 4 + 4 + 1 * 2 + 6 + 256);
const_assert_eq!(size_of::<Group>(), 32 * 5 + 4 + 4 + 1 + 1 + 6 + 256);
const_assert_eq!(size_of::<Group>() % 8, 0);
impl Group {

View File

@ -73,8 +73,8 @@ pub fn new_fixed_order_account_retriever<'a, 'info>(
Ok(FixedOrderAccountRetriever {
ais: ais
.into_iter()
.map(|ai| AccountInfoRef::borrow(ai))
.iter()
.map(AccountInfoRef::borrow)
.collect::<Result<Vec<_>>>()?,
n_banks: active_token_len,
begin_perp: cm!(active_token_len * 2),
@ -93,11 +93,7 @@ impl<T: KeyedAccountReader> FixedOrderAccountRetriever<T> {
fn oracle_price(&self, account_index: usize, bank: &Bank) -> Result<I80F48> {
let oracle = &self.ais[cm!(self.n_banks + account_index)];
require_keys_eq!(bank.oracle, *oracle.key());
Ok(oracle_price(
oracle,
bank.oracle_config.conf_filter,
bank.mint_decimals,
)?)
oracle_price(oracle, bank.oracle_config.conf_filter, bank.mint_decimals)
}
}
@ -238,8 +234,8 @@ impl<'a, 'info> ScanningAccountRetriever<'a, 'info> {
Ok(Self {
ais: ais
.into_iter()
.map(|ai| AccountInfoRefMut::borrow(ai))
.iter()
.map(AccountInfoRefMut::borrow)
.collect::<Result<Vec<_>>>()?,
token_index_map,
perp_index_map,
@ -270,6 +266,7 @@ impl<'a, 'info> ScanningAccountRetriever<'a, 'info> {
.ok_or_else(|| error_msg!("perp market index {} not found", perp_market_index))?)
}
#[allow(clippy::type_complexity)]
pub fn banks_mut_and_oracles(
&mut self,
token_index1: TokenIndex,
@ -393,8 +390,8 @@ pub fn compute_health_from_fixed_accounts(
let retriever = FixedOrderAccountRetriever {
ais: ais
.into_iter()
.map(|ai| AccountInfoRef::borrow(ai))
.iter()
.map(AccountInfoRef::borrow)
.collect::<Result<Vec<_>>>()?,
n_banks: active_token_len,
begin_perp: cm!(active_token_len * 2),
@ -799,7 +796,7 @@ pub fn new_health_cache(
// converts the token value to the basis token value for health computations
// TODO: health basis token == USDC?
let native = position.native(&bank);
let native = position.native(bank);
token_infos.push(TokenInfo {
token_index: bank.token_index,

View File

@ -209,7 +209,7 @@ fn test_dynamic_offsets() {
.resize(8, PerpOpenOrders::default());
assert_eq!(
8 + AnchorSerialize::try_to_vec(&account).unwrap().len(),
MangoAccount::space(AccountSize::Large.try_into().unwrap())
MangoAccount::space(AccountSize::Large)
);
}
@ -392,7 +392,7 @@ impl MangoAccountValue {
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
let (fixed, dynamic) = bytes.split_at(size_of::<MangoAccountFixed>());
Ok(Self {
fixed: *bytemuck::from_bytes(&fixed),
fixed: *bytemuck::from_bytes(fixed),
header: MangoAccountDynamicHeader::from_bytes(dynamic)?,
dynamic: dynamic.to_vec(),
})
@ -404,7 +404,7 @@ impl<'a> MangoAccountRefWithHeader<'a> {
pub fn from_bytes(bytes: &'a [u8]) -> Result<Self> {
let (fixed, dynamic) = bytes.split_at(size_of::<MangoAccountFixed>());
Ok(Self {
fixed: bytemuck::from_bytes(&fixed),
fixed: bytemuck::from_bytes(fixed),
header: MangoAccountDynamicHeader::from_bytes(dynamic)?,
dynamic,
})
@ -550,7 +550,7 @@ impl<
self.fixed().is_bankrupt()
}
pub fn borrow<'b>(&'b self) -> DynamicAccountRef<'b, MangoAccount> {
pub fn borrow(&self) -> DynamicAccountRef<MangoAccount> {
DynamicAccount {
header: self.header(),
fixed: self.fixed(),
@ -579,7 +579,7 @@ impl<
self.dynamic.deref_or_borrow_mut()
}
pub fn borrow_mut<'b>(&'b mut self) -> DynamicAccountRefMut<'b, MangoAccount> {
pub fn borrow_mut(&mut self) -> DynamicAccountRefMut<MangoAccount> {
DynamicAccount {
header: self.header.deref_or_borrow_mut(),
fixed: self.fixed.deref_or_borrow_mut(),