Skip to content

Commit 9ab6d4d

Browse files
committed
Merge #367: Clear some clippy warnings
7da5327 Use semvar-compliant version number (Tobin C. Harding) 17bd217 Use find instead of chaining filter and next (Tobin C. Harding) b75964c Use sort_unstable on primitive types (Tobin C. Harding) 769cd6d Do not check for < 0 for usize types (Tobin C. Harding) 289a1a8 Use flat_map instead of chaining map and flatten (Tobin C. Harding) 90ba692 Use unwrap_or_else instead of unwrap_or (Tobin C. Harding) da6e068 Remove the redundant calls to clone on Copy type (Tobin C. Harding) 98d4a06 Remove unneeded `ref` (Tobin C. Harding) af2260f Return block directly (Tobin C. Harding) 54412f1 Simplify duplicate code (Tobin C. Harding) 45fd5f7 Use push with character literal (Tobin C. Harding) ebc4dc9 Use to_string instead of format macro (Tobin C. Harding) 592b479 Use copied (Tobin C. Harding) 54f2384 Remove redundant clone (Tobin C. Harding) 230cab9 Use map instead of and_then (Tobin C. Harding) bc7be32 Remove unnecessary call to String::from (Tobin C. Harding) 35015bf Remove redundant pattern matching (Tobin C. Harding) ad51d83 Derive Default (Tobin C. Harding) ea57d6e Use contains instead of manual implementation (Tobin C. Harding) 7b91918 Use is_empty (Tobin C. Harding) 738ff15 Remove redundant closures (Tobin C. Harding) 63dee9e Use iter instead of into_iter (Tobin C. Harding) 3fdd0f5 Remove useless conversion call from error return (Tobin C. Harding) d29d10a Remove reference from expression and patterns (Tobin C. Harding) 0a1b5e4 Do not use & for pattern match arms (Tobin C. Harding) be1a4ee Use ok_or_else instead of ok_or (Tobin C. Harding) 8944ca0 Remove explicit lifetimes (Tobin C. Harding) 5647e29 Remove redundant slicing of whole range (Tobin C. Harding) 52f7dce Use & instead of `ref` (Tobin C. Harding) 81d5ae6 Remove explicit return statement (Tobin C. Harding) 03c6587 Remove unneeded explicit reference (Tobin C. Harding) 952af97 Remove unneeded unit return type (Tobin C. Harding) a1cf394 Use struct field shorthand (Tobin C. Harding) e94bf6e Remove redundant import statement (Tobin C. Harding) 77eb4a3 Add clippy config file (Tobin C. Harding) Pull request description: Make a start on clearing all the non-contentious clippy warnings. ACKs for top commit: sanket1729: ACK 7da5327 Tree-SHA512: cb7ad9d8983404a01148a61aa920dfd5657ff2a5fea1ea9d3ff565a1665148e31a693916a253bf8bad274063579d4484dfa738294b0eda4271f0e8fd0a9abbdd
2 parents 1d194d3 + 7da5327 commit 9ab6d4d

31 files changed

+243
-321
lines changed

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
msrv = "1.41.1"

src/descriptor/bare.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
4949
pub fn new(ms: Miniscript<Pk, BareCtx>) -> Result<Self, Error> {
5050
// do the top-level checks
5151
BareCtx::top_level_checks(&ms)?;
52-
Ok(Self { ms: ms })
52+
Ok(Self { ms })
5353
}
5454

