Skip to content

Commit cce0c65

Browse files
tmpolaczykshawntabriziggwpez
authored andcommitted
Use bool::then instead of then_some with function calls (paritytech#6156)
I noticed that hardware benchmarks are being run even though we pass the --no-hardware-benchmarks cli flag. After some debugging, the cause is an incorrect usage of the `then_some` method. From [std docs](https://doc.rust-lang.org/std/primitive.bool.html#method.then_some): > Arguments passed to then_some are eagerly evaluated; if you are passing the result of a function call, it is recommended to use [then](https://doc.rust-lang.org/std/primitive.bool.html#method.then), which is lazily evaluated. ```rust let mut a = 0; let mut function_with_side_effects = || { a += 1; }; true.then_some(function_with_side_effects()); false.then_some(function_with_side_effects()); // `a` is incremented twice because the value passed to `then_some` is // evaluated eagerly. assert_eq!(a, 2); ``` This PR fixes all the similar usages of the `then_some` method across the codebase. polkadot address: 138eUqXvUYT3o4GdbnWQfGRzM8yDWh5Q2eFrFULL7RAXzdWD --------- Signed-off-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: Shawn Tabrizi <[email protected]> Co-authored-by: Oliver Tale-Yazdi <[email protected]>
1 parent f062a95 commit cce0c65

File tree

12 files changed

+61
-30
lines changed

12 files changed

+61
-30
lines changed

cumulus/polkadot-omni-node/lib/src/command.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,15 @@ pub fn run<CliConfig: crate::cli::CliConfig>(cmd_config: RunConfig) -> Result<()
266266
}
267267

268268
let hwbench = (!cli.no_hardware_benchmarks)
269-
.then_some(config.database.path().map(|database_path| {
270-
let _ = std::fs::create_dir_all(database_path);
271-
sc_sysinfo::gather_hwbench(
272-
Some(database_path),
273-
&SUBSTRATE_REFERENCE_HARDWARE,
274-
)
275-
}))
269+
.then(|| {
270+
config.database.path().map(|database_path| {
271+
let _ = std::fs::create_dir_all(database_path);
272+
sc_sysinfo::gather_hwbench(
273+
Some(database_path),
274+
&SUBSTRATE_REFERENCE_HARDWARE,
275+
)
276+
})
277+
})
276278
.flatten();
277279

278280
let parachain_account =

polkadot/cli/src/command.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ where
203203

204204
runner.run_node_until_exit(move |config| async move {
205205
let hwbench = (!cli.run.no_hardware_benchmarks)
206-
.then_some(config.database.path().map(|database_path| {
207-
let _ = std::fs::create_dir_all(&database_path);
208-
sc_sysinfo::gather_hwbench(Some(database_path), &SUBSTRATE_REFERENCE_HARDWARE)
209-
}))
206+
.then(|| {
207+
config.database.path().map(|database_path| {
208+
let _ = std::fs::create_dir_all(&database_path);
209+
sc_sysinfo::gather_hwbench(Some(database_path), &SUBSTRATE_REFERENCE_HARDWARE)
210+
})
211+
})
210212
.flatten();
211213

212214
let database_source = config.database.clone();

polkadot/node/network/collator-protocol/src/collator_side/validators_buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl ValidatorGroupsBuffer {
110110
.validators
111111
.iter()
112112
.enumerate()
113-
.filter_map(|(idx, authority_id)| bits[idx].then_some(authority_id.clone()))
113+
.filter_map(|(idx, authority_id)| bits[idx].then(|| authority_id.clone()))
114114
.collect();
115115

116116
if let Some(last_group) = self.group_infos.iter().last() {

prdoc/pr_6156.prdoc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
title: "Use bool::then instead of then_some with function calls"
2+
doc:
3+
- audience: Node Dev
4+
description: |-
5+
Fix misusage of `bool::then_some`.
6+
7+
crates:
8+
- name: polkadot-omni-node-lib
9+
bump: patch
10+
- name: polkadot-cli
11+
bump: patch
12+
- name: polkadot-collator-protocol
13+
bump: patch
14+
- name: sc-network
15+
bump: patch
16+
- name: sc-network-sync
17+
bump: patch
18+
- name: pallet-contracts-proc-macro
19+
bump: patch
20+
- name: frame-support-procedural
21+
bump: patch
22+
- name: frame-support
23+
bump: patch

substrate/bin/node/cli/src/service.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,12 @@ pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
420420
let enable_offchain_worker = config.offchain_worker.enabled;
421421

422422
let hwbench = (!disable_hardware_benchmarks)
423-
.then_some(config.database.path().map(|database_path| {
424-
let _ = std::fs::create_dir_all(&database_path);
425-
sc_sysinfo::gather_hwbench(Some(database_path), &SUBSTRATE_REFERENCE_HARDWARE)
426-
}))
423+
.then(|| {
424+
config.database.path().map(|database_path| {
425+
let _ = std::fs::create_dir_all(&database_path);
426+
sc_sysinfo::gather_hwbench(Some(database_path), &SUBSTRATE_REFERENCE_HARDWARE)
427+
})
428+
})
427429
.flatten();
428430

429431
let sc_service::PartialComponents {

substrate/client/network/src/discovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
648648
let mut list: LinkedHashSet<_> = self
649649
.permanent_addresses
650650
.iter()
651-
.filter_map(|(p, a)| (*p == peer_id).then_some(a.clone()))
651+
.filter_map(|(p, a)| (*p == peer_id).then(|| a.clone()))
652652
.collect();
653653

654654
if let Some(ephemeral_addresses) = self.ephemeral_addresses.get(&peer_id) {

substrate/client/network/sync/src/strategy/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl<B: BlockT> StateStrategy<B> {
173173
peer_id: PeerId,
174174
announce: &BlockAnnounce<B::Header>,
175175
) -> Option<(B::Hash, NumberFor<B>)> {
176-
is_best.then_some({
176+
is_best.then(|| {
177177
let best_number = *announce.header.number();
178178
let best_hash = announce.header.hash();
179179
if let Some(ref mut peer) = self.peers.get_mut(&peer_id) {

substrate/client/network/sync/src/strategy/warp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ where
301301
peer_id: PeerId,
302302
announce: &BlockAnnounce<B::Header>,
303303
) -> Option<(B::Hash, NumberFor<B>)> {
304-
is_best.then_some({
304+
is_best.then(|| {
305305
let best_number = *announce.header.number();
306306
let best_hash = announce.header.hash();
307307
if let Some(ref mut peer) = self.peers.get_mut(&peer_id) {

substrate/frame/contracts/proc-macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ fn expand_docs(def: &EnvDef) -> TokenStream2 {
522522
/// `expand_impls()`).
523523
fn expand_env(def: &EnvDef, docs: bool) -> TokenStream2 {
524524
let impls = expand_impls(def);
525-
let docs = docs.then_some(expand_docs(def)).unwrap_or(TokenStream2::new());
525+
let docs = docs.then(|| expand_docs(def)).unwrap_or(TokenStream2::new());
526526
let stable_api_count = def.host_funcs.iter().filter(|f| f.is_stable).count();
527527

528528
quote! {

substrate/frame/support/procedural/src/pallet/expand/pallet_struct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ pub fn expand_pallet_struct(def: &mut Def) -> proc_macro2::TokenStream {
171171
let whitelisted_storage_idents: Vec<syn::Ident> = def
172172
.storages
173173
.iter()
174-
.filter_map(|s| s.whitelisted.then_some(s.ident.clone()))
174+
.filter_map(|s| s.whitelisted.then(|| s.ident.clone()))
175175
.collect();
176176

177177
let whitelisted_storage_keys_impl = quote::quote![

substrate/frame/support/src/migrations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,14 +825,14 @@ impl<T: SteppedMigration> SteppedMigrations for T {
825825

826826
fn nth_id(n: u32) -> Option<Vec<u8>> {
827827
n.is_zero()
828-
.then_some(T::id().encode())
828+
.then(|| T::id().encode())
829829
.defensive_proof("nth_id should only be called with n==0")
830830
}
831831

832832
fn nth_max_steps(n: u32) -> Option<Option<u32>> {
833833
// It should be generally fine to call with n>0, but the code should not attempt to.
834834
n.is_zero()
835-
.then_some(T::max_steps())
835+
.then(|| T::max_steps())
836836
.defensive_proof("nth_max_steps should only be called with n==0")
837837
}
838838

templates/parachain/node/src/command.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,15 @@ pub fn run() -> Result<()> {
220220

221221
runner.run_node_until_exit(|config| async move {
222222
let hwbench = (!cli.no_hardware_benchmarks)
223-
.then_some(config.database.path().map(|database_path| {
224-
let _ = std::fs::create_dir_all(database_path);
225-
sc_sysinfo::gather_hwbench(
226-
Some(database_path),
227-
&SUBSTRATE_REFERENCE_HARDWARE,
228-
)
229-
}))
223+
.then(|| {
224+
config.database.path().map(|database_path| {
225+
let _ = std::fs::create_dir_all(database_path);
226+
sc_sysinfo::gather_hwbench(
227+
Some(database_path),
228+
&SUBSTRATE_REFERENCE_HARDWARE,
229+
)
230+
})
231+
})
230232
.flatten();
231233

232234
let para_id = chain_spec::Extensions::try_get(&*config.chain_spec)

0 commit comments

Comments
 (0)