From 147bfa0920b533d4e4b5d5f014cf7f9b2abfada9 Mon Sep 17 00:00:00 2001 From: dorimedini-starkware Date: Thu, 22 Feb 2024 16:02:19 +0200 Subject: [PATCH] chore: remove some 'as' conversions and enforce no 'as' conversions (#1575) * chore: remove final f64s Signed-off-by: Dori Medini * chore: remove some 'as' conversions and enforce no 'as' conversions Signed-off-by: Dori Medini --- Cargo.toml | 3 +++ crates/blockifier/src/fee/fee_test.rs | 9 +++++---- crates/blockifier/src/test_utils.rs | 1 + crates/blockifier/src/utils.rs | 1 + 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5334bce675..584847755b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,3 +60,6 @@ future-incompatible = "deny" nonstandard-style = "deny" rust-2018-idioms = "deny" unused = "deny" + +[workspace.lints.clippy] +as_conversions = "deny" diff --git a/crates/blockifier/src/fee/fee_test.rs b/crates/blockifier/src/fee/fee_test.rs index bbf0c7e34a..760bdd5283 100644 --- a/crates/blockifier/src/fee/fee_test.rs +++ b/crates/blockifier/src/fee/fee_test.rs @@ -20,6 +20,7 @@ use crate::test_utils::{CairoVersion, BALANCE}; use crate::transaction::errors::TransactionFeeError; use crate::transaction::objects::{GasVector, ResourcesMapping}; use crate::transaction::test_utils::{account_invoke_tx, l1_resource_bounds}; +use crate::utils::u128_from_usize; use crate::versioned_constants::VersionedConstants; fn get_vm_resource_usage() -> ResourcesMapping { @@ -42,7 +43,7 @@ fn test_calculate_l1_gas_by_vm_usage() { // Verify calculation - in our case, n_steps is the heaviest resource. let l1_gas_by_vm_usage = vm_resource_usage.0.get(constants::N_STEPS_RESOURCE).unwrap(); assert_eq!( - GasVector { l1_gas: *l1_gas_by_vm_usage as u128, l1_data_gas: 0 }, + GasVector { l1_gas: u128_from_usize(*l1_gas_by_vm_usage), l1_data_gas: 0 }, calculate_l1_gas_by_vm_usage(&versioned_constants, &vm_resource_usage).unwrap() ); @@ -102,11 +103,11 @@ fn test_discounted_gas_overdraft( if expect_failure { let error = report.error().unwrap(); - let expected_actual_amount = - l1_gas_used as u128 + (l1_data_gas_used as u128 * data_gas_price) / gas_price; + let expected_actual_amount = u128_from_usize(l1_gas_used) + + (u128_from_usize(l1_data_gas_used) * data_gas_price) / gas_price; assert_matches!( error, FeeCheckError::MaxL1GasAmountExceeded { max_amount, actual_amount } - if max_amount == gas_bound as u128 && actual_amount == expected_actual_amount + if max_amount == u128::from(gas_bound) && actual_amount == expected_actual_amount ) } else { assert_matches!(report.error(), None); diff --git a/crates/blockifier/src/test_utils.rs b/crates/blockifier/src/test_utils.rs index 0a5ec13b10..d0504e8640 100644 --- a/crates/blockifier/src/test_utils.rs +++ b/crates/blockifier/src/test_utils.rs @@ -109,6 +109,7 @@ pub fn test_erc20_faulty_account_balance_key() -> StorageKey { // The max_fee / resource bounds used for txs in this test. pub const MAX_L1_GAS_AMOUNT: u64 = 1000000; +#[allow(clippy::as_conversions)] pub const MAX_L1_GAS_AMOUNT_U128: u128 = MAX_L1_GAS_AMOUNT as u128; pub const MAX_L1_GAS_PRICE: u128 = DEFAULT_STRK_L1_GAS_PRICE; pub const MAX_RESOURCE_COMMITMENT: u128 = MAX_L1_GAS_AMOUNT_U128 * MAX_L1_GAS_PRICE; diff --git a/crates/blockifier/src/utils.rs b/crates/blockifier/src/utils.rs index 2a1e669394..2b031ce592 100644 --- a/crates/blockifier/src/utils.rs +++ b/crates/blockifier/src/utils.rs @@ -19,6 +19,7 @@ where /// Returns the max value of two constants, at compile time. pub const fn const_max(a: u128, b: u128) -> u128 { + #[allow(clippy::as_conversions)] [a, b][(a < b) as usize] }