Skip to content

Commit 7c4bfe8

Browse files
decofezerosnacks
andauthored
clippy: enable needless_for_each (manual) (foundry-rs#14301)
clippy: enable `needless_for_each` Amp-Thread-ID: https://ampcode.com/threads/T-019d8707-07ff-77cf-b7f2-5dc0f64200ec Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com>
1 parent b5854b9 commit 7c4bfe8

12 files changed

Lines changed: 52 additions & 47 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ mutex_integer = "warn"
7373
naive_bytecount = "warn"
7474
needless_bitwise_bool = "warn"
7575
needless_continue = "warn"
76+
needless_for_each = "warn"
7677
nonstandard_macro_braces = "warn"
7778
option_as_ref_cloned = "warn"
7879
path_buf_push_overwrite = "warn"

crates/cast/src/cmd/storage.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,11 @@ fn print_storage(layout: StorageLayout, values: Vec<StorageValue>) -> Result<()>
374374
fn add_storage_layout_output<C: Compiler<CompilerContract = Contract>>(project: &mut Project<C>) {
375375
project.artifacts.additional_values.storage_layout = true;
376376
project.update_output_selection(|selection| {
377-
selection.0.values_mut().for_each(|contract_selection| {
378-
contract_selection
379-
.values_mut()
380-
.for_each(|selection| selection.push("storageLayout".to_string()))
381-
});
377+
for contract_selection in selection.0.values_mut() {
378+
for selection in contract_selection.values_mut() {
379+
selection.push("storageLayout".to_string());
380+
}
381+
}
382382
})
383383
}
384384

crates/cheatcodes/src/inspector.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,13 +1468,12 @@ impl<FEN: FoundryEvmNetwork> Inspector<FoundryContextFor<'_, FEN>> for Cheatcode
14681468
// Update the reverted status of all deeper calls if this call reverted, in
14691469
// accordance with EVM behavior
14701470
if outcome.result.is_revert() {
1471-
last_recorded_depth.iter_mut().for_each(|element| {
1471+
for element in &mut *last_recorded_depth {
14721472
element.reverted = true;
1473-
element
1474-
.storageAccesses
1475-
.iter_mut()
1476-
.for_each(|storage_access| storage_access.reverted = true);
1477-
})
1473+
for storage_access in &mut element.storageAccesses {
1474+
storage_access.reverted = true;
1475+
}
1476+
}
14781477
}
14791478

14801479
if let Some(call_access) = last_recorded_depth.first_mut() {
@@ -1914,13 +1913,12 @@ impl<FEN: FoundryEvmNetwork> Inspector<FoundryContextFor<'_, FEN>> for Cheatcode
19141913
// Update the reverted status of all deeper calls if this call reverted, in
19151914
// accordance with EVM behavior
19161915
if outcome.result.is_revert() {
1917-
last_depth.iter_mut().for_each(|element| {
1916+
for element in &mut *last_depth {
19181917
element.reverted = true;
1919-
element
1920-
.storageAccesses
1921-
.iter_mut()
1922-
.for_each(|storage_access| storage_access.reverted = true);
1923-
})
1918+
for storage_access in &mut element.storageAccesses {
1919+
storage_access.reverted = true;
1920+
}
1921+
}
19241922
}
19251923

19261924
if let Some(create_access) = last_depth.first_mut() {

crates/cli/src/opts/dependency.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ mod tests {
162162

163163
#[test]
164164
fn parses_dependencies() {
165-
[
165+
for (input, expected_path, expected_tag, expected_alias) in [
166166
("gakonst/lootloose", "https://github.com/gakonst/lootloose", None, None),
167167
("github.com/gakonst/lootloose", "https://github.com/gakonst/lootloose", None, None),
168168
(
@@ -239,15 +239,13 @@ mod tests {
239239
Some("v1"),
240240
Some("loot"),
241241
),
242-
]
243-
.iter()
244-
.for_each(|(input, expected_path, expected_tag, expected_alias)| {
242+
] {
245243
let dep = Dependency::from_str(input).unwrap();
246244
assert_eq!(dep.url, Some(expected_path.to_string()));
247245
assert_eq!(dep.tag, expected_tag.map(ToString::to_string));
248246
assert_eq!(dep.name, "lootloose");
249247
assert_eq!(dep.alias, expected_alias.map(ToString::to_string));
250-
});
248+
}
251249
}
252250

253251
#[test]
@@ -267,28 +265,26 @@ mod tests {
267265

268266
#[test]
269267
fn parses_contract_info() {
270-
[
268+
for (input, expected_path, expected_name) in [
271269
(
272270
"src/contracts/Contracts.sol:Contract",
273271
Some("src/contracts/Contracts.sol"),
274272
"Contract",
275273
),
276274
("Contract", None, "Contract"),
277-
]
278-
.iter()
279-
.for_each(|(input, expected_path, expected_name)| {
275+
] {
280276
let contract = ContractInfo::from_str(input).unwrap();
281277
assert_eq!(contract.path, expected_path.map(ToString::to_string));
282278
assert_eq!(contract.name, expected_name.to_string());
283-
});
279+
}
284280
}
285281

286282
#[test]
287283
fn contract_info_should_reject_without_name() {
288-
["src/contracts/", "src/contracts/Contracts.sol"].iter().for_each(|input| {
284+
for input in ["src/contracts/", "src/contracts/Contracts.sol"] {
289285
let contract = ContractInfo::from_str(input);
290-
assert!(contract.is_err())
291-
});
286+
assert!(contract.is_err());
287+
}
292288
}
293289

294290
#[test]

crates/evm/core/src/fork/multi.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,9 @@ impl<
509509
std::thread::Builder::new()
510510
.name("flusher".into())
511511
.spawn(move || {
512-
forks.into_iter().for_each(|fork| fork.flush_cache());
512+
for fork in forks {
513+
fork.flush_cache();
514+
}
513515
})
514516
.expect("failed to spawn thread");
515517
}

crates/evm/fuzz/src/strategies/mutators.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,9 @@ fn apply_scale_to_bytes(bytes: &mut [u8], scale_factor: f64) -> Option<()> {
485485
let scaled = (byte_val + carry_down * 256.0) * scale_factor;
486486

487487
if i == 0 && scaled >= 256.0 {
488-
bytes.iter_mut().for_each(|b| *b = 0xFF);
488+
for b in bytes.iter_mut() {
489+
*b = 0xFF;
490+
}
489491
return Some(());
490492
}
491493

@@ -500,7 +502,9 @@ fn apply_scale_to_bytes(bytes: &mut [u8], scale_factor: f64) -> Option<()> {
500502
j -= 1;
501503
let new_val = bytes[j] as f64 + carry_up;
502504
if j == 0 && new_val >= 256.0 {
503-
bytes.iter_mut().for_each(|b| *b = 0xFF);
505+
for b in bytes.iter_mut() {
506+
*b = 0xFF;
507+
}
504508
return Some(());
505509
}
506510
bytes[j] = (new_val % 256.0).floor() as u8;

crates/forge/src/cmd/clone.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,8 @@ mod tests {
10161016
contract_name: &str,
10171017
stripped_creation_code: &str,
10181018
) {
1019-
compiled.compiled_contracts_by_compiler_version().iter().for_each(|(_, contracts)| {
1020-
contracts.iter().for_each(|(name, contract)| {
1019+
for contracts in compiled.compiled_contracts_by_compiler_version().values() {
1020+
for (name, contract) in contracts {
10211021
if name == contract_name {
10221022
let compiled_creation_code =
10231023
contract.bin_ref().expect("creation code not found");
@@ -1027,8 +1027,8 @@ mod tests {
10271027
"inconsistent creation code"
10281028
);
10291029
}
1030-
});
1031-
});
1030+
}
1031+
}
10321032
}
10331033

10341034
fn mock_etherscan(address: Address) -> impl super::ExplorerClient {

crates/forge/src/cmd/compiler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ impl ResolveArgs {
109109
// Clear paths if verbosity is 0, performed only after filtering to avoid being
110110
// skipped.
111111
if shell::verbosity() == 0 {
112-
versions_with_paths.iter_mut().for_each(|version| version.paths.clear());
112+
for version in &mut versions_with_paths {
113+
version.paths.clear();
114+
}
113115
}
114116

115117
output.insert(language.to_string(), versions_with_paths);

crates/forge/src/cmd/test/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ impl TestArgs {
557557
// Run tests in a non-streaming fashion and collect results for serialization.
558558
if !self.gas_report && !self.summary && shell::is_json() {
559559
let mut results = runner.test_collect(filter)?;
560-
results.values_mut().for_each(|suite_result| {
560+
for suite_result in results.values_mut() {
561561
for test_result in suite_result.test_results.values_mut() {
562562
if verbosity >= 2 {
563563
// Decode logs at level 2 and above.
@@ -567,7 +567,7 @@ impl TestArgs {
567567
test_result.logs = vec![];
568568
}
569569
}
570-
});
570+
}
571571
sh_println!("{}", serde_json::to_string(&results)?)?;
572572
let kc = runner.known_contracts.clone();
573573
return Ok(TestOutcome::new(Some(kc), results, self.allow_failure, fuzz_seed));

crates/forge/src/gas_report.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ impl GasReport {
231231
Cell::new("# Calls").fg(Color::Cyan),
232232
]);
233233

234-
contract.functions.iter().for_each(|(fname, sigs)| {
235-
sigs.iter().for_each(|(sig, gas_info)| {
234+
for (fname, sigs) in &contract.functions {
235+
for (sig, gas_info) in sigs {
236236
// Show function signature if overloaded else display function name.
237237
let display_name =
238238
if sigs.len() == 1 { fname.clone() } else { sig.replace(':', "") };
@@ -253,8 +253,8 @@ impl GasReport {
253253
.set_alignment(CellAlignment::Right),
254254
Cell::new(gas_info.calls.to_string()).set_alignment(CellAlignment::Right),
255255
]);
256-
})
257-
});
256+
}
257+
}
258258

259259
table
260260
}

0 commit comments

Comments
 (0)