Skip to content

Commit d8e3fa2

Browse files
authored
Clippy (#218)
* Format * CI * Fixed * Add comment to README * Format config * Fix build * Log time and ema * Fix ema deserialize * Revert format * Add expo * Fix comments * Unused import * Clippy * Clippy 2 * Clippy * Fix tests * Deny warnings * Restore allow * CI * Comments Co-authored-by: Guillermo Bescos <guibescos>
1 parent 9662887 commit d8e3fa2

File tree

11 files changed

+46
-37
lines changed

11 files changed

+46
-37
lines changed

.github/workflows/check-fomatting.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ jobs:
1515
with:
1616
profile: minimal
1717
toolchain: nightly
18-
components: rustfmt
18+
components: rustfmt, clippy
1919
- uses: pre-commit/action@v2.0.3

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ repos:
1414
language: "rust"
1515
entry: cargo +nightly fmt
1616
pass_filenames: false
17+
- id: cargo-clippy
18+
name: Cargo clippy
19+
language: "rust"
20+
entry : cargo +nightly clippy -- -D warnings
21+
pass_filenames : false

program/rust/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
cargo-features = ["edition2021"]
2-
31
[package]
42
name = "pyth-oracle"
53
version = "2.13.1"

program/rust/build_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<'a> DeriveAdderParserCallback<'a> {
1919
}
2020
//add pairs of types and their desired traits
2121
pub fn register_traits(&mut self, type_name: &'a str, traits: Vec<String>) {
22-
self.types_to_traits.insert(&type_name, traits);
22+
self.types_to_traits.insert(type_name, traits);
2323
}
2424
}
2525

program/rust/src/c_oracle_header.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#![allow(non_upper_case_globals)]
21
#![allow(non_camel_case_types)]
32
#![allow(non_snake_case)]
43
//we do not use all the variables in oracle.h, so this helps with the warnings
5-
#![allow(unused_variables)]
64
#![allow(dead_code)]
75
//All the custom trait imports should go here
86
use borsh::{

program/rust/src/deserialize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ pub fn deserialize_single_field_from_account<T: BorshDeserialize>(
2626
i: usize,
2727
offset: Option<size_t>,
2828
) -> Result<T, ProgramError> {
29-
Ok(deserialize_single_field_from_buffer::<T>(
29+
deserialize_single_field_from_buffer::<T>(
3030
&accounts
3131
.get(i)
3232
.ok_or(ProgramError::NotEnoughAccountKeys)?
3333
.try_borrow_data()?,
3434
offset,
35-
)?)
35+
)
3636
}

program/rust/src/lib.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#![deny(warnings)]
2+
// Allow non upper case globals from C
3+
#![allow(non_upper_case_globals)]
4+
// Allow using the solana_program::entrypoint::deserialize function
5+
#![allow(clippy::not_unsafe_ptr_arg_deref)]
6+
17
mod c_oracle_header;
28
mod deserialize;
39
mod error;
@@ -16,6 +22,7 @@ use crate::log::{
1622
pre_log,
1723
};
1824
use processor::process_instruction;
25+
1926
use solana_program::entrypoint::deserialize;
2027
use solana_program::program_error::ProgramError;
2128
use solana_program::{
@@ -48,8 +55,10 @@ extern "C" {
4855
}
4956

5057
//make the C entrypoint a no-op when running cargo test
58+
// Missing safety doc OK because this is just a no-op
5159
#[cfg(not(target_arch = "bpf"))]
52-
pub extern "C" fn c_entrypoint(input: *mut u8) -> u64 {
60+
#[allow(clippy::missing_safety_doc)]
61+
pub unsafe extern "C" fn c_entrypoint(_input: *mut u8) -> u64 {
5362
0 //SUCCESS value
5463
}
5564

@@ -68,26 +77,24 @@ pub fn c_entrypoint_wrapper(input: *mut u8) -> OracleResult {
6877
pub extern "C" fn entrypoint(input: *mut u8) -> u64 {
6978
let (program_id, accounts, instruction_data) = unsafe { deserialize(input) };
7079

71-
match pre_log(&accounts, instruction_data) {
72-
Err(error) => return error.into(),
73-
_ => {}
80+
if let Err(error) = pre_log(&accounts, instruction_data) {
81+
return error.into();
7482
}
7583

7684
let c_ret_val = match process_instruction(program_id, &accounts, instruction_data, input) {
7785
Err(error) => error.into(),
7886
Ok(success_status) => success_status,
7987
};
8088

81-
match post_log(c_ret_val, &accounts) {
82-
Err(error) => return error.into(),
83-
_ => {}
89+
if let Err(error) = post_log(c_ret_val, &accounts) {
90+
return error.into();
8491
}
8592

8693
if c_ret_val == SUCCESSFULLY_UPDATED_AGGREGATE {
8794
//0 is the SUCCESS value for solana
88-
return 0;
95+
0
8996
} else {
90-
return c_ret_val;
97+
c_ret_val
9198
}
9299
}
93100

program/rust/src/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
1616
msg!("Pyth oracle contract");
1717

1818
let instruction_header: cmd_hdr =
19-
deserialize_single_field_from_buffer::<cmd_hdr>(&instruction_data, None)?;
19+
deserialize_single_field_from_buffer::<cmd_hdr>(instruction_data, None)?;
2020
let instruction_id: u32 = instruction_header
2121
.cmd_
2222
.try_into()

program/rust/src/processor.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use solana_program::sysvar::slot_history::AccountInfo;
2222
///dispatch to the right instruction in the oracle
2323
pub fn process_instruction(
2424
program_id: &Pubkey,
25-
accounts: &Vec<AccountInfo>,
25+
accounts: &[AccountInfo],
2626
instruction_data: &[u8],
2727
input: *mut u8,
2828
) -> OracleResult {
@@ -43,11 +43,9 @@ pub fn process_instruction(
4343
{
4444
command_t_e_cmd_upd_price
4545
| command_t_e_cmd_upd_price_no_fail_on_error
46-
| command_t_e_cmd_agg_price => {
47-
update_price(program_id, &accounts, &instruction_data, input)
48-
}
46+
| command_t_e_cmd_agg_price => update_price(program_id, accounts, instruction_data, input),
4947
command_t_e_cmd_upd_account_version => {
50-
update_version(program_id, &accounts, &instruction_data)
48+
update_version(program_id, accounts, instruction_data)
5149
}
5250
_ => c_entrypoint_wrapper(input),
5351
}

program/rust/src/rust_oracle.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use solana_program::sysvar::slot_history::AccountInfo;
55

66
///Calls the c oracle update_price, and updates the Time Machine if needed
77
pub fn update_price(
8-
program_id: &Pubkey,
9-
accounts: &Vec<AccountInfo>,
10-
instruction_data: &[u8],
8+
_program_id: &Pubkey,
9+
_accounts: &[AccountInfo],
10+
_instruction_data: &[u8],
1111
input: *mut u8,
1212
) -> OracleResult {
1313
//For now, we did not change the behavior of this. this is just to show the proposed structure
@@ -18,10 +18,9 @@ pub fn update_price(
1818
/// with the current version
1919
/// updates the version number for all accounts, and resizes price accounts
2020
pub fn update_version(
21-
program_id: &Pubkey,
22-
accounts: &Vec<AccountInfo>,
23-
instruction_data: &[u8],
21+
_program_id: &Pubkey,
22+
_accounts: &[AccountInfo],
23+
_instruction_data: &[u8],
2424
) -> OracleResult {
2525
panic!("Need to merge fix to pythd in order to implement this");
26-
Ok(0) //SUCCESS
2726
}

0 commit comments

Comments
 (0)