Skip to content

Commit 51514dc

Browse files
authored
Add clippy to all rust projects (#400)
1 parent 895f053 commit 51514dc

File tree

23 files changed

+132
-173
lines changed

23 files changed

+132
-173
lines changed

.pre-commit-config.yaml

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,56 @@ repos:
1717
- repo: local
1818
hooks:
1919
# Hooks for the remote executor
20-
- id: cargo-fmt-executor-remote-executor
21-
name: Cargo format executor for remote executor
20+
- id: cargo-fmt-remote-executor
21+
name: Cargo format for remote executor
2222
language: "rust"
2323
entry: cargo +nightly fmt --manifest-path ./pythnet/remote-executor/Cargo.toml --all -- --config-path rustfmt.toml
2424
pass_filenames: false
2525
files: pythnet/remote-executor/
26-
- id: cargo-clippy-executor
27-
name: Cargo clippy executor
26+
- id: cargo-clippy-remote-executor
27+
name: Cargo clippy for remote executor
2828
language: "rust"
29-
entry: cargo +nightly clippy --manifest-path ./pythnet/remote-executor/Cargo.toml -- -D warnings
29+
entry: cargo +nightly clippy --manifest-path ./pythnet/remote-executor/Cargo.toml --tests --fix --allow-dirty --allow-staged -- -D warnings
3030
pass_filenames: false
3131
files: pythnet/remote-executor/
3232
# Hooks for the attester
33-
- id: cargo-fmt-executor-attester
34-
name: Cargo format executor for attester
33+
- id: cargo-fmt-attester
34+
name: Cargo format for attester
3535
language: "rust"
3636
entry: cargo +nightly fmt --manifest-path ./solana/pyth2wormhole/Cargo.toml --all -- --config-path rustfmt.toml
3737
pass_filenames: false
3838
files: solana/pyth2wormhole/
39+
- id: cargo-clippy-attester
40+
name: Cargo clippy for attester
41+
language: "rust"
42+
entry: |
43+
bash -c 'EMITTER_ADDRESS=0 BRIDGE_ADDRESS=0 cargo +nightly clippy --manifest-path \
44+
./solana/pyth2wormhole/Cargo.toml --tests --fix --allow-dirty --allow-staged -- -D warnings'
45+
pass_filenames: false
46+
files: solana/pyth2wormhole/
3947
# Hooks for cosmwasm contract
40-
- id: cargo-fmt-executor-cosmwasm
41-
name: Cargo format executor form cosmwasm contract
48+
- id: cargo-fmt-cosmwasm
49+
name: Cargo format for cosmwasm contract
4250
language: "rust"
4351
entry: cargo +nightly fmt --manifest-path ./cosmwasm/Cargo.toml --all -- --config-path rustfmt.toml
4452
pass_filenames: false
4553
files: cosmwasm/
54+
- id: cargo-clippy-cosmwasm
55+
name: Cargo clippy for cosmwasm contract
56+
language: "rust"
57+
entry: cargo +nightly clippy --manifest-path ./cosmwasm/Cargo.toml --tests --fix --allow-dirty --allow-staged -- -D warnings
58+
pass_filenames: false
59+
files: cosmwasm/
60+
# Hooks for p2w-sdk/rust
61+
- id: cargo-fmt-p2w-sdk
62+
name: Cargo format for p2w-sdk
63+
language: "rust"
64+
entry: cargo +nightly fmt --manifest-path ./third_party/pyth/p2w-sdk/rust/Cargo.toml --all -- --config-path rustfmt.toml
65+
pass_filenames: false
66+
files: third_party/pyth/p2w-sdk/rust/
67+
- id: cargo-clippy-p2w-sdk
68+
name: Cargo clippy for p2w-sdk
69+
language: "rust"
70+
entry: cargo +nightly clippy --manifest-path ./third_party/pyth/p2w-sdk/rust/Cargo.toml --tests --fix --allow-dirty --allow-staged -- -D warnings
71+
pass_filenames: false
72+
files: third_party/pyth/p2w-sdk/rust/

pythnet/remote-executor/programs/remote-executor/src/tests/executor_simulator.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,24 @@ impl ExecutorBench {
124124
program_test.add_account(program_key, program_account);
125125
program_test.add_account(programdata_key, programdata_account);
126126

127-
return ExecutorBench {
127+
ExecutorBench {
128128
program_test,
129129
program_id: program_key.key(),
130130
seqno: HashMap::<Pubkey, u64>::new(),
131-
};
131+
}
132132
}
133133

134134
/// Start local validator based on the current bench
135135
pub async fn start(self) -> ExecutorSimulator {
136136
// Start validator
137137
let (banks_client, genesis_keypair, recent_blockhash) = self.program_test.start().await;
138138

139-
return ExecutorSimulator {
139+
ExecutorSimulator {
140140
banks_client,
141141
payer: genesis_keypair,
142142
last_blockhash: recent_blockhash,
143143
program_id: self.program_id,
144-
};
144+
}
145145
}
146146

147147
/// Add VAA account with emitter and instructions for consumption by the remote_executor
@@ -168,10 +168,7 @@ impl ExecutorBench {
168168

169169
let payload = ExecutorPayload {
170170
header: GovernanceHeader::executor_governance_header(),
171-
instructions: instructions
172-
.iter()
173-
.map(|x| InstructionData::from(x))
174-
.collect(),
171+
instructions: instructions.iter().map(InstructionData::from).collect(),
175172
};
176173

177174
let payload_bytes = payload.try_to_vec().unwrap();
@@ -185,7 +182,7 @@ impl ExecutorBench {
185182
vaa_signature_account: Pubkey::new_unique(),
186183
submission_time: 0,
187184
nonce: 0,
188-
sequence: self.seqno.get(&emitter).unwrap_or(&0) + 1,
185+
sequence: self.seqno.get(emitter).unwrap_or(&0) + 1,
189186
emitter_chain,
190187
emitter_address: emitter.to_bytes(),
191188
payload: payload_bytes,
@@ -211,7 +208,7 @@ impl ExecutorBench {
211208

212209
let vaa_pubkey = Pubkey::new_unique();
213210
self.program_test.add_account(vaa_pubkey, vaa_account);
214-
return vaa_pubkey;
211+
vaa_pubkey
215212
}
216213

217214
// Get executor key of an emitter, useful to construct instructions that will be in the VAA
@@ -298,7 +295,7 @@ impl ExecutorSimulator {
298295
&self.program_id,
299296
&self.payer.pubkey(),
300297
&Pubkey::new(&posted_vaa_data.emitter_address),
301-
&posted_vaa_address,
298+
posted_vaa_address,
302299
)
303300
.to_account_metas(None);
304301

@@ -383,12 +380,12 @@ impl ExecutorSimulator {
383380
}
384381
}
385382

386-
impl Into<TransactionError> for ExecutorError {
387-
fn into(self) -> TransactionError {
383+
impl From<ExecutorError> for TransactionError {
384+
fn from(val: ExecutorError) -> Self {
388385
TransactionError::InstructionError(
389386
0,
390387
InstructionError::try_from(u64::from(ProgramError::from(
391-
anchor_lang::prelude::Error::from(self),
388+
anchor_lang::prelude::Error::from(val),
392389
)))
393390
.unwrap(),
394391
)

pythnet/remote-executor/programs/remote-executor/src/tests/test_adversarial.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async fn test_adversarial() {
4141
&emitter,
4242
&vec![transfer(
4343
&executor_key,
44-
&&receiver,
44+
&receiver,
4545
Rent::default().minimum_balance(0),
4646
)],
4747
VaaAttack::None,
@@ -50,7 +50,7 @@ async fn test_adversarial() {
5050
&emitter,
5151
&vec![transfer(
5252
&executor_key,
53-
&&receiver,
53+
&receiver,
5454
Rent::default().minimum_balance(0),
5555
)],
5656
VaaAttack::WrongData,
@@ -59,7 +59,7 @@ async fn test_adversarial() {
5959
&emitter,
6060
&vec![transfer(
6161
&executor_key,
62-
&&receiver,
62+
&receiver,
6363
Rent::default().minimum_balance(0),
6464
)],
6565
VaaAttack::WrongOwner,
@@ -68,7 +68,7 @@ async fn test_adversarial() {
6868
&emitter,
6969
&vec![transfer(
7070
&executor_key,
71-
&&receiver,
71+
&receiver,
7272
Rent::default().minimum_balance(0),
7373
)],
7474
VaaAttack::WrongEmitterChain,
@@ -78,7 +78,7 @@ async fn test_adversarial() {
7878
&emitter,
7979
&vec![transfer(
8080
&executor_key,
81-
&&receiver,
81+
&receiver,
8282
Rent::default().minimum_balance(0),
8383
)],
8484
VaaAttack::WrongVaaMagic,

solana/pyth2wormhole/Cargo.lock

Lines changed: 1 addition & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

solana/pyth2wormhole/client/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,5 @@ warp = "0.3.3"
3939
http = "0.2.8"
4040

4141
[dev-dependencies]
42-
pyth-client = "0.5.0"
4342
solana-program-test = "=1.10.31"
4443
solana-sdk = "=1.10.31"

solana/pyth2wormhole/client/src/attestation_cfg.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ impl AttestationConfig {
7171
// Turn the pruned symbols into P2WSymbol structs
7272
let mut new_symbols_vec = new_symbols
7373
.drain() // Makes us own the elements and lets us move them
74-
.map(|(prod, prices)| iter::zip(iter::repeat(prod), prices)) // Convert to iterator over flat (prod, price) tuples
75-
.flatten() // Flatten the tuple iterators
74+
.flat_map(|(prod, prices)| iter::zip(iter::repeat(prod), prices)) // Flatten the tuple iterators
7675
.map(|(prod, price)| P2WSymbol {
7776
name: None,
7877
product_addr: prod,
@@ -87,7 +86,7 @@ impl AttestationConfig {
8786
.find(|g| g.group_name == group_name) // Advances the iterator and returns Some(item) on first hit
8887
{
8988
Some(existing_group) => existing_group.symbols.append(&mut new_symbols_vec),
90-
None if new_symbols_vec.len() != 0 => {
89+
None if !new_symbols_vec.is_empty() => {
9190
// Group does not exist, assume defaults
9291
let new_group = SymbolGroup {
9392
group_name,
@@ -104,7 +103,7 @@ impl AttestationConfig {
104103
pub fn as_batches(&self, max_batch_size: usize) -> Vec<BatchState> {
105104
self.symbol_groups
106105
.iter()
107-
.map(move |g| {
106+
.flat_map(move |g| {
108107
let conditions4closure = g.conditions.clone();
109108
let name4closure = g.group_name.clone();
110109

@@ -113,12 +112,11 @@ impl AttestationConfig {
113112
// Divide group into batches
114113
g.symbols
115114
.as_slice()
116-
.chunks(max_batch_size.clone())
115+
.chunks(max_batch_size)
117116
.map(move |symbols| {
118117
BatchState::new(name4closure.clone(), symbols, conditions4closure.clone())
119118
})
120119
})
121-
.flatten()
122120
.collect()
123121
}
124122
}
@@ -257,7 +255,7 @@ fn opt_pubkey_string_ser<S>(k_opt: &Option<Pubkey>, ser: S) -> Result<S::Ok, S::
257255
where
258256
S: Serializer,
259257
{
260-
let k_str_opt = k_opt.clone().map(|k| k.to_string());
258+
let k_str_opt = (*k_opt).map(|k| k.to_string());
261259

262260
Option::<String>::serialize(&k_str_opt, ser)
263261
}
@@ -352,7 +350,7 @@ mod tests {
352350
mock_prod_bytes[31] = sym_idx;
353351

354352
let mut mock_prices = HashSet::new();
355-
for px_idx in 1..=5 {
353+
for _px_idx in 1..=5 {
356354
let mut mock_price_bytes = [0u8; 32];
357355
mock_price_bytes[31] = sym_idx;
358356
mock_prices.insert(Pubkey::new_from_array(mock_price_bytes));
@@ -370,7 +368,7 @@ mod tests {
370368

371369
// Should not be created because there's no new symbols to add
372370
// (we're adding identical mock_new_symbols again)
373-
config2.add_symbols(mock_new_symbols.clone(), "default2".to_owned());
371+
config2.add_symbols(mock_new_symbols, "default2".to_owned());
374372

375373
assert_ne!(config1, empty_config); // Check that config grows from empty
376374
assert_eq!(config1, config2); // Check that no changes are made if all symbols are already in there

solana/pyth2wormhole/client/src/batch_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,6 @@ impl<'a> BatchState {
157157
}
158158
}
159159

160-
return ret;
160+
ret
161161
}
162162
}

0 commit comments

Comments
 (0)