clippy: add from_iter_instead_of_collect warn (#11666)

This commit is contained in:
Thomas Coratger
2024-10-11 19:56:33 +02:00
committed by GitHub
parent d8b7f6014f
commit bca11aa2dd
12 changed files with 85 additions and 80 deletions

View File

@@ -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"

View File

@@ -52,7 +52,7 @@ impl Chain {
execution_outcome: ExecutionOutcome,
trie_updates: Option<TrieUpdates>,
) -> 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::<BTreeMap<_, _>>();
debug_assert!(!blocks.is_empty(), "Chain should have at least one block");
Self { blocks, execution_outcome, trie_updates }

View File

@@ -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(),
}),
}
}

View File

@@ -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
}

View File

@@ -2914,10 +2914,10 @@ impl<TX: DbTxMut + DbTx, Spec: Send + Sync> HashingWriter for DatabaseProvider<T
map
});
let hashed_storage_keys =
HashMap::from_iter(hashed_storages.iter().map(|(hashed_address, entries)| {
(*hashed_address, BTreeSet::from_iter(entries.keys().copied()))
}));
let hashed_storage_keys = hashed_storages
.iter()
.map(|(hashed_address, entries)| (*hashed_address, entries.keys().copied().collect()))
.collect();
let mut hashed_storage_cursor = self.tx.cursor_dup_write::<tables::HashedStorages>()?;
// Hash the address and key and apply them to HashedStorage (if Storage is None

View File

@@ -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)

View File

@@ -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();
}
}
}

View File

@@ -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);

View File

@@ -244,22 +244,24 @@ impl<TX: DbTx> DatabaseHashedPostState<TX> 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 })
}

View File

@@ -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::<Vec<_>>();
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::<Vec<_>>();
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::<Vec<_>>();
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::<Vec<_>>();
// 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::<Vec<_>>();
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::<Vec<_>>();
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::<BTreeMap<_, _>>();
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::<BTreeMap<_, _>>();
let post_state_storage = (11..21)
.map(|key| (B256::with_last_byte(key), U256::from(key)))
.collect::<BTreeMap<_, _>>();
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::<BTreeMap<_, _>>(); // 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::<BTreeMap<_, _>>();
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::<BTreeMap<_, _>>();
let post_state_storage = (11..21)
.map(|key| (B256::with_last_byte(key), U256::from(key)))
.collect::<BTreeMap<_, _>>();
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::<BTreeMap<_, _>>();
let db = create_test_rw_db();
db.update(|tx| {

View File

@@ -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();

View File

@@ -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::<Vec<_>>();
storage_nodes.sort_unstable();
storage_nodes.serialize(serializer)
}