Skip to content

Clear some clippy warnings #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
77eb4a3
Add clippy config file
tcharding May 2, 2022
e94bf6e
Remove redundant import statement
tcharding Apr 22, 2022
a1cf394
Use struct field shorthand
tcharding Apr 22, 2022
952af97
Remove unneeded unit return type
tcharding Apr 22, 2022
03c6587
Remove unneeded explicit reference
tcharding Apr 22, 2022
81d5ae6
Remove explicit return statement
tcharding Apr 22, 2022
52f7dce
Use & instead of `ref`
tcharding Apr 22, 2022
5647e29
Remove redundant slicing of whole range
tcharding Apr 22, 2022
8944ca0
Remove explicit lifetimes
tcharding Apr 22, 2022
be1a4ee
Use ok_or_else instead of ok_or
tcharding Apr 22, 2022
0a1b5e4
Do not use & for pattern match arms
tcharding Apr 22, 2022
d29d10a
Remove reference from expression and patterns
tcharding Apr 22, 2022
3fdd0f5
Remove useless conversion call from error return
tcharding Apr 22, 2022
63dee9e
Use iter instead of into_iter
tcharding Apr 22, 2022
738ff15
Remove redundant closures
tcharding Apr 22, 2022
7b91918
Use is_empty
tcharding Apr 22, 2022
ea57d6e
Use contains instead of manual implementation
tcharding Apr 22, 2022
ad51d83
Derive Default
tcharding Apr 22, 2022
35015bf
Remove redundant pattern matching
tcharding Apr 22, 2022
bc7be32
Remove unnecessary call to String::from
tcharding Apr 22, 2022
230cab9
Use map instead of and_then
tcharding Apr 22, 2022
54f2384
Remove redundant clone
tcharding Apr 22, 2022
592b479
Use copied
tcharding Apr 22, 2022
ebc4dc9
Use to_string instead of format macro
tcharding Apr 22, 2022
45fd5f7
Use push with character literal
tcharding Apr 22, 2022
54412f1
Simplify duplicate code
tcharding May 10, 2022
af2260f
Return block directly
tcharding May 2, 2022
98d4a06
Remove unneeded `ref`
tcharding May 2, 2022
da6e068
Remove the redundant calls to clone on Copy type
tcharding May 2, 2022
90ba692
Use unwrap_or_else instead of unwrap_or
tcharding May 2, 2022
289a1a8
Use flat_map instead of chaining map and flatten
tcharding May 2, 2022
769cd6d
Do not check for < 0 for usize types
tcharding May 2, 2022
b75964c
Use sort_unstable on primitive types
tcharding May 2, 2022
17bd217
Use find instead of chaining filter and next
tcharding May 2, 2022
7da5327
Use semvar-compliant version number
tcharding May 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msrv = "1.41.1"
6 changes: 3 additions & 3 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
pub fn new(ms: Miniscript<Pk, BareCtx>) -> Result<Self, Error> {
// do the top-level checks
BareCtx::top_level_checks(&ms)?;
Ok(Self { ms: ms })
Ok(Self { ms })
}

/// get the inner
Expand Down Expand Up @@ -111,7 +111,7 @@ where
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
{
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
let sub = Miniscript::<Pk, BareCtx>::from_tree(&top)?;
let sub = Miniscript::<Pk, BareCtx>::from_tree(top)?;
BareCtx::top_level_checks(&sub)?;
Bare::new(sub)
}
Expand Down Expand Up @@ -241,7 +241,7 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
/// Create a new Pkh descriptor
pub fn new(pk: Pk) -> Self {
// do the top-level checks
Self { pk: pk }
Self { pk }
}

