[ci] Fix clippy warnings, enable clippy checks

This commit is contained in:
Steve Myers 2020-10-07 14:18:50 -07:00
parent 6402fd07c2
commit aea9abff8a
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051
15 changed files with 69 additions and 68 deletions

View File

@ -24,10 +24,11 @@ jobs:
include:
- rust: stable
features: default
clippy: false
clippy: true
test: true
- rust: 1.45.0
features: default
clippy: true
test: true
- rust: nightly
features: test-md-docs
@ -55,6 +56,7 @@ jobs:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
components: clippy
- name: build
uses: actions-rs/cargo@v1
with:

View File

@ -221,7 +221,11 @@ pub fn log_progress() -> LogProgress {
impl Progress for LogProgress {
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error> {
log::info!("Sync {:.3}%: `{}`", progress, message.unwrap_or("".into()));
log::info!(
"Sync {:.3}%: `{}`",
progress,
message.unwrap_or_else(|| "".into())
);
Ok(())
}

View File

@ -314,7 +314,7 @@ impl Database for Tree {
let is_internal = serde_json::from_value(val["i"].take())?;
Ok(UTXO {
outpoint: outpoint.clone(),
outpoint: *outpoint,
txout,
is_internal,
})

View File

@ -77,7 +77,7 @@ impl MapKey<'_> {
fn serialize_content(&self) -> Vec<u8> {
match self {
MapKey::Path((_, Some(child))) => u32::from(*child).to_be_bytes().to_vec(),
MapKey::Path((_, Some(child))) => child.to_be_bytes().to_vec(),
MapKey::Script(Some(s)) => serialize(*s),
MapKey::UTXO(Some(s)) => serialize(*s),
MapKey::RawTx(Some(s)) => serialize(*s),
@ -94,8 +94,8 @@ impl MapKey<'_> {
}
}
fn after(key: &Vec<u8>) -> Vec<u8> {
let mut key = key.clone();
fn after(key: &[u8]) -> Vec<u8> {
let mut key = key.to_owned();
let mut idx = key.len();
while idx > 0 {
if key[idx - 1] == 0xFF {
@ -233,7 +233,7 @@ impl BatchOperations for MemoryDatabase {
Some(b) => {
let (txout, is_internal) = b.downcast_ref().cloned().unwrap();
Ok(Some(UTXO {
outpoint: outpoint.clone(),
outpoint: *outpoint,
txout,
is_internal,
}))
@ -387,7 +387,7 @@ impl Database for MemoryDatabase {
Ok(self.map.get(&key).map(|b| {
let (txout, is_internal) = b.downcast_ref().cloned().unwrap();
UTXO {
outpoint: outpoint.clone(),
outpoint: *outpoint,
txout,
is_internal,
}
@ -424,9 +424,9 @@ impl Database for MemoryDatabase {
let key = MapKey::LastIndex(script_type).as_map_key();
let value = self
.map
.entry(key.clone())
.entry(key)
.and_modify(|x| *x.downcast_mut::<u32>().unwrap() += 1)
.or_insert(Box::<u32>::new(0))
.or_insert_with(|| Box::<u32>::new(0))
.downcast_mut()
.unwrap();
@ -445,8 +445,8 @@ impl BatchDatabase for MemoryDatabase {
for key in batch.deleted_keys {
self.map.remove(&key);
}
Ok(self.map.append(&mut batch.map))
self.map.append(&mut batch.map);
Ok(())
}
}

View File

@ -191,7 +191,7 @@ pub(crate) trait DatabaseUtils: Database {
self.get_raw_tx(&outpoint.txid)?
.map(|previous_tx| {
if outpoint.vout as usize >= previous_tx.output.len() {
Err(Error::InvalidOutpoint(outpoint.clone()))
Err(Error::InvalidOutpoint(*outpoint))
} else {
Ok(previous_tx.output[outpoint.vout as usize].clone())
}

View File

@ -57,7 +57,7 @@ impl From<crate::keys::KeyError> for Error {
match key_error {
crate::keys::KeyError::Miniscript(inner) => Error::Miniscript(inner),
crate::keys::KeyError::BIP32(inner) => Error::BIP32(inner),
e @ _ => Error::Key(e),
e => Error::Key(e),
}
}
}

View File

@ -78,8 +78,8 @@ impl ToWalletDescriptor for &str {
self,
network: Network,
) -> Result<(ExtendedDescriptor, KeyMap), KeyError> {
let descriptor = if self.contains("#") {
let parts: Vec<&str> = self.splitn(2, "#").collect();
let descriptor = if self.contains('#') {
let parts: Vec<&str> = self.splitn(2, '#').collect();
if !get_checksum(parts[0])
.ok()
.map(|computed| computed == parts[1])
@ -171,7 +171,7 @@ impl ToWalletDescriptor for (ExtendedDescriptor, KeyMap, ValidNetworks) {
DescriptorPublicKey::XPub(xpub)
}
other @ _ => other.clone(),
other => other.clone(),
};
Ok(pk)
@ -201,19 +201,19 @@ pub(crate) trait XKeyUtils {
impl<K: InnerXKey> XKeyUtils for DescriptorXKey<K> {
fn full_path(&self, append: &[ChildNumber]) -> DerivationPath {
let full_path = match &self.source {
&Some((_, ref path)) => path
let full_path = match self.source {
Some((_, ref path)) => path
.into_iter()
.chain(self.derivation_path.into_iter())
.cloned()
.collect(),
&None => self.derivation_path.clone(),
None => self.derivation_path.clone(),
};
if self.is_wildcard {
full_path
.into_iter()
.chain(append.into_iter())
.chain(append.iter())
.cloned()
.collect()
} else {
@ -222,9 +222,9 @@ impl<K: InnerXKey> XKeyUtils for DescriptorXKey<K> {
}
fn root_fingerprint(&self) -> Fingerprint {
match &self.source {
&Some((fingerprint, _)) => fingerprint.clone(),
&None => self.xkey.xkey_fingerprint(),
match self.source {
Some((fingerprint, _)) => fingerprint,
None => self.xkey.xkey_fingerprint(),
}
}
}

View File

@ -43,7 +43,7 @@
//! # Ok::<(), bdk::Error>(())
//! ```
use std::cmp::max;
use std::cmp::{max, Ordering};
use std::collections::{BTreeMap, HashSet, VecDeque};
use std::fmt;
use std::sync::Arc;
@ -155,7 +155,7 @@ impl SatisfiableItem {
}
}
fn combinations(vec: &Vec<usize>, size: usize) -> Vec<Vec<usize>> {
fn combinations(vec: &[usize], size: usize) -> Vec<Vec<usize>> {
assert!(vec.len() >= size);
let mut answer = Vec::new();
@ -344,8 +344,8 @@ impl Satisfaction {
.map(|i| {
conditions
.get(i)
.and_then(|set| Some(set.clone().into_iter().collect()))
.unwrap_or(vec![])
.map(|set| set.clone().into_iter().collect())
.unwrap_or_default()
})
.collect())
.into_iter()
@ -525,7 +525,7 @@ impl Policy {
}
fn make_multisig(
keys: &Vec<DescriptorPublicKey>,
keys: &[DescriptorPublicKey],
signers: Arc<SignersContainer<DescriptorPublicKey>>,
threshold: usize,
) -> Result<Option<Policy>, PolicyError> {
@ -542,7 +542,7 @@ impl Policy {
conditions: Default::default(),
};
for (index, key) in keys.iter().enumerate() {
if let Some(_) = signers.find(signer_id(key)) {
if signers.find(signer_id(key)).is_some() {
contribution.add(
&Satisfaction::Complete {
condition: Default::default(),
@ -582,7 +582,7 @@ impl Policy {
// if items.len() == threshold, selected can be omitted and we take all of them by default
let default = match &self.item {
SatisfiableItem::Thresh { items, threshold } if items.len() == *threshold => {
(0..*threshold).into_iter().collect()
(0..*threshold).collect()
}
_ => vec![],
};
@ -608,10 +608,14 @@ impl Policy {
// if we have something, make sure we have enough items. note that the user can set
// an empty value for this step in case of n-of-n, because `selected` is set to all
// the elements above
if selected.len() < *threshold {
return Err(PolicyError::NotEnoughItemsSelected(self.id.clone()));
} else if selected.len() > *threshold {
return Err(PolicyError::TooManyItemsSelected(self.id.clone()));
match selected.len().cmp(threshold) {
Ordering::Less => {
return Err(PolicyError::NotEnoughItemsSelected(self.id.clone()))
}
Ordering::Greater => {
return Err(PolicyError::TooManyItemsSelected(self.id.clone()))
}
Ordering::Equal => (),
}
// check the selected items, see if there are conflicting requirements
@ -676,7 +680,7 @@ fn signature_key(
) -> Policy {
let mut policy: Policy = SatisfiableItem::Signature(PKOrF::from_key_hash(*key_hash)).into();
if let Some(_) = signers.find(SignerId::PkHash(*key_hash)) {
if signers.find(SignerId::PkHash(*key_hash)).is_some() {
policy.contribution = Satisfaction::Complete {
condition: Default::default(),
}

View File

@ -122,7 +122,7 @@ impl From<crate::keys::KeyError> for Error {
crate::keys::KeyError::Miniscript(inner) => Error::Miniscript(inner),
crate::keys::KeyError::BIP32(inner) => Error::BIP32(inner),
crate::keys::KeyError::InvalidChecksum => Error::ChecksumMismatch,
e @ _ => Error::Key(e),
e => Error::Key(e),
}
}
}

View File

@ -562,12 +562,12 @@ pub mod test {
use super::*;
const test_entropy: [u8; 32] = [0xAA; 32];
const TEST_ENTROPY: [u8; 32] = [0xAA; 32];
#[test]
fn test_keys_generate_xprv() {
let generated_xprv: GeneratedKey<_, miniscript::Segwitv0> =
bip32::ExtendedPrivKey::generate_with_entropy((), test_entropy).unwrap();
bip32::ExtendedPrivKey::generate_with_entropy((), TEST_ENTROPY).unwrap();
assert_eq!(generated_xprv.valid_networks, any_network());
assert_eq!(generated_xprv.to_string(), "xprv9s21ZrQH143K4Xr1cJyqTvuL2FWR8eicgY9boWqMBv8MDVUZ65AXHnzBrK1nyomu6wdcabRgmGTaAKawvhAno1V5FowGpTLVx3jxzE5uk3Q");

View File

@ -42,7 +42,7 @@ extern crate async_trait;
#[macro_use]
extern crate bdk_macros;
#[cfg(any(test, feature = "compact_filters"))]
#[cfg(feature = "compact_filters")]
#[macro_use]
extern crate lazy_static;

View File

@ -39,8 +39,8 @@ pub enum ScriptType {
impl ScriptType {
pub fn as_byte(&self) -> u8 {
match self {
ScriptType::External => 'e' as u8,
ScriptType::Internal => 'i' as u8,
ScriptType::External => b'e',
ScriptType::Internal => b'i',
}
}

View File

@ -605,9 +605,7 @@ where
available_utxos,
use_all_utxos,
new_feerate,
fee_difference
.checked_sub(removed_change_output.value)
.unwrap_or(0),
fee_difference.saturating_sub(removed_change_output.value),
input_witness_weight,
0.0,
)?;
@ -620,7 +618,7 @@ where
// copy the n_sequence from the inputs that were already in the transaction
txin.iter_mut()
.for_each(|i| i.sequence = tx.input[0].sequence);
tx.input.extend_from_slice(&mut txin);
tx.input.extend_from_slice(&txin);
details.sent += selected_amount;
selected_amount
@ -666,7 +664,7 @@ where
.iter()
.map(|txin| {
Ok((
txin.previous_output.clone(),
txin.previous_output,
self.database
.borrow()
.get_previous_output(&txin.previous_output)?,
@ -765,7 +763,7 @@ where
.database
.borrow()
.get_tx(&input.previous_output.txid, false)?
.and_then(|tx| Some(tx.height.unwrap_or(std::u32::MAX)));
.map(|tx| tx.height.unwrap_or(std::u32::MAX));
let current_height = assume_height.or(self.current_height);
debug!(
@ -833,15 +831,13 @@ where
&self,
script_type: ScriptType,
) -> (&ExtendedDescriptor, ScriptType) {
let desc = match script_type {
match script_type {
ScriptType::Internal if self.change_descriptor.is_some() => (
self.change_descriptor.as_ref().unwrap(),
ScriptType::Internal,
),
_ => (&self.descriptor, ScriptType::External),
};
desc
}
}
fn get_descriptor_for_txout(&self, txout: &TxOut) -> Result<Option<ExtendedDescriptor>, Error> {
@ -941,7 +937,7 @@ where
) -> Result<(Vec<UTXO>, bool), Error> {
let unspendable_set = match unspendable {
None => HashSet::new(),
Some(vec) => vec.into_iter().collect(),
Some(vec) => vec.iter().collect(),
};
match utxo {
@ -1131,10 +1127,7 @@ where
if self
.database
.borrow()
.get_script_pubkey_from_path(
ScriptType::Internal,
max_address.checked_sub(1).unwrap_or(0),
)?
.get_script_pubkey_from_path(ScriptType::Internal, max_address.saturating_sub(1))?
.is_none()
{
run_setup = true;

View File

@ -205,7 +205,7 @@ impl Signer for DescriptorXKey<ExtendedPrivKey> {
.hd_keypaths
.iter()
.filter_map(|(pk, &(fingerprint, ref path))| {
if self.matches(fingerprint.clone(), &path).is_some() {
if self.matches(fingerprint, &path).is_some() {
Some((pk, path))
} else {
None
@ -284,7 +284,7 @@ impl Signer for PrivateKey {
}
fn descriptor_secret_key(&self) -> Option<DescriptorSecretKey> {
Some(DescriptorSecretKey::PrivKey(self.clone()))
Some(DescriptorSecretKey::PrivKey(*self))
}
}
@ -407,7 +407,7 @@ impl<Pk: MiniscriptKey> SignersContainer<Pk> {
Included(&(id, SignerOrdering(usize::MAX)).into()),
))
.map(|(_, v)| v)
.nth(0)
.next()
}
}
@ -431,9 +431,9 @@ impl ComputeSighash for Legacy {
let tx_input = &psbt.global.unsigned_tx.input[input_index];
let sighash = psbt_input.sighash_type.ok_or(SignerError::MissingSighash)?;
let script = match &psbt_input.redeem_script {
&Some(ref redeem_script) => redeem_script.clone(),
&None => {
let script = match psbt_input.redeem_script {
Some(ref redeem_script) => redeem_script.clone(),
None => {
let non_witness_utxo = psbt_input
.non_witness_utxo
.as_ref()
@ -485,9 +485,9 @@ impl ComputeSighash for Segwitv0 {
.ok_or(SignerError::MissingNonWitnessUtxo)?;
let value = witness_utxo.value;
let script = match &psbt_input.witness_script {
&Some(ref witness_script) => witness_script.clone(),
&None => {
let script = match psbt_input.witness_script {
Some(ref witness_script) => witness_script.clone(),
None => {
if witness_utxo.script_pubkey.is_v0_p2wpkh() {
p2wpkh_script_code(&witness_utxo.script_pubkey)
} else if psbt_input

View File

@ -24,8 +24,6 @@
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate serial_test;
pub use serial_test::serial;