Skip to content

Commit f11dfa6

Browse files
authored
Merge pull request #312 from input-output-hk/sg/test-clippy
Run clippy against tests
2 parents 9695d82 + b7cc9cb commit f11dfa6

File tree

16 files changed

+145
-217
lines changed

16 files changed

+145
-217
lines changed

.github/workflows/run-tests-on-push-to-main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525

2626
- name: Run Clippy
2727
run: |
28-
cargo clippy \
28+
cargo clippy --all-targets --all-features \
2929
--package acropolis_common \
3030
--package acropolis_codec \
3131
--package acropolis_module_accounts_state \

common/examples/test_streaming_parser.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::env;
1212
use std::time::Instant;
1313

1414
// Simple counter callback that doesn't store data in memory
15+
#[derive(Default)]
1516
struct CountingCallbacks {
1617
metadata: Option<SnapshotMetadata>,
1718
utxo_count: u64,
@@ -26,24 +27,6 @@ struct CountingCallbacks {
2627
sample_proposals: Vec<GovernanceProposal>,
2728
}
2829

29-
impl Default for CountingCallbacks {
30-
fn default() -> Self {
31-
Self {
32-
metadata: None,
33-
utxo_count: 0,
34-
pool_count: 0,
35-
account_count: 0,
36-
drep_count: 0,
37-
proposal_count: 0,
38-
sample_utxos: Vec::new(),
39-
sample_pools: Vec::new(),
40-
sample_accounts: Vec::new(),
41-
sample_dreps: Vec::new(),
42-
sample_proposals: Vec::new(),
43-
}
44-
}
45-
}
46-
4730
impl UtxoCallback for CountingCallbacks {
4831
fn on_utxo(&mut self, utxo: UtxoEntry) -> Result<()> {
4932
self.utxo_count += 1;
@@ -62,7 +45,7 @@ impl UtxoCallback for CountingCallbacks {
6245
self.sample_utxos.push(utxo);
6346
}
6447
// Progress reporting every million UTXOs
65-
if self.utxo_count > 0 && self.utxo_count % 1000000 == 0 {
48+
if self.utxo_count > 0 && self.utxo_count.is_multiple_of(1000000) {
6649
eprintln!(" Parsed {} UTXOs...", self.utxo_count);
6750
}
6851
Ok(())
@@ -95,7 +78,7 @@ impl PoolCallback for CountingCallbacks {
9578
impl StakeCallback for CountingCallbacks {
9679
fn on_accounts(&mut self, accounts: Vec<AccountState>) -> Result<()> {
9780
self.account_count = accounts.len();
98-
if accounts.len() > 0 {
81+
if !accounts.is_empty() {
9982
eprintln!("✓ Parsed {} stake accounts", accounts.len());
10083

10184
// Show first 10 accounts
@@ -152,7 +135,7 @@ impl DRepCallback for CountingCallbacks {
152135
impl ProposalCallback for CountingCallbacks {
153136
fn on_proposals(&mut self, proposals: Vec<GovernanceProposal>) -> Result<()> {
154137
self.proposal_count = proposals.len();
155-
if proposals.len() > 0 {
138+
if !proposals.is_empty() {
156139
eprintln!("✓ Parsed {} governance proposals", proposals.len());
157140

158141
// Show first 10 proposals

common/src/stake_addresses.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ mod tests {
558558

559559
fn create_stake_address(hash: &[u8]) -> StakeAddress {
560560
StakeAddress::new(
561-
StakeCredential::AddrKeyHash(hash.to_vec().try_into().expect("Invalid hash length")),
561+
StakeCredential::AddrKeyHash(hash.to_vec()),
562562
NetworkId::Mainnet,
563563
)
564564
}
@@ -1017,7 +1017,7 @@ mod tests {
10171017

10181018
let spdd = stake_addresses.generate_spdd();
10191019

1020-
let pool_stake = spdd.get(&SPO_HASH.to_vec()).unwrap();
1020+
let pool_stake = spdd.get(SPO_HASH.as_slice()).unwrap();
10211021
assert_eq!(pool_stake.active, 3000); // utxo only
10221022
assert_eq!(pool_stake.live, 3150); // utxo + rewards
10231023
assert_eq!(pool_stake.active_delegators_count, 2);
@@ -1047,8 +1047,8 @@ mod tests {
10471047
let spdd = stake_addresses.generate_spdd();
10481048

10491049
assert_eq!(spdd.len(), 2);
1050-
assert_eq!(spdd.get(&SPO_HASH.to_vec()).unwrap().active, 1000);
1051-
assert_eq!(spdd.get(&SPO_HASH_2.to_vec()).unwrap().active, 2000);
1050+
assert_eq!(spdd.get(SPO_HASH.as_slice()).unwrap().active, 1000);
1051+
assert_eq!(spdd.get(SPO_HASH_2.as_slice()).unwrap().active, 2000);
10521052
}
10531053

10541054
#[test]

common/src/types.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ mod tests {
19281928
assert_eq!(Era::default() as u8, 0);
19291929
assert_eq!(Era::Byron as u8, 0);
19301930
assert_eq!(Era::Conway as u8, 6);
1931-
assert!(!Era::try_from(7).is_ok());
1931+
assert!(Era::try_from(7).is_err());
19321932

19331933
for ei in 0..=6 {
19341934
for ej in 0..=6 {
@@ -1986,16 +1986,14 @@ mod tests {
19861986
let gov_action = GovernanceAction::UpdateCommittee(UpdateCommitteeAction {
19871987
previous_action_id: None,
19881988
data: CommitteeChange {
1989-
removed_committee_members: HashSet::from_iter(
1990-
vec![
1991-
make_committee_credential(true, 48),
1992-
make_committee_credential(false, 12),
1993-
]
1994-
.into_iter(),
1995-
),
1996-
new_committee_members: HashMap::from_iter(
1997-
vec![(make_committee_credential(false, 87), 1234)].into_iter(),
1998-
),
1989+
removed_committee_members: HashSet::from_iter([
1990+
make_committee_credential(true, 48),
1991+
make_committee_credential(false, 12),
1992+
]),
1993+
new_committee_members: HashMap::from_iter([(
1994+
make_committee_credential(false, 87),
1995+
1234,
1996+
)]),
19991997
terms: RationalNumber::from(1),
20001998
},
20011999
});

common/tests/loc_over_bus.rs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,46 +63,37 @@ impl Subscriber {
6363
let ctx = context.clone();
6464
ctx.run(async move {
6565
while let Ok((_, msg)) = sub.read().await {
66-
match &*msg {
67-
BusMsg::Loc(loc) => {
68-
let res = Resolver::new(&registry()).resolve(loc);
69-
match res {
70-
Ok(view) => {
71-
// touch the bytes so we know mapping worked
72-
let slice = view.as_slice();
73-
// trivial check: non-empty
74-
if !slice.is_empty() {
75-
context
76-
.publish(
77-
&ack_topic,
78-
Arc::new(BusMsg::Ack("ok".to_string())),
79-
)
80-
.await
81-
.expect("Failed to publish ACK");
82-
} else {
83-
context
84-
.publish(
85-
&ack_topic,
86-
Arc::new(BusMsg::Ack("empty".to_string())),
87-
)
88-
.await
89-
.expect("Failed to publish ACK");
90-
}
91-
break; // test done
92-
}
93-
Err(_) => {
66+
if let BusMsg::Loc(loc) = &*msg {
67+
let res = Resolver::new(&registry()).resolve(loc);
68+
match res {
69+
Ok(view) => {
70+
// touch the bytes so we know mapping worked
71+
let slice = view.as_slice();
72+
// trivial check: non-empty
73+
if !slice.is_empty() {
9474
context
95-
.publish(
96-
&ack_topic,
97-
Arc::new(BusMsg::Ack("resolve_err".to_string())),
98-
)
75+
.publish(&ack_topic, Arc::new(BusMsg::Ack("ok".to_string())))
76+
.await
77+
.expect("Failed to publish ACK");
78+
} else {
79+
context
80+
.publish(&ack_topic, Arc::new(BusMsg::Ack("empty".to_string())))
9981
.await
10082
.expect("Failed to publish ACK");
101-
break;
10283
}
84+
break; // test done
85+
}
86+
Err(_) => {
87+
context
88+
.publish(
89+
&ack_topic,
90+
Arc::new(BusMsg::Ack("resolve_err".to_string())),
91+
)
92+
.await
93+
.expect("Failed to publish ACK");
94+
break;
10395
}
10496
}
105-
_ => {}
10697
}
10798
}
10899
});

modules/accounts_state/src/snapshot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ mod tests {
196196
hash[0] = id;
197197
StakeAddress {
198198
network: Mainnet,
199-
credential: StakeCredential::AddrKeyHash(hash.try_into().expect("Invalid hash length")),
199+
credential: StakeCredential::AddrKeyHash(hash),
200200
}
201201
}
202202

modules/address_state/src/state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ mod tests {
232232
fn delta(addr: &Address, utxo: &UTxOIdentifier, lovelace: i64) -> AddressDelta {
233233
AddressDelta {
234234
address: addr.clone(),
235-
utxo: utxo.clone(),
235+
utxo: *utxo,
236236
value: ValueDelta {
237237
lovelace,
238238
assets: Vec::new(),
@@ -311,21 +311,21 @@ mod tests {
311311

312312
// Verify UTxO was removed while in volatile
313313
let after_spend_volatile = state.get_address_utxos(&addr).await?;
314-
assert!(after_spend_volatile.as_ref().map_or(true, |u| u.is_empty()));
314+
assert!(after_spend_volatile.as_ref().is_none_or(|u| u.is_empty()));
315315

316316
// Drain volatile to immutable
317317
state.prune_volatile().await;
318318

319319
// Verify UTxO was removed while in pending immutable
320320
let after_spend_pending = state.get_address_utxos(&addr).await?;
321-
assert!(after_spend_pending.as_ref().map_or(true, |u| u.is_empty()));
321+
assert!(after_spend_pending.as_ref().is_none_or(|u| u.is_empty()));
322322

323323
// Perisist immutable to disk
324324
state.immutable.persist_epoch(2, &state.config).await?;
325325

326326
// Verify UTxO was removed after persisting spend to disk
327327
let after_spend_disk = state.get_address_utxos(&addr).await?;
328-
assert!(after_spend_disk.as_ref().map_or(true, |u| u.is_empty()));
328+
assert!(after_spend_disk.as_ref().is_none_or(|u| u.is_empty()));
329329

330330
Ok(())
331331
}

modules/assets_state/src/asset_registry.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ mod tests {
8888
let policy = dummy_policy(1);
8989
let name = asset_name_from_str("tokenA");
9090

91-
let id1 = registry.get_or_insert(policy.clone(), name.clone());
92-
let id2 = registry.get_or_insert(policy.clone(), name.clone());
91+
let id1 = registry.get_or_insert(policy, name);
92+
let id2 = registry.get_or_insert(policy, name);
9393

9494
assert_eq!(id1, id2);
9595
}
@@ -102,8 +102,8 @@ mod tests {
102102
let name1 = asset_name_from_str("tokenA");
103103
let name2 = asset_name_from_str("tokenB");
104104

105-
let id1 = registry.get_or_insert(policy1.clone(), name1.clone());
106-
let id2 = registry.get_or_insert(policy2.clone(), name2.clone());
105+
let id1 = registry.get_or_insert(policy1, name1);
106+
let id2 = registry.get_or_insert(policy2, name2);
107107

108108
assert_ne!(id1, id2);
109109
assert_eq!(id1.index(), 0);
@@ -116,7 +116,7 @@ mod tests {
116116
let policy = dummy_policy(1);
117117
let name = asset_name_from_str("tokenA");
118118

119-
let id1 = registry.get_or_insert(policy.clone(), name.clone());
119+
let id1 = registry.get_or_insert(policy, name);
120120
let id2 = registry.lookup_id(&policy, &name).unwrap();
121121

122122
assert_eq!(id1, id2);
@@ -137,7 +137,7 @@ mod tests {
137137
let policy = dummy_policy(1);
138138
let name = asset_name_from_str("tokenA");
139139

140-
let id = registry.get_or_insert(policy.clone(), name.clone());
140+
let id = registry.get_or_insert(policy, name);
141141
let key = registry.lookup(id).unwrap();
142142

143143
assert_eq!(policy, *key.policy);

0 commit comments

Comments
 (0)