5555
/// get the inner
@@ -111,7 +111,7 @@ where
111111
<<Pk as MiniscriptKey>::Hash as FromStr>::Err: ToString,
112112
{
113113
fn from_tree(top: &expression::Tree) -> Result<Self, Error> {
114-
let sub = Miniscript::<Pk, BareCtx>::from_tree(&top)?;
114+
let sub = Miniscript::<Pk, BareCtx>::from_tree(top)?;
115115
BareCtx::top_level_checks(&sub)?;
116116
Bare::new(sub)
117117
}
@@ -241,7 +241,7 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
241241
/// Create a new Pkh descriptor
242242
pub fn new(pk: Pk) -> Self {
243243
// do the top-level checks
244-
Self { pk: pk }
244+
Self { pk }
245245
}
246246

247247
/// Get a reference to the inner key

src/descriptor/checksum.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ pub fn desc_checksum(desc: &str) -> Result<String, Error> {
4343
let mut clscount = 0;
4444

4545
for ch in desc.chars() {
46-
let pos = INPUT_CHARSET.find(ch).ok_or(Error::BadDescriptor(format!(
47-
"Invalid character in checksum: '{}'",
48-
ch
49-
)))? as u64;
46+
let pos = INPUT_CHARSET.find(ch).ok_or_else(|| {
47+
Error::BadDescriptor(format!("Invalid character in checksum: '{}'", ch))
48+
})? as u64;
5049
c = poly_mod(c, pos & 31);
5150
cls = cls * 3 + (pos >> 5);
5251
clscount += 1;

src/descriptor/key.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ pub enum SinglePubKey {
7373
impl fmt::Display for DescriptorSecretKey {
7474
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7575
match self {
76-
&DescriptorSecretKey::Single(ref sk) => {
76+
DescriptorSecretKey::Single(ref sk) => {
7777
maybe_fmt_master_id(f, &sk.origin)?;
7878
sk.key.fmt(f)?;
7979
Ok(())
8080
}
81-
&DescriptorSecretKey::XPrv(ref xprv) => {
81+
DescriptorSecretKey::XPrv(ref xprv) => {
8282
maybe_fmt_master_id(f, &xprv.origin)?;
8383
xprv.xkey.fmt(f)?;
8484
fmt_derivation_path(f, &xprv.derivation_path)?;
@@ -172,9 +172,9 @@ impl DescriptorXKey<bip32::ExtendedPrivKey> {
172172

173173
let xprv = self
174174
.xkey
175-
.derive_priv(&secp, &hardened_path)
175+
.derive_priv(secp, &hardened_path)
176176
.map_err(|_| DescriptorKeyParseError("Unable to derive the hardened steps"))?;
177-
let xpub = bip32::ExtendedPubKey::from_priv(&secp, &xprv);
177+
let xpub = bip32::ExtendedPubKey::from_priv(secp, &xprv);
178178

179179
let origin = match &self.origin {
180180
Some((fingerprint, path)) => Some((
@@ -188,7 +188,7 @@ impl DescriptorXKey<bip32::ExtendedPrivKey> {
188188
if hardened_path.is_empty() {
189189
None
190190
} else {
191-
Some((self.xkey.fingerprint(&secp), hardened_path.into()))
191+
Some((self.xkey.fingerprint(secp), hardened_path.into()))
192192
}
193193
}
194194
};
@@ -562,7 +562,7 @@ impl<K: InnerXKey> DescriptorXKey<K> {
562562
DescriptorKeyParseError("Malformed master fingerprint, expected 8 hex chars")
563563
})?;
564564
let origin_path = raw_origin
565-
.map(|p| bip32::ChildNumber::from_str(p))
565+
.map(bip32::ChildNumber::from_str)
566566
.collect::<Result<bip32::DerivationPath, bip32::Error>>()
567567
.map_err(|_| {
568568
DescriptorKeyParseError("Error while parsing master derivation path")
@@ -657,21 +657,20 @@ impl<K: InnerXKey> DescriptorXKey<K> {
657657
) -> Option<bip32::DerivationPath> {
658658
let (fingerprint, path) = keysource;
659659

660-
let (compare_fingerprint, compare_path) = match &self.origin {
661-
&Some((fingerprint, ref path)) => (
660+
let (compare_fingerprint, compare_path) = match self.origin {
661+
Some((fingerprint, ref path)) => (
662662
fingerprint,
663663
path.into_iter()
664664
.chain(self.derivation_path.into_iter())
665665
.collect(),
666666
),
667-
&None => (
667+
None => (
668668
self.xkey.xkey_fingerprint(secp),
669669
self.derivation_path.into_iter().collect::<Vec<_>>(),
670670
),
671671
};
672672

673-
let path_excluding_wildcard = if self.wildcard != Wildcard::None && path.as_ref().len() > 0
674-
{
673+
let path_excluding_wildcard = if self.wildcard != Wildcard::None && !path.is_empty() {
675674
path.into_iter()
676675
.take(path.as_ref().len() - 1)
677676
.cloned()

src/descriptor/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ impl Descriptor<DescriptorPublicKey> {
712712
key_map: &mut KeyMap|
713713
-> Result<DescriptorPublicKey, DescriptorKeyParseError> {
714714
let (public_key, secret_key) = match DescriptorSecretKey::from_str(s) {
715-
Ok(sk) => (sk.to_public(&secp)?, Some(sk)),
715+
Ok(sk) => (sk.to_public(secp)?, Some(sk)),
716716
Err(_) => (DescriptorPublicKey::from_str(s)?, None),
717717
};
718718

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

776776
for i in range {
777-
let concrete = self.derived_descriptor(&secp, i)?;
777+
let concrete = self.derived_descriptor(secp, i)?;
778778
if &concrete.script_pubkey() == script_pubkey {
779779
return Ok(Some((i, concrete)));
780780
}

src/descriptor/pretaproot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub(crate) mod traits {
259259
{
260260
// This expect can technically be avoided if we implement this for types, but
261261
// having this expect saves lots of LoC because of default implementation
262-
<Self as DescriptorTrait<Pk>>::explicit_script(&self)
262+
<Self as DescriptorTrait<Pk>>::explicit_script(self)
263263
.expect("Pre taproot descriptor have explicit script")
264264
}
265265

@@ -269,7 +269,7 @@ pub(crate) mod traits {
269269
where
270270
Pk: ToPublicKey,
271271
{
272-
<Self as DescriptorTrait<Pk>>::script_code(&self)
272+
<Self as DescriptorTrait<Pk>>::script_code(self)
273273
.expect("Pre taproot descriptor have non-failing script code")
274274
}
275275
}

src/descriptor/segwitv0.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ where
140140
let top = &top.args[0];
141141
if top.name == "sortedmulti" {
142142
return Ok(Wsh {
143-
inner: WshInner::SortedMulti(SortedMultiVec::from_tree(&top)?),
143+
inner: WshInner::SortedMulti(SortedMultiVec::from_tree(top)?),
144144
});
145145
}
146-
let sub = Miniscript::from_tree(&top)?;
146+
let sub = Miniscript::from_tree(top)?;
147147
Segwitv0::top_level_checks(&sub)?;
148148
Ok(Wsh {
149149
inner: WshInner::Ms(sub),
@@ -313,7 +313,7 @@ where
313313
WshInner::SortedMulti(ref smv) => WshInner::SortedMulti(smv.translate_pk(&mut fpk)?),
314314
WshInner::Ms(ref ms) => WshInner::Ms(ms.translate_pk(&mut fpk, &mut fpkh)?),
315315
};
316-
Ok(Wsh { inner: inner })
316+
Ok(Wsh { inner })
317317
}
318318
}
319319

@@ -333,7 +333,7 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
333333
pk.to_string(),
334334
)))
335335
} else {
336-
Ok(Self { pk: pk })
336+
Ok(Self { pk })
337337
}
338338
}
339339

src/descriptor/sh.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ where
103103
if top.name == "sh" && top.args.len() == 1 {
104104
let top = &top.args[0];
105105
let inner = match top.name {
106-
"wsh" => ShInner::Wsh(Wsh::from_tree(&top)?),
107-
"wpkh" => ShInner::Wpkh(Wpkh::from_tree(&top)?),
108-
"sortedmulti" => ShInner::SortedMulti(SortedMultiVec::from_tree(&top)?),
106+
"wsh" => ShInner::Wsh(Wsh::from_tree(top)?),
107+
"wpkh" => ShInner::Wpkh(Wpkh::from_tree(top)?),
108+
"sortedmulti" => ShInner::SortedMulti(SortedMultiVec::from_tree(top)?),
109109
_ => {
110-
let sub = Miniscript::from_tree(&top)?;
110+
let sub = Miniscript::from_tree(top)?;
111111
Legacy::top_level_checks(&sub)?;
112112
ShInner::Ms(sub)
113113
}
114114
};
115-
Ok(Sh { inner: inner })
115+
Ok(Sh { inner })
116116
} else {
117117
Err(Error::Unexpected(format!(
118118
"{}({} args) while parsing sh descriptor",
@@ -439,6 +439,6 @@ where
439439
ShInner::SortedMulti(ref smv) => ShInner::SortedMulti(smv.translate_pk(&mut fpk)?),
440440
ShInner::Ms(ref ms) => ShInner::Ms(ms.translate_pk(&mut fpk, &mut fpkh)?),
441441
};
442-
Ok(Sh { inner: inner })
442+
Ok(Sh { inner })
443443
}
444444
}

src/descriptor/tr.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ where
322322
let (depth, last) = self.stack.pop().expect("Size checked above");
323323
match &*last {
324324
TapTree::Tree(l, r) => {
325-
self.stack.push((depth + 1, &r));
326-
self.stack.push((depth + 1, &l));
325+
self.stack.push((depth + 1, r));
326+
self.stack.push((depth + 1, l));
327327
}
328328
TapTree::Leaf(ref ms) => return Some((depth, ms)),
329329
}
@@ -360,12 +360,10 @@ where
360360
let right = parse_tr_script_spend(&args[1])?;
361361
Ok(TapTree::Tree(Arc::new(left), Arc::new(right)))
362362
}
363-
_ => {
364-
return Err(Error::Unexpected(
365-
"unknown format for script spending paths while parsing taproot descriptor"
366-
.to_string(),
367-
));
368-
}
363+
_ => Err(Error::Unexpected(
364+
"unknown format for script spending paths while parsing taproot descriptor"
365+
.to_string(),
366+
)),
369367
}
370368
}
371369

@@ -386,14 +384,14 @@ where
386384
})
387385
}
388386
2 => {
389-
let ref key = top.args[0];
387+
let key = &top.args[0];
390388
if !key.args.is_empty() {
391389
return Err(Error::Unexpected(format!(
392390
"#{} script associated with `key-path` while parsing taproot descriptor",
393391
key.args.len()
394392
)));
395393
}
396-
let ref tree = top.args[1];
394+
let tree = &top.args[1];
397395
let ret = parse_tr_script_spend(tree)?;
398396
Ok(Tr {
399397
internal_key: expression::terminal(key, Pk::from_str)?,
@@ -460,7 +458,7 @@ fn parse_tr_tree(s: &str) -> Result<expression::Tree, Error> {
460458
}
461459
}
462460

463-
let ret = if s.len() > 3 && &s[..3] == "tr(" && s.as_bytes()[s.len() - 1] == b')' {
461+
if s.len() > 3 && &s[..3] == "tr(" && s.as_bytes()[s.len() - 1] == b')' {
464462
let rest = &s[3..s.len() - 1];
465463
if !rest.contains(',') {
466464
let internal_key = expression::Tree {
@@ -497,12 +495,11 @@ fn parse_tr_tree(s: &str) -> Result<expression::Tree, Error> {
497495
}
498496
} else {
499497
Err(Error::Unexpected("invalid taproot descriptor".to_string()))
500-
};
501-
return ret;
498+
}
502499
}
503500

504501
fn split_once(inp: &str, delim: char) -> Option<(&str, &str)> {
505-
let ret = if inp.is_empty() {
502+
if inp.is_empty() {
506503
None
507504
} else {
508505
let mut found = inp.len();
@@ -514,12 +511,11 @@ fn split_once(inp: &str, delim: char) -> Option<(&str, &str)> {
514511
}
515512
// No comma or trailing comma found
516513
if found >= inp.len() - 1 {
517-
Some((&inp[..], ""))
514+
Some((inp, ""))
518515
} else {
519516
Some((&inp[..found], &inp[found + 1..]))
520517
}
521-
};
522-
return ret;
518+
}
523519
}
524520

525521
impl<Pk: MiniscriptKey> Liftable<Pk> for TapTree<Pk> {
@@ -533,7 +529,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for TapTree<Pk> {
533529
}
534530
}
535531

536-
let pol = lift_helper(&self)?;
532+
let pol = lift_helper(self)?;
537533
Ok(pol.normalized())
538534
}
539535
}
@@ -594,15 +590,15 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
594590
Pk: ToPublicKey,
595591
S: Satisfier<Pk>,
596592
{
597-
best_tap_spend(&self, satisfier, false /* allow_mall */)
593+
best_tap_spend(self, satisfier, false /* allow_mall */)
598594
}
599595

600596
fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
601597
where
602598
Pk: ToPublicKey,
603599
S: Satisfier<Pk>,
604600
{
605-
best_tap_spend(&self, satisfier, true /* allow_mall */)
601+
best_tap_spend(self, satisfier, true /* allow_mall */)
606602
}
607603

608604
fn max_satisfaction_weight(&self) -> Result<usize, Error> {

src/expression.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'a> Tree<'a> {
126126
// String-ending terminal
127127
Found::Nothing => Ok((
128128
Tree {
129-
name: &sl[..],
129+
name: sl,
130130
args: vec![],
131131
},
132132
"",
@@ -195,7 +195,7 @@ impl<'a> Tree<'a> {
195195
pub fn parse_num(s: &str) -> Result<u32, Error> {
196196
if s.len() > 1 {
197197
let ch = s.chars().next().unwrap();
198-
if ch < '1' || ch > '9' {
198+
if !('1'..='9').contains(&ch) {
199199
return Err(Error::Unexpected(
200200
"Number must start with a digit 1-9".to_string(),
201201
));

src/interpreter/inner.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn pk_from_slice(slice: &[u8], require_compressed: bool) -> Result<bitcoin::Publ
3737
}
3838
}
3939

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

5252
// Parse the script with appropriate context to check for context errors like
5353
// correct usage of x-only keys or multi_a
54-
fn script_from_stackelem<'a, Ctx: ScriptContext>(
55-
elem: &stack::Element<'a>,
54+
fn script_from_stackelem<Ctx: ScriptContext>(
55+
elem: &stack::Element<'_>,
5656
) -> Result<Miniscript<Ctx::Key, Ctx>, Error> {
5757
match *elem {
5858
stack::Element::Push(sl) => {
@@ -212,7 +212,7 @@ pub(super) fn from_txdata<'txin>(
212212
let has_annex = wit_stack
213213
.last()
214214
.and_then(|x| x.as_push().ok())
215-
.map(|x| x.len() > 0 && x[0] == TAPROOT_ANNEX_PREFIX)
215+
.map(|x| !x.is_empty() && x[0] == TAPROOT_ANNEX_PREFIX)
216216
.unwrap_or(false);
217217
let has_annex = has_annex && (wit_stack.len() >= 2);
218218
if has_annex {
@@ -233,8 +233,8 @@ pub(super) fn from_txdata<'txin>(
233233
let ctrl_blk = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?;
234234
let ctrl_blk = ctrl_blk.as_push()?;
235235
let tap_script = wit_stack.pop().ok_or(Error::UnexpectedStackEnd)?;
236-
let ctrl_blk = ControlBlock::from_slice(ctrl_blk)
237-
.map_err(|e| Error::ControlBlockParse(e))?;
236+
let ctrl_blk =
237+
ControlBlock::from_slice(ctrl_blk).map_err(Error::ControlBlockParse)?;
238238
let tap_script = script_from_stackelem::<Tap>(&tap_script)?;
239239
let ms = tap_script.to_no_checks_ms();
240240
// Creating new contexts is cheap
@@ -255,7 +255,7 @@ pub(super) fn from_txdata<'txin>(
255255
Some(tap_script),
256256
))
257257
} else {
258-
return Err(Error::ControlBlockVerificationError);
258+
Err(Error::ControlBlockVerificationError)
259259
}
260260
}
261261
}

0 commit comments

Comments
 (0)