From 13fc5182685d1436b406729c428053658792efbd Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Wed, 2 Oct 2019 18:04:18 -0700 Subject: [PATCH] Clippy work towards rust 1.38 (#6219) --- client/src/rpc_client.rs | 21 +++++------ client/src/rpc_client_request.rs | 7 ++-- client/src/thin_client.rs | 4 +- drone/src/drone.rs | 11 ++---- drone/src/drone_mock.rs | 13 ++++--- install/src/command.rs | 14 +++---- install/src/update_manifest.rs | 8 ++-- programs/config_api/src/config_processor.rs | 16 ++++---- .../librapay_api/src/librapay_transaction.rs | 2 +- programs/storage_api/src/storage_contract.rs | 37 ++++++++++--------- programs/storage_api/src/storage_processor.rs | 14 +++---- programs/vote_api/src/vote_instruction.rs | 6 +-- 12 files changed, 75 insertions(+), 78 deletions(-) diff --git a/client/src/rpc_client.rs b/client/src/rpc_client.rs index 195c6331f..15cd9a1b2 100644 --- a/client/src/rpc_client.rs +++ b/client/src/rpc_client.rs @@ -61,9 +61,11 @@ impl RpcClient { Err(io::Error::new( io::ErrorKind::Other, "Received result of an unexpected type", - ))?; + ) + .into()) + } else { + Ok(signature.as_str().unwrap().to_string()) } - Ok(signature.as_str().unwrap().to_string()) } pub fn get_signature_status( @@ -195,14 +197,11 @@ impl RpcClient { send_retries - 1 }; if send_retries == 0 { - if status.is_some() { - status.unwrap()? - } else { - Err(io::Error::new( - io::ErrorKind::Other, - format!("Transaction {:?} failed: {:?}", signature_str, status), - ))?; - } + return Err(io::Error::new( + io::ErrorKind::Other, + format!("Transaction {:?} failed: {:?}", signature_str, status), + ) + .into()); } } } @@ -262,7 +261,7 @@ impl RpcClient { } if send_retries == 0 { - Err(io::Error::new(io::ErrorKind::Other, "Transactions failed"))?; + return Err(io::Error::new(io::ErrorKind::Other, "Transactions failed").into()); } send_retries -= 1; diff --git a/client/src/rpc_client_request.rs b/client/src/rpc_client_request.rs index cb1f534bf..ce7fb0d29 100644 --- a/client/src/rpc_client_request.rs +++ b/client/src/rpc_client_request.rs @@ -53,10 +53,11 @@ impl GenericRpcClientRequest for RpcClientRequest { Ok(mut response) => { let json: serde_json::Value = serde_json::from_str(&response.text()?)?; if json["error"].is_object() { - Err(RpcError::RpcRequestError(format!( + return Err(RpcError::RpcRequestError(format!( "RPC Error response: {}", serde_json::to_string(&json["error"]).unwrap() - )))? + )) + .into()); } return Ok(json["result"].clone()); } @@ -66,7 +67,7 @@ impl GenericRpcClientRequest for RpcClientRequest { request, retries, e ); if retries == 0 { - Err(e)?; + return Err(e.into()); } retries -= 1; diff --git a/client/src/thin_client.rs b/client/src/thin_client.rs index 5c18ef82e..bf45a66b6 100644 --- a/client/src/thin_client.rs +++ b/client/src/thin_client.rs @@ -339,7 +339,7 @@ impl SyncClient for ThinClient { } Err(e) => { self.optimizer.report(index, std::u64::MAX); - Err(e)? + Err(e.into()) } } } @@ -380,7 +380,7 @@ impl SyncClient for ThinClient { } Err(e) => { self.optimizer.report(index, std::u64::MAX); - Err(e)? + Err(e.into()) } } } diff --git a/drone/src/drone.rs b/drone/src/drone.rs index 8fa863ed7..25c57f5f6 100644 --- a/drone/src/drone.rs +++ b/drone/src/drone.rs @@ -213,13 +213,13 @@ pub fn request_airdrop_transaction( })?; let transaction_length = LittleEndian::read_u16(&buffer) as usize; if transaction_length >= PACKET_DATA_SIZE { - Err(Error::new( + return Err(Error::new( ErrorKind::Other, format!( "request_airdrop_transaction: invalid transaction_length from drone: {}", transaction_length ), - ))?; + )); } // Read the transaction @@ -265,11 +265,8 @@ pub fn run_drone( send_addr: Option>, ) { let socket = TcpListener::bind(&drone_addr).unwrap(); - if send_addr.is_some() { - send_addr - .unwrap() - .send(socket.local_addr().unwrap()) - .unwrap(); + if let Some(send_addr) = send_addr { + send_addr.send(socket.local_addr().unwrap()).unwrap(); } info!("Drone started. Listening on: {}", drone_addr); let done = socket diff --git a/drone/src/drone_mock.rs b/drone/src/drone_mock.rs index 6f6440fdf..a7b9a36fe 100644 --- a/drone/src/drone_mock.rs +++ b/drone/src/drone_mock.rs @@ -13,11 +13,12 @@ pub fn request_airdrop_transaction( _blockhash: Hash, ) -> Result { if lamports == 0 { - Err(Error::new(ErrorKind::Other, "Airdrop failed"))? + Err(Error::new(ErrorKind::Other, "Airdrop failed")) + } else { + let key = Keypair::new(); + let to = Pubkey::new_rand(); + let blockhash = Hash::default(); + let tx = system_transaction::create_user_account(&key, &to, lamports, blockhash); + Ok(tx) } - let key = Keypair::new(); - let to = Pubkey::new_rand(); - let blockhash = Hash::default(); - let tx = system_transaction::create_user_account(&key, &to, lamports, blockhash); - Ok(tx) } diff --git a/install/src/command.rs b/install/src/command.rs index 322424c64..b42337721 100644 --- a/install/src/command.rs +++ b/install/src/command.rs @@ -127,7 +127,7 @@ fn download_to_temp_archive( .map_err(|err| format!("Unable to hash {:?}: {}", temp_file, err))?; if expected_sha256.is_some() && expected_sha256 != Some(&temp_file_sha256) { - Err(io::Error::new(io::ErrorKind::Other, "Incorrect hash"))?; + return Err(io::Error::new(io::ErrorKind::Other, "Incorrect hash").into()); } source.progress_bar.finish_and_clear(); @@ -651,7 +651,7 @@ pub fn deploy( })?; progress_bar.finish_and_clear(); if balance.unwrap_or(0) == 0 { - Err(format!("{} account balance is empty", from_keypair_file))?; + return Err(format!("{} account balance is empty", from_keypair_file)); } // Download the release @@ -780,12 +780,12 @@ pub fn update(config_file: &str) -> Result { if timestamp_secs() < u64::from_str_radix(crate::build_env::BUILD_SECONDS_SINCE_UNIX_EPOCH, 10).unwrap() { - Err("Unable to update as system time seems unreliable".to_string())? + return Err("Unable to update as system time seems unreliable".to_string()); } if let Some(ref current_update_manifest) = config.current_update_manifest { if update_manifest.timestamp_secs < current_update_manifest.timestamp_secs { - Err("Unable to update to an older version".to_string())? + return Err("Unable to update to an older version".to_string()); } } let release_dir = config.release_dir(&update_manifest.download_sha256); @@ -818,7 +818,7 @@ pub fn update(config_file: &str) -> Result { })?; if release_target != crate::build_env::TARGET { - Err(format!("Incompatible update target: {}", release_target))?; + return Err(format!("Incompatible update target: {}", release_target)); } let _ = fs::remove_dir_all(config.active_release_dir()); @@ -854,10 +854,10 @@ pub fn run( } if !full_program_path.exists() { - Err(format!( + return Err(format!( "{} does not exist", full_program_path.to_str().unwrap() - ))?; + )); } let mut child_option: Option = None; diff --git a/install/src/update_manifest.rs b/install/src/update_manifest.rs index 2a6a2e16f..b10de79ab 100644 --- a/install/src/update_manifest.rs +++ b/install/src/update_manifest.rs @@ -47,12 +47,10 @@ impl SignedUpdateManifest { let mut manifest: SignedUpdateManifest = bincode::deserialize(input)?; manifest.account_pubkey = *account_pubkey; if !manifest.verify() { - Err(io::Error::new( - io::ErrorKind::Other, - "Manifest failed to verify", - ))?; + Err(io::Error::new(io::ErrorKind::Other, "Manifest failed to verify").into()) + } else { + Ok(manifest) } - Ok(manifest) } } diff --git a/programs/config_api/src/config_processor.rs b/programs/config_api/src/config_processor.rs index 1cadbbe90..553bb8c54 100644 --- a/programs/config_api/src/config_processor.rs +++ b/programs/config_api/src/config_processor.rs @@ -33,7 +33,7 @@ pub fn process_instruction( // or when no signers specified in Config data if keyed_accounts[0].signer_key().is_none() { error!("account[0].signer_key().is_none()"); - Err(InstructionError::MissingRequiredSignature)?; + return Err(InstructionError::MissingRequiredSignature); } } @@ -50,19 +50,19 @@ pub fn process_instruction( let signer_account = keyed_accounts.get(account_index); if signer_account.is_none() { error!("account {:?} is not in account list", signer); - Err(InstructionError::MissingRequiredSignature)?; + return Err(InstructionError::MissingRequiredSignature); } let signer_key = signer_account.unwrap().signer_key(); if signer_key.is_none() { error!("account {:?} signer_key().is_none()", signer); - Err(InstructionError::MissingRequiredSignature)?; + return Err(InstructionError::MissingRequiredSignature); } if signer_key.unwrap() != signer { error!( "account[{:?}].signer_key() does not match Config data)", account_index ); - Err(InstructionError::MissingRequiredSignature)?; + return Err(InstructionError::MissingRequiredSignature); } // If Config account is already initialized, update signatures must match Config data if !current_data.keys.is_empty() @@ -72,11 +72,11 @@ pub fn process_instruction( .is_none() { error!("account {:?} is not in stored signer list", signer); - Err(InstructionError::MissingRequiredSignature)?; + return Err(InstructionError::MissingRequiredSignature); } } else if keyed_accounts[0].signer_key().is_none() { error!("account[0].signer_key().is_none()"); - Err(InstructionError::MissingRequiredSignature)?; + return Err(InstructionError::MissingRequiredSignature); } } @@ -87,12 +87,12 @@ pub fn process_instruction( counter, current_signer_keys.len() ); - Err(InstructionError::MissingRequiredSignature)?; + return Err(InstructionError::MissingRequiredSignature); } if keyed_accounts[0].account.data.len() < data.len() { error!("instruction data too large"); - Err(InstructionError::InvalidInstructionData)?; + return Err(InstructionError::InvalidInstructionData); } keyed_accounts[0].account.data[0..data.len()].copy_from_slice(&data); diff --git a/programs/librapay_api/src/librapay_transaction.rs b/programs/librapay_api/src/librapay_transaction.rs index 37bb2c88b..2f21714ec 100644 --- a/programs/librapay_api/src/librapay_transaction.rs +++ b/programs/librapay_api/src/librapay_transaction.rs @@ -121,7 +121,7 @@ pub fn get_libra_balance( } state => { info!("Unknown account state: {:?}", state); - return Err(LibrapayError::UnknownAccountState)?; + return Err(LibrapayError::UnknownAccountState.into()); } } let resource = data_store diff --git a/programs/storage_api/src/storage_contract.rs b/programs/storage_api/src/storage_contract.rs index 4d7772536..08d40d7f9 100644 --- a/programs/storage_api/src/storage_contract.rs +++ b/programs/storage_api/src/storage_contract.rs @@ -141,7 +141,7 @@ impl<'a> StorageAccount<'a> { }; self.account.set_state(storage_contract) } else { - Err(InstructionError::AccountAlreadyInitialized)? + Err(InstructionError::AccountAlreadyInitialized) } } @@ -157,7 +157,7 @@ impl<'a> StorageAccount<'a> { }; self.account.set_state(storage_contract) } else { - Err(InstructionError::AccountAlreadyInitialized)? + Err(InstructionError::AccountAlreadyInitialized) } } @@ -234,7 +234,7 @@ impl<'a> StorageAccount<'a> { segment_proofs.push(proof); self.account.set_state(storage_contract) } else { - Err(InstructionError::InvalidArgument)? + Err(InstructionError::InvalidArgument) } } @@ -270,7 +270,7 @@ impl<'a> StorageAccount<'a> { credits.current_epoch += total_validations; self.account.set_state(storage_contract) } else { - Err(InstructionError::InvalidArgument)? + Err(InstructionError::InvalidArgument) } } @@ -360,7 +360,7 @@ impl<'a> StorageAccount<'a> { self.account.set_state(storage_contract) } else { - Err(InstructionError::InvalidArgument)? + Err(InstructionError::InvalidArgument) } } @@ -380,9 +380,9 @@ impl<'a> StorageAccount<'a> { } = &mut storage_contract { if owner.id != *account_owner { - Err(InstructionError::CustomError( + return Err(InstructionError::CustomError( StorageError::InvalidOwner as u32, - ))? + )); } credits.update_epoch(clock.epoch); @@ -397,9 +397,9 @@ impl<'a> StorageAccount<'a> { } = &mut storage_contract { if owner.id != *account_owner { - Err(InstructionError::CustomError( + return Err(InstructionError::CustomError( StorageError::InvalidOwner as u32, - ))? + )); } credits.update_epoch(clock.epoch); let (num_validations, _total_proofs) = count_valid_proofs(&validations); @@ -409,7 +409,7 @@ impl<'a> StorageAccount<'a> { self.account.set_state(storage_contract) } else { - Err(InstructionError::InvalidArgument)? + Err(InstructionError::InvalidArgument) } } } @@ -424,15 +424,16 @@ fn check_redeemable( if rewards_pool.account.lamports < rewards { Err(InstructionError::CustomError( StorageError::RewardPoolDepleted as u32, - ))? + )) + } else { + if rewards >= 1 { + rewards_pool.account.lamports -= rewards; + owner.account.lamports += rewards; + //clear credits + credits.redeemable = 0; + } + Ok(()) } - if rewards >= 1 { - rewards_pool.account.lamports -= rewards; - owner.account.lamports += rewards; - //clear credits - credits.redeemable = 0; - } - Ok(()) } pub fn create_rewards_pool() -> Account { diff --git a/programs/storage_api/src/storage_processor.rs b/programs/storage_api/src/storage_processor.rs index 2c4c46f4e..2c40f5177 100644 --- a/programs/storage_api/src/storage_processor.rs +++ b/programs/storage_api/src/storage_processor.rs @@ -22,13 +22,13 @@ pub fn process_instruction( match bincode::deserialize(data).map_err(|_| InstructionError::InvalidInstructionData)? { StorageInstruction::InitializeReplicatorStorage { owner } => { if !rest.is_empty() { - Err(InstructionError::InvalidArgument)?; + return Err(InstructionError::InvalidArgument); } storage_account.initialize_replicator_storage(owner) } StorageInstruction::InitializeValidatorStorage { owner } => { if !rest.is_empty() { - Err(InstructionError::InvalidArgument)?; + return Err(InstructionError::InvalidArgument); } storage_account.initialize_validator_storage(owner) } @@ -40,7 +40,7 @@ pub fn process_instruction( } => { if me_unsigned || rest.len() != 1 { // This instruction must be signed by `me` - Err(InstructionError::InvalidArgument)?; + return Err(InstructionError::InvalidArgument); } let clock = sysvar::clock::from_keyed_account(&rest[0])?; storage_account.submit_mining_proof( @@ -54,14 +54,14 @@ pub fn process_instruction( StorageInstruction::AdvertiseStorageRecentBlockhash { hash, segment } => { if me_unsigned || rest.len() != 1 { // This instruction must be signed by `me` - Err(InstructionError::InvalidArgument)?; + return Err(InstructionError::InvalidArgument); } let clock = sysvar::clock::from_keyed_account(&rest[0])?; storage_account.advertise_storage_recent_blockhash(hash, segment, clock) } StorageInstruction::ClaimStorageReward => { if rest.len() != 4 { - Err(InstructionError::InvalidArgument)?; + return Err(InstructionError::InvalidArgument); } let (clock, rest) = rest.split_at_mut(1); let (rewards, rest) = rest.split_at_mut(1); @@ -75,13 +75,13 @@ pub fn process_instruction( } StorageInstruction::ProofValidation { segment, proofs } => { if rest.is_empty() { - Err(InstructionError::InvalidArgument)?; + return Err(InstructionError::InvalidArgument); } let (clock, rest) = rest.split_at_mut(1); if me_unsigned || rest.is_empty() { // This instruction must be signed by `me` and `rest` cannot be empty - Err(InstructionError::InvalidArgument)?; + return Err(InstructionError::InvalidArgument); } let me_id = storage_account.id; let clock = sysvar::clock::from_keyed_account(&clock[0])?; diff --git a/programs/vote_api/src/vote_instruction.rs b/programs/vote_api/src/vote_instruction.rs index fce9f37a2..0c1fd0b72 100644 --- a/programs/vote_api/src/vote_instruction.rs +++ b/programs/vote_api/src/vote_instruction.rs @@ -166,7 +166,7 @@ pub fn process_instruction( trace!("keyed_accounts: {:?}", keyed_accounts); if keyed_accounts.is_empty() { - Err(InstructionError::InvalidInstructionData)?; + return Err(InstructionError::InvalidInstructionData); } // 0th index is vote account @@ -184,7 +184,7 @@ pub fn process_instruction( VoteInstruction::Vote(vote) => { datapoint_info!("vote-native", ("count", 1, i64)); if rest.len() < 2 { - Err(InstructionError::InvalidInstructionData)?; + return Err(InstructionError::InvalidInstructionData); } let (slot_hashes_and_clock, other_signers) = rest.split_at_mut(2); @@ -198,7 +198,7 @@ pub fn process_instruction( } VoteInstruction::Withdraw(lamports) => { if rest.is_empty() { - Err(InstructionError::InvalidInstructionData)?; + return Err(InstructionError::InvalidInstructionData); } let (to, rest) = rest.split_at_mut(1); let to = &mut to[0];