Skip to content

Commit

Permalink
clippy: Replaces .get(0) with .first() (solana-labs#34048)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Nov 13, 2023
1 parent a25eae4 commit 725ab37
Show file tree
Hide file tree
Showing 26 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10937,7 +10937,7 @@ pub mod tests {
expected[0].push(raw_expected[index]);
}
let mut result2 = (0..range).map(|_| Vec::default()).collect::<Vec<_>>();
if let Some(m) = result.get(0) {
if let Some(m) = result.first() {
m.load_all(&mut result2, bin, &PubkeyBinCalculator24::new(bins));
} else {
result2 = vec![];
Expand Down
2 changes: 1 addition & 1 deletion banks-server/src/banks_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Banks for BanksServer {
.root_bank()
.get_blockhash_last_valid_block_height(blockhash)
.unwrap();
let signature = transaction.signatures.get(0).cloned().unwrap_or_default();
let signature = transaction.signatures.first().cloned().unwrap_or_default();
let info = TransactionInfo::new(
signature,
serialize(&transaction).unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion cli/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,7 @@ fn check_payer(
}
if !write_messages.is_empty() {
// Assume all write messages cost the same
if let Some(message) = write_messages.get(0) {
if let Some(message) = write_messages.first() {
fee += rpc_client.get_fee_for_message(message)? * (write_messages.len() as u64);
}
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/program_v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ fn check_payer(
}
if !write_messages.is_empty() {
// Assume all write messages cost the same
if let Some(message) = write_messages.get(0) {
if let Some(message) = write_messages.first() {
fee += rpc_client.get_fee_for_message(message)? * (write_messages.len() as u64);
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,8 +2603,8 @@ pub fn process_delegate_stake(
// filter should return at most one result
let rpc_vote_account =
current
.get(0)
.or_else(|| delinquent.get(0))
.first()
.or_else(|| delinquent.first())
.ok_or(CliError::RpcRequestError(format!(
"Vote account not found: {vote_account_pubkey}"
)))?;
Expand Down
4 changes: 2 additions & 2 deletions core/src/banking_stage/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ mod tests {
let commit_transactions_result = commit_transactions_result.unwrap();
assert_eq!(commit_transactions_result.len(), 2);
assert_matches!(
commit_transactions_result.get(0),
commit_transactions_result.first(),
Some(CommitTransactionDetails::Committed { .. })
);
assert_matches!(
Expand All @@ -1274,7 +1274,7 @@ mod tests {
assert_eq!(retryable_transaction_indexes, vec![1]);

let expected_block_cost = if !apply_cost_tracker_during_replay_enabled {
let actual_bpf_execution_cost = match commit_transactions_result.get(0).unwrap() {
let actual_bpf_execution_cost = match commit_transactions_result.first().unwrap() {
CommitTransactionDetails::Committed { compute_units } => *compute_units,
CommitTransactionDetails::NotCommitted => {
unreachable!()
Expand Down
2 changes: 1 addition & 1 deletion core/src/banking_stage/latest_unprocessed_votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl LatestValidatorVotePacket {
let &pubkey = message
.message
.static_account_keys()
.get(0)
.first()
.ok_or(DeserializedPacketError::VoteTransactionError)?;
let slot = vote_state_update_instruction.last_voted_slot().unwrap_or(0);
let timestamp = vote_state_update_instruction.timestamp();
Expand Down
2 changes: 1 addition & 1 deletion core/src/consensus/tower_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl TowerStorage for EtcdTowerStorage {

for op_response in response.op_responses() {
if let etcd_client::TxnOpResponse::Get(get_response) = op_response {
if let Some(kv) = get_response.kvs().get(0) {
if let Some(kv) = get_response.kvs().first() {
return bincode::deserialize_from(kv.value())
.map_err(|e| e.into())
.and_then(|t: SavedTowerVersions| t.try_into_tower(node_pubkey));
Expand Down
2 changes: 1 addition & 1 deletion ledger/src/blockstore/blockstore_purge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ impl Blockstore {
.into_iter()
.flat_map(|entry| entry.transactions);
for (i, transaction) in transactions.enumerate() {
if let Some(&signature) = transaction.signatures.get(0) {
if let Some(&signature) = transaction.signatures.first() {
batch.delete::<cf::TransactionStatus>((signature, slot))?;
batch.delete::<cf::TransactionMemos>((signature, slot))?;
if !primary_indexes.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ mod tests {
(solana_sdk::pubkey::new_rand(), loader_account),
];
let metas = vec![
AccountMeta::new(transaction_accounts.get(0).unwrap().0, false),
AccountMeta::new(transaction_accounts.first().unwrap().0, false),
AccountMeta::new(transaction_accounts.get(1).unwrap().0, false),
AccountMeta::new_readonly(transaction_accounts.get(2).unwrap().0, false),
];
Expand Down
6 changes: 3 additions & 3 deletions program-runtime/src/loaded_programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ mod tests {
.get(&program1)
.expect("Failed to find the entry");
assert_eq!(second_level.len(), 1);
assert!(second_level.get(0).unwrap().is_tombstone());
assert!(second_level.first().unwrap().is_tombstone());
assert_eq!(tombstone.deployment_slot, 10);
assert_eq!(tombstone.effective_slot, 10);

Expand All @@ -1398,7 +1398,7 @@ mod tests {
.get(&program2)
.expect("Failed to find the entry");
assert_eq!(second_level.len(), 1);
assert!(!second_level.get(0).unwrap().is_tombstone());
assert!(!second_level.first().unwrap().is_tombstone());

let tombstone = set_tombstone(
&mut cache,
Expand All @@ -1411,7 +1411,7 @@ mod tests {
.get(&program2)
.expect("Failed to find the entry");
assert_eq!(second_level.len(), 2);
assert!(!second_level.get(0).unwrap().is_tombstone());
assert!(!second_level.first().unwrap().is_tombstone());
assert!(second_level.get(1).unwrap().is_tombstone());
assert!(tombstone.is_tombstone());
assert_eq!(tombstone.deployment_slot, 60);
Expand Down
2 changes: 1 addition & 1 deletion programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2687,7 +2687,7 @@ mod tests {
&elf_orig,
&elf_new,
);
*instruction_accounts.get_mut(3).unwrap() = instruction_accounts.get(0).unwrap().clone();
*instruction_accounts.get_mut(3).unwrap() = instruction_accounts.first().unwrap().clone();
process_instruction(
transaction_accounts,
instruction_accounts,
Expand Down
2 changes: 1 addition & 1 deletion programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ declare_builtin_function!(
invoke_context.get_check_aligned(),
invoke_context.get_check_size(),
)?
.get(0)
.first()
.ok_or(SyscallError::InvalidLength)?;

if params.base_len > 512 || params.exponent_len > 512 || params.modulus_len > 512 {
Expand Down
2 changes: 1 addition & 1 deletion remote-wallet/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl Locator {
let host = uri.host().map(|h| h.to_string());
match (scheme, host) {
(Some(scheme), Some(host)) if scheme == "usb" => {
let path = uri.path().segments().get(0).and_then(|s| {
let path = uri.path().segments().first().and_then(|s| {
if !s.is_empty() {
Some(s.as_str())
} else {
Expand Down
2 changes: 1 addition & 1 deletion rpc-test/tests/nonblocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn test_tpu_send_transaction() {
.get_signature_statuses(&signatures)
.await
.unwrap();
if statuses.value.get(0).is_some() {
if statuses.value.first().is_some() {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion rpc-test/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ fn run_tpu_send_transaction(tpu_use_quic: bool) {
loop {
assert!(now.elapsed() < timeout);
let statuses = rpc_client.get_signature_statuses(&signatures).unwrap();
if statuses.value.get(0).is_some() {
if statuses.value.first().is_some() {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6384,7 +6384,7 @@ impl Bank {
pub fn process_transaction(&self, tx: &Transaction) -> Result<()> {
self.try_process_transactions(std::iter::once(tx))?[0].clone()?;
tx.signatures
.get(0)
.first()
.map_or(Ok(()), |sig| self.get_signature_status(sig).unwrap())
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/src/bank_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl AsyncClient for BankClient {
&self,
transaction: VersionedTransaction,
) -> Result<Signature> {
let signature = transaction.signatures.get(0).cloned().unwrap_or_default();
let signature = transaction.signatures.first().cloned().unwrap_or_default();
let transaction_sender = self.transaction_sender.lock().unwrap();
transaction_sender.send(transaction).unwrap();
Ok(signature)
Expand All @@ -60,7 +60,7 @@ impl SyncClient for BankClient {
let blockhash = self.bank.last_blockhash();
let transaction = Transaction::new(keypairs, message, blockhash);
self.bank.process_transaction(&transaction)?;
Ok(transaction.signatures.get(0).cloned().unwrap_or_default())
Ok(transaction.signatures.first().cloned().unwrap_or_default())
}

/// Create and process a transaction from a single instruction.
Expand Down
4 changes: 2 additions & 2 deletions sdk/cargo-build-bpf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
s.replace("--bpf", "--sbf")
})
.collect::<Vec<_>>();
let program = if let Some(arg0) = args.get(0) {
let program = if let Some(arg0) = args.first() {
let arg0 = arg0.replace("build-bpf", "build-sbf");
args.remove(0);
PathBuf::from(arg0)
Expand All @@ -25,7 +25,7 @@ fn main() {
};
// When run as a cargo subcommand, the first program argument is the subcommand name.
// Remove it
if let Some(arg0) = args.get(0) {
if let Some(arg0) = args.first() {
if arg0 == "build-bpf" {
args.remove(0);
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/cargo-test-bpf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
let cargo_build_sbf = cargo_build_bpf.replace("build-bpf", "build-sbf");
env::set_var("CARGO_BUILD_SBF", cargo_build_sbf);
}
let program = if let Some(arg0) = args.get(0) {
let program = if let Some(arg0) = args.first() {
let cargo_test_sbf = arg0.replace("test-bpf", "test-sbf");
let cargo_build_sbf = cargo_test_sbf.replace("test-sbf", "build-sbf");
env::set_var("CARGO_BUILD_SBF", cargo_build_sbf);
Expand All @@ -27,7 +27,7 @@ fn main() {
};
// When run as a cargo subcommand, the first program argument is the subcommand name.
// Remove it
if let Some(arg0) = args.get(0) {
if let Some(arg0) = args.first() {
if arg0 == "test-bpf" {
args.remove(0);
}
Expand Down
2 changes: 1 addition & 1 deletion turbine/src/broadcast_stage/standard_broadcast_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ mod test {
// Slot 2 interrupted slot 1
let shreds = run.finish_prev_slot(&keypair, 0, &mut ProcessShredsStats::default());
let shred = shreds
.get(0)
.first()
.expect("Expected a shred that signals an interrupt");

// Validate the shred
Expand Down
4 changes: 2 additions & 2 deletions validator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn wait_for_restart_window(
Err("Current epoch is almost complete".to_string())
} else {
while leader_schedule
.get(0)
.front()
.map(|slot| *slot < epoch_info.absolute_slot)
.unwrap_or(false)
{
Expand All @@ -258,7 +258,7 @@ fn wait_for_restart_window(
upcoming_idle_windows.pop();
}

match leader_schedule.get(0) {
match leader_schedule.front() {
None => {
Ok(()) // Validator has no leader slots
}
Expand Down
4 changes: 2 additions & 2 deletions vote/src/vote_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn parse_sanitized_vote_transaction(tx: &SanitizedTransaction) -> Option<Par
let first_account = usize::from(*first_instruction.accounts.first()?);
let key = message.account_keys().get(first_account)?;
let (vote, switch_proof_hash) = parse_vote_instruction_data(&first_instruction.data)?;
let signature = tx.signatures().get(0).cloned().unwrap_or_default();
let signature = tx.signatures().first().cloned().unwrap_or_default();
Some((*key, vote, switch_proof_hash, signature))
}

Expand All @@ -40,7 +40,7 @@ pub fn parse_vote_transaction(tx: &Transaction) -> Option<ParsedVote> {
let first_account = usize::from(*first_instruction.accounts.first()?);
let key = message.account_keys.get(first_account)?;
let (vote, switch_proof_hash) = parse_vote_instruction_data(&first_instruction.data)?;
let signature = tx.signatures.get(0).cloned().unwrap_or_default();
let signature = tx.signatures.first().cloned().unwrap_or_default();
Some((*key, vote, switch_proof_hash, signature))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ impl ZkProofData<BatchedGroupedCiphertext2HandlesValidityProofContext>
let grouped_ciphertext_hi: GroupedElGamalCiphertext<2> =
self.context.grouped_ciphertext_hi.try_into()?;

let destination_handle_lo = grouped_ciphertext_lo.handles.get(0).unwrap();
let destination_handle_lo = grouped_ciphertext_lo.handles.first().unwrap();
let auditor_handle_lo = grouped_ciphertext_lo.handles.get(1).unwrap();

let destination_handle_hi = grouped_ciphertext_hi.handles.get(0).unwrap();
let destination_handle_hi = grouped_ciphertext_hi.handles.first().unwrap();
let auditor_handle_hi = grouped_ciphertext_hi.handles.get(1).unwrap();

let proof: BatchedGroupedCiphertext2HandlesValidityProof = self.proof.try_into()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl ZkProofData<GroupedCiphertext2HandlesValidityProofContext>
let grouped_ciphertext: GroupedElGamalCiphertext<2> =
self.context.grouped_ciphertext.try_into()?;

let destination_handle = grouped_ciphertext.handles.get(0).unwrap();
let destination_handle = grouped_ciphertext.handles.first().unwrap();
let auditor_handle = grouped_ciphertext.handles.get(1).unwrap();

let proof: GroupedCiphertext2HandlesValidityProof = self.proof.try_into()?;
Expand Down
4 changes: 2 additions & 2 deletions zk-token-sdk/src/instruction/transfer/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl TransferAmountCiphertext {
pub fn get_source_handle(&self) -> &DecryptHandle {
// `TransferAmountCiphertext` is a wrapper for `GroupedElGamalCiphertext<3>`, which
// holds exactly three decryption handles.
self.0.handles.get(0).unwrap()
self.0.handles.first().unwrap()
}

pub fn get_destination_handle(&self) -> &DecryptHandle {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl FeeEncryption {
pub fn get_destination_handle(&self) -> &DecryptHandle {
// `FeeEncryption` is a wrapper for `GroupedElGamalCiphertext<2>`, which holds
// exactly two decryption handles.
self.0.handles.get(0).unwrap()
self.0.handles.first().unwrap()
}

pub fn get_withdraw_withheld_authority_handle(&self) -> &DecryptHandle {
Expand Down

0 comments on commit 725ab37

Please sign in to comment.