/// Get a reference to the inner key
Expand Down
7 changes: 3 additions & 4 deletions src/descriptor/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ pub fn desc_checksum(desc: &str) -> Result<String, Error> {
let mut clscount = 0;

for ch in desc.chars() {
let pos = INPUT_CHARSET.find(ch).ok_or(Error::BadDescriptor(format!(
"Invalid character in checksum: '{}'",
ch
)))? as u64;
let pos = INPUT_CHARSET.find(ch).ok_or_else(|| {
Error::BadDescriptor(format!("Invalid character in checksum: '{}'", ch))
})? as u64;
c = poly_mod(c, pos & 31);
cls = cls * 3 + (pos >> 5);
clscount += 1;
Expand Down
21 changes: 10 additions & 11 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ pub enum SinglePubKey {
impl fmt::Display for DescriptorSecretKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&DescriptorSecretKey::Single(ref sk) => {
DescriptorSecretKey::Single(ref sk) => {
maybe_fmt_master_id(f, &sk.origin)?;
sk.key.fmt(f)?;
Ok(())
}
&DescriptorSecretKey::XPrv(ref xprv) => {
DescriptorSecretKey::XPrv(ref xprv) => {
maybe_fmt_master_id(f, &xprv.origin)?;
xprv.xkey.fmt(f)?;
fmt_derivation_path(f, &xprv.derivation_path)?;
Expand Down Expand Up @@ -172,9 +172,9 @@ impl DescriptorXKey<bip32::ExtendedPrivKey> {

let xprv = self
.xkey
.derive_priv(&secp, &hardened_path)
.derive_priv(secp, &hardened_path)
.map_err(|_| DescriptorKeyParseError("Unable to derive the hardened steps"))?;
let xpub = bip32::ExtendedPubKey::from_priv(&secp, &xprv);
let xpub = bip32::ExtendedPubKey::from_priv(secp, &xprv);

let origin = match &self.origin {
Some((fingerprint, path)) => Some((
Expand All @@ -188,7 +188,7 @@ impl DescriptorXKey<bip32::ExtendedPrivKey> {
if hardened_path.is_empty() {
None
} else {
Some((self.xkey.fingerprint(&secp), hardened_path.into()))
Some((self.xkey.fingerprint(secp), hardened_path.into()))
}
}
};
Expand Down Expand Up @@ -562,7 +562,7 @@ impl<K: InnerXKey> DescriptorXKey<K> {
DescriptorKeyParseError("Malformed master fingerprint, expected 8 hex chars")
})?;
let origin_path = raw_origin
.map(|p| bip32::ChildNumber::from_str(p))
.map(bip32::ChildNumber::from_str)
.collect::<Result<bip32::DerivationPath, bip32::Error>>()
.map_err(|_| {
DescriptorKeyParseError("Error while parsing master derivation path")
Expand Down Expand Up @@ -657,21 +657,20 @@ impl<K: InnerXKey> DescriptorXKey<K> {
) -> Option<bip32::DerivationPath> {
let (fingerprint, path) = keysource;

let (compare_fingerprint, compare_path) = match &self.origin {
&Some((fingerprint, ref path)) => (
let (compare_fingerprint, compare_path) = match self.origin {
Some((fingerprint, ref path)) => (
fingerprint,
path.into_iter()
.chain(self.derivation_path.into_iter())
.collect(),
),
&None => (
None => (
self.xkey.xkey_fingerprint(secp),
self.derivation_path.into_iter().collect::<Vec<_>>(),
),
};

let path_excluding_wildcard = if self.wildcard != Wildcard::None && path.as_ref().len() > 0
{
let path_excluding_wildcard = if self.wildcard != Wildcard::None && !path.is_empty() {
path.into_iter()
.take(path.as_ref().len() - 1)
.cloned()
Expand Down
4 changes: 2 additions & 2 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ impl Descriptor<DescriptorPublicKey> {
key_map: &mut KeyMap|
-> Result<DescriptorPublicKey, DescriptorKeyParseError> {
let (public_key, secret_key) = match DescriptorSecretKey::from_str(s) {
Ok(sk) => (sk.to_public(&secp)?, Some(sk)),
Ok(sk) => (sk.to_public(secp)?, Some(sk)),
Err(_) => (DescriptorPublicKey::from_str(s)?, None),
};

Expand Down Expand Up @@ -774,7 +774,7 @@ impl Descriptor<DescriptorPublicKey> {
let range = if self.is_deriveable() { range } else { 0..1 };

for i in range {
let concrete = self.derived_descriptor(&secp, i)?;
let concrete = self.derived_descriptor(secp, i)?;
if &concrete.script_pubkey() == script_pubkey {
return Ok(Some((i, concrete)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/descriptor/pretaproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ pub(crate) mod traits {
{
// This expect can technically be avoided if we implement this for types, but
// having this expect saves lots of LoC because of default implementation
<Self as DescriptorTrait<Pk>>::explicit_script(&self)
<Self as DescriptorTrait<Pk>>::explicit_script(self)
.expect("Pre taproot descriptor have explicit script")
}

Expand All @@ -269,7 +269,7 @@ pub(crate) mod traits {
where
Pk: ToPublicKey,
{
<Self as DescriptorTrait<Pk>>::script_code(&self)
<Self as DescriptorTrait<Pk>>::script_code(self)
.expect("Pre taproot descriptor have non-failing script code")
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/descriptor/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ where
let top = &top.args[0];
if top.name == "sortedmulti" {
return Ok(Wsh {
inner: WshInner::SortedMulti(SortedMultiVec::from_tree(&top)?),
inner: WshInner::SortedMulti(SortedMultiVec::from_tree(top)?),
});
}
let sub = Miniscript::from_tree(&top)?;
let sub = Miniscript::from_tree(top)?;
Segwitv0::top_level_checks(&sub)?;
Ok(Wsh {
inner: WshInner::Ms(sub),
Expand Down Expand Up @@ -313,7 +313,7 @@ where
WshInner::SortedMulti(ref smv) => WshInner::SortedMulti(smv.translate_pk(&mut fpk)?),
WshInner::Ms(ref ms) => WshInner::Ms(ms.translate_pk(&mut fpk, &mut fpkh)?),
};
Ok(Wsh { inner: inner })
Ok(Wsh { inner })
}
}

Expand All @@ -333,7 +333,7 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
pk.to_string(),
)))
} else {
Ok(Self { pk: pk })
Ok(Self { pk })
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/descriptor/sh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ where
if top.name == "sh" && top.args.len() == 1 {
let top = &top.args[0];
let inner = match top.name {
"wsh" => ShInner::Wsh(Wsh::from_tree(&top)?),
"wpkh" => ShInner::Wpkh(Wpkh::from_tree(&top)?),
"sortedmulti" => ShInner::SortedMulti(SortedMultiVec::from_tree(&top)?),
"wsh" => ShInner::Wsh(Wsh::from_tree(top)?),
"wpkh" => ShInner::Wpkh(Wpkh::from_tree(top)?),
"sortedmulti" => ShInner::SortedMulti(SortedMultiVec::from_tree(top)?),
_ => {
let sub = Miniscript::from_tree(&top)?;
let sub = Miniscript::from_tree(top)?;
Legacy::top_level_checks(&sub)?;
ShInner::Ms(sub)
}
};
Ok(Sh { inner: inner })
Ok(Sh { inner })
} else {
Err(Error::Unexpected(format!(
"{}({} args) while parsing sh descriptor",
Expand Down Expand Up @@ -439,6 +439,6 @@ where
ShInner::SortedMulti(ref smv) => ShInner::SortedMulti(smv.translate_pk(&mut fpk)?),
ShInner::Ms(ref ms) => ShInner::Ms(ms.translate_pk(&mut fpk, &mut fpkh)?),
};
Ok(Sh { inner: inner })
Ok(Sh { inner })
}
}
36 changes: 16 additions & 20 deletions src/descriptor/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ where
let (depth, last) = self.stack.pop().expect("Size checked above");
match &*last {
TapTree::Tree(l, r) => {
self.stack.push((depth + 1, &r));
self.stack.push((depth + 1, &l));
self.stack.push((depth + 1, r));
self.stack.push((depth + 1, l));
}
TapTree::Leaf(ref ms) => return Some((depth, ms)),
}
Expand Down Expand Up @@ -360,12 +360,10 @@ where
let right = parse_tr_script_spend(&args[1])?;
Ok(TapTree::Tree(Arc::new(left), Arc::new(right)))
}
_ => {
return Err(Error::Unexpected(
"unknown format for script spending paths while parsing taproot descriptor"
.to_string(),
));
}
_ => Err(Error::Unexpected(
"unknown format for script spending paths while parsing taproot descriptor"
.to_string(),
)),
}
}

Expand All @@ -386,14 +384,14 @@ where
})
}
2 => {
let ref key = top.args[0];
let key = &top.args[0];
if !key.args.is_empty() {
return Err(Error::Unexpected(format!(
"#{} script associated with `key-path` while parsing taproot descriptor",
key.args.len()
)));
}
let ref tree = top.args[1];
let tree = &top.args[1];
let ret = parse_tr_script_spend(tree)?;
Ok(Tr {
internal_key: expression::terminal(key, Pk::from_str)?,
Expand Down Expand Up @@ -460,7 +458,7 @@ fn parse_tr_tree(s: &str) -> Result<expression::Tree, Error> {
}
}

let ret = if s.len() > 3 && &s[..3] == "tr(" && s.as_bytes()[s.len() - 1] == b')' {
if s.len() > 3 && &s[..3] == "tr(" && s.as_bytes()[s.len() - 1] == b')' {
let rest = &s[3..s.len() - 1];
if !rest.contains(',') {
let internal_key = expression::Tree {
Expand Down Expand Up @@ -497,12 +495,11 @@ fn parse_tr_tree(s: &str) -> Result<expression::Tree, Error> {
}
} else {
Err(Error::Unexpected("invalid taproot descriptor".to_string()))
};
return ret;
}
}

fn split_once(inp: &str, delim: char) -> Option<(&str, &str)> {
let ret = if inp.is_empty() {
if inp.is_empty() {
None
} else {
let mut found = inp.len();
Expand All @@ -514,12 +511,11 @@ fn split_once(inp: &str, delim: char) -> Option<(&str, &str)> {
}
// No comma or trailing comma found
if found >= inp.len() - 1 {
Some((&inp[..], ""))
Some((inp, ""))
} else {
Some((&inp[..found], &inp[found + 1..]))
}
};
return ret;
}
}

impl<Pk: MiniscriptKey> Liftable<Pk> for TapTree<Pk> {
Expand All @@ -533,7 +529,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for TapTree<Pk> {
}
}

let pol = lift_helper(&self)?;
let pol = lift_helper(self)?;
Ok(pol.normalized())
}
}
Expand Down Expand Up @@ -594,15 +590,15 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
Pk: ToPublicKey,
S: Satisfier<Pk>,
{
best_tap_spend(&self, satisfier, false /* allow_mall */)
best_tap_spend(self, satisfier, false /* allow_mall */)
}

fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
where
Pk: ToPublicKey,
S: Satisfier<Pk>,
{
best_tap_spend(&self, satisfier, true /* allow_mall */)
best_tap_spend(self, satisfier, true /* allow_mall */)
}

fn max_satisfaction_weight(&self) -> Result<usize, Error> {
Expand Down
4 changes: 2 additions & 2 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a> Tree<'a> {
// String-ending terminal
Found::Nothing => Ok((
Tree {
name: &sl[..],
name: sl,
args: vec![],
},
"",
Expand Down Expand Up @@ -195,7 +195,7 @@ impl<'a> Tree<'a> {
pub fn parse_num(s: &str) -> Result<u32, Error> {
if s.len() > 1 {
let ch = s.chars().next().unwrap();
if ch < '1' || ch > '9' {
if !('1'..='9').contains(&ch) {
return Err(Error::Unexpected(
"Number must start with a digit 1-9".to_string(),
));
Expand Down
16 changes: 8 additions & 8 deletions src/interpreter/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn pk_from_slice(slice: &[u8], require_compressed: bool) -> Result<bitcoin::Publ
}
}

fn pk_from_stackelem<'a>(
elem: &stack::Element<'a>,
fn pk_from_stackelem(
elem: &stack::Element<'_>,
require_compressed: bool,
) -> Result<bitcoin::PublicKey, Error> {
let slice = if let stack::Element::Push(slice) = *elem {
Expand All @@ -51,8 +51,8 @@ fn pk_from_stackelem<'a>(

// Parse the script with appropriate context to check for context errors like
// correct usage of x-only keys or multi_a
fn script_from_stackelem<'a, Ctx: ScriptContext>(
elem: &stack::Element<'a>,
fn script_from_stackelem<Ctx: ScriptContext>(
elem: &stack::Element<'_>,
) -> Result<Miniscript<Ctx::Key, Ctx>, Error> {
match *elem {
stack::Element::Push(sl) => {
Expand Down Expand Up @@ -212,7 +212,7 @@ pub(super) fn from_txdata<'txin>(
let has_annex = wit_stack
.last()
.and_then(|x| x.as_push().ok())
.map(|x| x.len() > 0 && x[0] == TAPROOT_ANNEX_PREFIX)
.map(|x| !x.is_empty() && x[0] == TAPROOT_ANNEX_PREFIX)
.unwrap_or(false);
let has_annex = has_annex && (wit_stack.len() >= 2);
if has_annex {
Expand All @@ -233,8 +233,8 @@ pub(super) fn from_txdata<'txin>(
let ctrl_blk = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?;
let ctrl_blk = ctrl_blk.as_push()?;
let tap_script = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?;
let ctrl_blk = ControlBlock::from_slice(ctrl_blk)
.map_err(|e| Error::ControlBlockParse(e))?;
let ctrl_blk =
ControlBlock::from_slice(ctrl_blk).map_err(Error::ControlBlockParse)?;
let tap_script = script_from_stackelem::<Tap>(&tap_script)?;
let ms = tap_script.to_no_checks_ms();
// Creating new contexts is cheap
Expand All @@ -255,7 +255,7 @@ pub(super) fn from_txdata<'txin>(
Some(tap_script),
))
} else {
return Err(Error::ControlBlockVerificationError);
Err(Error::ControlBlockVerificationError)
}
}
}
Expand Down
Loading