From bca11aa2dd2ad2a0fdb43853ca1c5047fcb43e8f Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Fri, 11 Oct 2024 19:56:33 +0200 Subject: [PATCH] clippy: add `from_iter_instead_of_collect` warn (#11666) --- Cargo.toml | 1 + crates/evm/execution-types/src/chain.rs | 2 +- crates/rpc/rpc-builder/src/metrics.rs | 9 ++-- crates/rpc/rpc/src/debug.rs | 6 +-- .../src/providers/database/provider.rs | 8 ++-- .../storage/provider/src/test_utils/blocks.rs | 47 +++++++++---------- crates/transaction-pool/benches/reorder.rs | 10 ++-- crates/transaction-pool/src/maintain.rs | 2 +- crates/trie/db/src/state.rs | 32 +++++++------ crates/trie/db/tests/post_state.rs | 44 +++++++++-------- crates/trie/trie/src/proof.rs | 2 +- crates/trie/trie/src/updates.rs | 2 +- 12 files changed, 85 insertions(+), 80 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index db115c89cc..efae22f8ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -183,6 +183,7 @@ equatable_if_let = "warn" explicit_into_iter_loop = "warn" explicit_iter_loop = "warn" flat_map_option = "warn" +from_iter_instead_of_collect = "warn" if_not_else = "warn" implicit_clone = "warn" imprecise_flops = "warn" diff --git a/crates/evm/execution-types/src/chain.rs b/crates/evm/execution-types/src/chain.rs index 25bc39ea32..30f1f4cd2f 100644 --- a/crates/evm/execution-types/src/chain.rs +++ b/crates/evm/execution-types/src/chain.rs @@ -52,7 +52,7 @@ impl Chain { execution_outcome: ExecutionOutcome, trie_updates: Option, ) -> Self { - let blocks = BTreeMap::from_iter(blocks.into_iter().map(|b| (b.number, b))); + let blocks = blocks.into_iter().map(|b| (b.number, b)).collect::>(); debug_assert!(!blocks.is_empty(), "Chain should have at least one block"); Self { blocks, execution_outcome, trie_updates } diff --git a/crates/rpc/rpc-builder/src/metrics.rs b/crates/rpc/rpc-builder/src/metrics.rs index 08fd388985..57283ded37 100644 --- a/crates/rpc/rpc-builder/src/metrics.rs +++ b/crates/rpc/rpc-builder/src/metrics.rs @@ -30,9 +30,12 @@ impl RpcRequestMetrics { Self { inner: Arc::new(RpcServerMetricsInner { connection_metrics: transport.connection_metrics(), - call_metrics: HashMap::from_iter(module.method_names().map(|method| { - (method, RpcServerCallMetrics::new_with_labels(&[("method", method)])) - })), + call_metrics: module + .method_names() + .map(|method| { + (method, RpcServerCallMetrics::new_with_labels(&[("method", method)])) + }) + .collect(), }), } } diff --git a/crates/rpc/rpc/src/debug.rs b/crates/rpc/rpc/src/debug.rs index 738b91ad0e..fbef6f7a7e 100644 --- a/crates/rpc/rpc/src/debug.rs +++ b/crates/rpc/rpc/src/debug.rs @@ -663,11 +663,7 @@ where let state = state_provider.witness(Default::default(), hashed_state).map_err(Into::into)?; - Ok(ExecutionWitness { - state: HashMap::from_iter(state.into_iter()), - codes, - keys: Some(keys), - }) + Ok(ExecutionWitness { state: state.into_iter().collect(), codes, keys: Some(keys) }) }) .await } diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index c9b0af3d33..33fed1e80c 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -2914,10 +2914,10 @@ impl HashingWriter for DatabaseProvider()?; // Hash the address and key and apply them to HashedStorage (if Storage is None diff --git a/crates/storage/provider/src/test_utils/blocks.rs b/crates/storage/provider/src/test_utils/blocks.rs index daed906646..f7928319b4 100644 --- a/crates/storage/provider/src/test_utils/blocks.rs +++ b/crates/storage/provider/src/test_utils/blocks.rs @@ -324,11 +324,10 @@ fn block3( ) .state_storage( address, - HashMap::from_iter( - slot_range - .clone() - .map(|slot| (U256::from(slot), (U256::ZERO, U256::from(slot)))), - ), + slot_range + .clone() + .map(|slot| (U256::from(slot), (U256::ZERO, U256::from(slot)))) + .collect(), ) .revert_account_info(number, address, Some(None)) .revert_storage(number, address, Vec::new()); @@ -393,20 +392,18 @@ fn block4( ) .state_storage( address, - HashMap::from_iter( - slot_range.clone().map(|slot| { - (U256::from(slot), (U256::from(slot), U256::from(slot * 2))) - }), - ), + slot_range + .clone() + .map(|slot| (U256::from(slot), (U256::from(slot), U256::from(slot * 2)))) + .collect(), ) } else { bundle_state_builder.state_address(address).state_storage( address, - HashMap::from_iter( - slot_range - .clone() - .map(|slot| (U256::from(slot), (U256::from(slot), U256::ZERO))), - ), + slot_range + .clone() + .map(|slot| (U256::from(slot), (U256::from(slot), U256::ZERO))) + .collect(), ) }; // record previous account info @@ -423,7 +420,7 @@ fn block4( .revert_storage( number, address, - Vec::from_iter(slot_range.clone().map(|slot| (U256::from(slot), U256::from(slot)))), + slot_range.clone().map(|slot| (U256::from(slot), U256::from(slot))).collect(), ); } let execution_outcome = ExecutionOutcome::new( @@ -485,12 +482,11 @@ fn block5( ) .state_storage( address, - HashMap::from_iter( - slot_range - .clone() - .take(50) - .map(|slot| (U256::from(slot), (U256::from(slot), U256::from(slot * 4)))), - ), + slot_range + .clone() + .take(50) + .map(|slot| (U256::from(slot), (U256::from(slot), U256::from(slot * 4)))) + .collect(), ); bundle_state_builder = if idx % 2 == 0 { bundle_state_builder @@ -506,9 +502,10 @@ fn block5( .revert_storage( number, address, - Vec::from_iter( - slot_range.clone().map(|slot| (U256::from(slot), U256::from(slot * 2))), - ), + slot_range + .clone() + .map(|slot| (U256::from(slot), U256::from(slot * 2))) + .collect(), ) } else { bundle_state_builder.revert_address(number, address) diff --git a/crates/transaction-pool/benches/reorder.rs b/crates/transaction-pool/benches/reorder.rs index 1836ce40c0..9fc2162975 100644 --- a/crates/transaction-pool/benches/reorder.rs +++ b/crates/transaction-pool/benches/reorder.rs @@ -206,10 +206,12 @@ mod implementations { self.base_fee = Some(base_fee); let drained = self.inner.drain(); - self.inner = BinaryHeap::from_iter(drained.map(|mock| { - let priority = mock.tx.effective_tip_per_gas(base_fee).expect("set"); - MockTransactionWithPriority { tx: mock.tx, priority } - })); + self.inner = drained + .map(|mock| { + let priority = mock.tx.effective_tip_per_gas(base_fee).expect("set"); + MockTransactionWithPriority { tx: mock.tx, priority } + }) + .collect(); } } } diff --git a/crates/transaction-pool/src/maintain.rs b/crates/transaction-pool/src/maintain.rs index 66b9861473..36c7067d4a 100644 --- a/crates/transaction-pool/src/maintain.rs +++ b/crates/transaction-pool/src/maintain.rs @@ -490,7 +490,7 @@ impl MaintainedPoolState { } } -/// A unique `ChangedAccount` identified by its address that can be used for deduplication +/// A unique [`ChangedAccount`] identified by its address that can be used for deduplication #[derive(Eq)] struct ChangedAccountEntry(ChangedAccount); diff --git a/crates/trie/db/src/state.rs b/crates/trie/db/src/state.rs index 5acb9e0d1b..4d46183dfd 100644 --- a/crates/trie/db/src/state.rs +++ b/crates/trie/db/src/state.rs @@ -244,22 +244,24 @@ impl DatabaseHashedPostState for HashedPostState { } } - let hashed_accounts = HashMap::from_iter( - accounts.into_iter().map(|(address, info)| (keccak256(address), info)), - ); + let hashed_accounts = + accounts.into_iter().map(|(address, info)| (keccak256(address), info)).collect(); - let hashed_storages = HashMap::from_iter(storages.into_iter().map(|(address, storage)| { - ( - keccak256(address), - HashedStorage::from_iter( - // The `wiped` flag indicates only whether previous storage entries - // should be looked up in db or not. For reverts it's a noop since all - // wiped changes had been written as storage reverts. - false, - storage.into_iter().map(|(slot, value)| (keccak256(slot), value)), - ), - ) - })); + let hashed_storages = storages + .into_iter() + .map(|(address, storage)| { + ( + keccak256(address), + HashedStorage::from_iter( + // The `wiped` flag indicates only whether previous storage entries + // should be looked up in db or not. For reverts it's a noop since all + // wiped changes had been written as storage reverts. + false, + storage.into_iter().map(|(slot, value)| (keccak256(slot), value)), + ), + ) + }) + .collect(); Ok(Self { accounts: hashed_accounts, storages: hashed_storages }) } diff --git a/crates/trie/db/tests/post_state.rs b/crates/trie/db/tests/post_state.rs index ceadf7cde5..ce6f10d76a 100644 --- a/crates/trie/db/tests/post_state.rs +++ b/crates/trie/db/tests/post_state.rs @@ -55,7 +55,7 @@ fn assert_storage_cursor_order( #[test] fn post_state_only_accounts() { let accounts = - Vec::from_iter((1..11).map(|key| (B256::with_last_byte(key), Account::default()))); + (1..11).map(|key| (B256::with_last_byte(key), Account::default())).collect::>(); let mut hashed_post_state = HashedPostState::default(); for (hashed_address, account) in &accounts { @@ -73,7 +73,7 @@ fn post_state_only_accounts() { #[test] fn db_only_accounts() { let accounts = - Vec::from_iter((1..11).map(|key| (B256::with_last_byte(key), Account::default()))); + (1..11).map(|key| (B256::with_last_byte(key), Account::default())).collect::>(); let db = create_test_rw_db(); db.update(|tx| { @@ -96,7 +96,7 @@ fn db_only_accounts() { fn account_cursor_correct_order() { // odd keys are in post state, even keys are in db let accounts = - Vec::from_iter((1..111).map(|key| (B256::with_last_byte(key), Account::default()))); + (1..111).map(|key| (B256::with_last_byte(key), Account::default())).collect::>(); let db = create_test_rw_db(); db.update(|tx| { @@ -121,9 +121,9 @@ fn account_cursor_correct_order() { fn removed_accounts_are_discarded() { // odd keys are in post state, even keys are in db let accounts = - Vec::from_iter((1..111).map(|key| (B256::with_last_byte(key), Account::default()))); + (1..111).map(|key| (B256::with_last_byte(key), Account::default())).collect::>(); // accounts 5, 9, 11 should be considered removed from post state - let removed_keys = Vec::from_iter([5, 9, 11].into_iter().map(B256::with_last_byte)); + let removed_keys = [5, 9, 11].into_iter().map(B256::with_last_byte).collect::>(); let db = create_test_rw_db(); db.update(|tx| { @@ -150,9 +150,9 @@ fn removed_accounts_are_discarded() { #[test] fn post_state_accounts_take_precedence() { - let accounts = Vec::from_iter((1..10).map(|key| { - (B256::with_last_byte(key), Account { nonce: key as u64, ..Default::default() }) - })); + let accounts = (1..10) + .map(|key| (B256::with_last_byte(key), Account { nonce: key as u64, ..Default::default() })) + .collect::>(); let db = create_test_rw_db(); db.update(|tx| { @@ -224,7 +224,7 @@ fn storage_is_empty() { } let db_storage = - BTreeMap::from_iter((0..10).map(|key| (B256::with_last_byte(key), U256::from(key)))); + (0..10).map(|key| (B256::with_last_byte(key), U256::from(key))).collect::>(); db.update(|tx| { for (slot, value) in &db_storage { // insert zero value accounts to the database @@ -299,9 +299,10 @@ fn storage_is_empty() { fn storage_cursor_correct_order() { let address = B256::random(); let db_storage = - BTreeMap::from_iter((1..11).map(|key| (B256::with_last_byte(key), U256::from(key)))); - let post_state_storage = - BTreeMap::from_iter((11..21).map(|key| (B256::with_last_byte(key), U256::from(key)))); + (1..11).map(|key| (B256::with_last_byte(key), U256::from(key))).collect::>(); + let post_state_storage = (11..21) + .map(|key| (B256::with_last_byte(key), U256::from(key))) + .collect::>(); let db = create_test_rw_db(); db.update(|tx| { @@ -334,10 +335,12 @@ fn storage_cursor_correct_order() { fn zero_value_storage_entries_are_discarded() { let address = B256::random(); let db_storage = - BTreeMap::from_iter((0..10).map(|key| (B256::with_last_byte(key), U256::from(key)))); // every even number is changed to zero value - let post_state_storage = BTreeMap::from_iter((0..10).map(|key| { - (B256::with_last_byte(key), if key % 2 == 0 { U256::ZERO } else { U256::from(key) }) - })); + (0..10).map(|key| (B256::with_last_byte(key), U256::from(key))).collect::>(); // every even number is changed to zero value + let post_state_storage = (0..10) + .map(|key| { + (B256::with_last_byte(key), if key % 2 == 0 { U256::ZERO } else { U256::from(key) }) + }) + .collect::>(); let db = create_test_rw_db(); db.update(|tx| { @@ -371,9 +374,10 @@ fn zero_value_storage_entries_are_discarded() { fn wiped_storage_is_discarded() { let address = B256::random(); let db_storage = - BTreeMap::from_iter((1..11).map(|key| (B256::with_last_byte(key), U256::from(key)))); - let post_state_storage = - BTreeMap::from_iter((11..21).map(|key| (B256::with_last_byte(key), U256::from(key)))); + (1..11).map(|key| (B256::with_last_byte(key), U256::from(key))).collect::>(); + let post_state_storage = (11..21) + .map(|key| (B256::with_last_byte(key), U256::from(key))) + .collect::>(); let db = create_test_rw_db(); db.update(|tx| { @@ -404,7 +408,7 @@ fn wiped_storage_is_discarded() { fn post_state_storages_take_precedence() { let address = B256::random(); let storage = - BTreeMap::from_iter((1..10).map(|key| (B256::with_last_byte(key), U256::from(key)))); + (1..10).map(|key| (B256::with_last_byte(key), U256::from(key))).collect::>(); let db = create_test_rw_db(); db.update(|tx| { diff --git a/crates/trie/trie/src/proof.rs b/crates/trie/trie/src/proof.rs index d31d63fd9a..e99d686aca 100644 --- a/crates/trie/trie/src/proof.rs +++ b/crates/trie/trie/src/proof.rs @@ -100,7 +100,7 @@ where let walker = TrieWalker::new(trie_cursor, prefix_set.freeze()); // Create a hash builder to rebuild the root node since it is not available in the database. - let retainer = ProofRetainer::from_iter(targets.keys().map(Nibbles::unpack)); + let retainer = targets.keys().map(Nibbles::unpack).collect(); let mut hash_builder = HashBuilder::default().with_proof_retainer(retainer); let mut storages = HashMap::default(); diff --git a/crates/trie/trie/src/updates.rs b/crates/trie/trie/src/updates.rs index 03e80cf52e..6d1bcab63d 100644 --- a/crates/trie/trie/src/updates.rs +++ b/crates/trie/trie/src/updates.rs @@ -236,7 +236,7 @@ mod serde_nibbles_set { S: Serializer, { let mut storage_nodes = - Vec::from_iter(map.iter().map(|elem| alloy_primitives::hex::encode(elem.pack()))); + map.iter().map(|elem| alloy_primitives::hex::encode(elem.pack())).collect::>(); storage_nodes.sort_unstable(); storage_nodes.serialize(serializer) }