Skip to content

Commit d2ffd8b

Browse files
committed
Merge rust-bitcoin/rust-miniscript#367: Clear some clippy warnings
7da532763924c9a0d3b5569745182e552ed14ea0 Use semvar-compliant version number (Tobin C. Harding) 17bd217697035fe183c76625f623e4995ae4bdae Use find instead of chaining filter and next (Tobin C. Harding) b75964cca195bd79c062cc635ba4d67d7dfb6cb7 Use sort_unstable on primitive types (Tobin C. Harding) 769cd6db4cb7c039b4d0ee0c05fb55127d495b58 Do not check for < 0 for usize types (Tobin C. Harding) 289a1a8bda3e5af0bb1b68708c5c811482631fd4 Use flat_map instead of chaining map and flatten (Tobin C. Harding) 90ba69228912def84265883f3edb728f6a0b1c08 Use unwrap_or_else instead of unwrap_or (Tobin C. Harding) da6e068df151867003ec0768f9fc20c5c684a3a3 Remove the redundant calls to clone on Copy type (Tobin C. Harding) 98d4a063a52f2a882ba0ff7f94113a23ef447f99 Remove unneeded `ref` (Tobin C. Harding) af2260fae9989558c513cd6034113f9f51bd227a Return block directly (Tobin C. Harding) 54412f137cb97623fb1f935a6c099a028645d520 Simplify duplicate code (Tobin C. Harding) 45fd5f7fc7676bbcfa10f327482bd58b542536ab Use push with character literal (Tobin C. Harding) ebc4dc9f78ee87bbbc3f8833a4da890c29fdcb9f Use to_string instead of format macro (Tobin C. Harding) 592b4790f3f229b2bcad28238777f6440235d28d Use copied (Tobin C. Harding) 54f23840ae190f4240b3438279320254aa3c4591 Remove redundant clone (Tobin C. Harding) 230cab996721b64a1de8efb04fe6c5c212399d22 Use map instead of and_then (Tobin C. Harding) bc7be32fab6e4ecf7ba9235013def628ba77a8f4 Remove unnecessary call to String::from (Tobin C. Harding) 35015bf7e702de0c619face925971429c69d3e76 Remove redundant pattern matching (Tobin C. Harding) ad51d83a9d481e2b0c9b278f4d27625a76c37ea2 Derive Default (Tobin C. Harding) ea57d6e1bc4b4fa778da347a9d8cf27d039fe32a Use contains instead of manual implementation (Tobin C. Harding) 7b91918a4a37f60348dabd8e7029eeeaef248eef Use is_empty (Tobin C. Harding) 738ff15174f8405c305b843b4118a587018bea0e Remove redundant closures (Tobin C. Harding) 63dee9eb228a08a40a3028ab47647e3adc8c4da4 Use iter instead of into_iter (Tobin C. Harding) 3fdd0f516bd0ff5d26e37c984332036752adeb1a Remove useless conversion call from error return (Tobin C. Harding) d29d10a95b9357a9bff9abfe73cd9fd5974f95a8 Remove reference from expression and patterns (Tobin C. Harding) 0a1b5e497944da82a5fad08fb67bcd1c1e34992e Do not use & for pattern match arms (Tobin C. Harding) be1a4ee0af639ba727c93a8b3221c2e41de4e779 Use ok_or_else instead of ok_or (Tobin C. Harding) 8944ca0d4f6cc54ff712e45448f2230ea2432933 Remove explicit lifetimes (Tobin C. Harding) 5647e29606d9503aa7c8180a70827f2d4f14f877 Remove redundant slicing of whole range (Tobin C. Harding) 52f7dce75808b678d11c1c8a0c9a434a714ba19f Use & instead of `ref` (Tobin C. Harding) 81d5ae61375eddcdae7cd5fc97895dac2a0f4d82 Remove explicit return statement (Tobin C. Harding) 03c65871b50581ba7d203b0a77ea09bfcf4f4f49 Remove unneeded explicit reference (Tobin C. Harding) 952af97e2aa3986f1141f7fd0cace331ac943157 Remove unneeded unit return type (Tobin C. Harding) a1cf3944650e4e3508e78e4bdc0bb80d1e0b1471 Use struct field shorthand (Tobin C. Harding) e94bf6efb776c234800164451e871fc473fbffa4 Remove redundant import statement (Tobin C. Harding) 77eb4a34803ffd13e3bfd6298fe8a249940ab2f8 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 7da532763924c9a0d3b5569745182e552ed14ea0 Tree-SHA512: cb7ad9d8983404a01148a61aa920dfd5657ff2a5fea1ea9d3ff565a1665148e31a693916a253bf8bad274063579d4484dfa738294b0eda4271f0e8fd0a9abbdd
2 parents 0d41a32 + ac6c6c8 commit d2ffd8b

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
));

0 commit comments

Comments
 (0)