Skip to content

Commit a94cf9d

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#1004: Clear Clippy warnings
a6efe98 Use write_all to write whole buffer (Tobin C. Harding) 51c60b8 Allow no is_empty method for VarInt (Tobin C. Harding) 841f1f5 Implement Default for TaprootBuilder (Tobin C. Harding) f81d4aa Remove unnecessary call to clone (Tobin C. Harding) 27649ba Use copied instead of map to copy (Tobin C. Harding) 62ccc91 Use iter().flatten().any() instead of if let Some (Tobin C. Harding) 4b28a1b Remove unneeded return statement (Tobin C. Harding) 16cac3c Derive Default for Witness (Tobin C. Harding) c751898 Remove unnecessary closure (Tobin C. Harding) dfff853 Ignore bytes written for sighash_single bug output (Tobin C. Harding) 14c72e7 Use contains combinator instead of manual range (Tobin C. Harding) b7d6c3e Remove additional reference (Tobin C. Harding) 1940b00 Implement From instead of Into (Tobin C. Harding) fcd0f4d Use struct field init shorthand (Tobin C. Harding) 641960f Use rustfmt::skip (Tobin C. Harding) 3cd00e5 Remove unnecessary whitespace (Tobin C. Harding) Pull request description: Clear all current Clippy warnings, codebase wide. Possibly contentious patches include: - [commit](rust-bitcoin/rust-bitcoin@fcd0f4d): `fcd0f4d Use struct field init shorthand` - [commit](rust-bitcoin/rust-bitcoin@14c72e7): `14c72e7 Use contains combinator instead of manual range` - [commit](rust-bitcoin/rust-bitcoin@3b3c378): `3b3c378 Use iter().flatten() instead of if let Some` ## Notes Please note commit `dfff8535 Ignore bytes written for sighash_single bug output` touches the same lines of code as commit `a6efe982 Use write_all to write whole buffer`. ACKs for top commit: apoelstra: ACK a6efe98 Kixunil: ACK a6efe98 Tree-SHA512: 5351a82fd3deadb8e53911c43b5a60a9517d5c57014f5fa833b79b32c0a4606ada0bcd28e06ce35d47aa74115c7cf70c27a1ba9c561a3424ac85a4f69774014d
2 parents a411769 + d33f774 commit a94cf9d

File tree

14 files changed

+50
-64
lines changed

14 files changed

+50
-64
lines changed

src/blockdata/script.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,9 @@ impl Script {
470470
/// the current script, assuming that the script is a Tapscript.
471471
#[inline]
472472
pub fn to_v1_p2tr<C: Verification>(&self, secp: &Secp256k1<C>, internal_key: UntweakedPublicKey) -> Script {
473-
let leaf_hash = TapLeafHash::from_script(&self, LeafVersion::TapScript);
473+
let leaf_hash = TapLeafHash::from_script(self, LeafVersion::TapScript);
474474
let merkle_root = TapBranchHash::from_inner(leaf_hash.into_inner());
475-
Script::new_v1_p2tr(&secp, internal_key, Some(merkle_root))
475+
Script::new_v1_p2tr(secp, internal_key, Some(merkle_root))
476476
}
477477

478478
/// Returns witness version of the script, if any, assuming the script is a `scriptPubkey`.
@@ -525,7 +525,7 @@ impl Script {
525525
// special meaning. The value of the first push is called the "version byte". The following
526526
// byte vector pushed is called the "witness program".
527527
let script_len = self.0.len();
528-
if script_len < 4 || script_len > 42 {
528+
if !(4..=42).contains(&script_len) {
529529
return false
530530
}
531531
let ver_opcode = opcodes::All::from(self.0[0]); // Version 0 or PUSHNUM_1-PUSHNUM_16
@@ -877,7 +877,7 @@ impl Builder {
877877
/// dedicated opcodes to push some small integers.
878878
pub fn push_int(self, data: i64) -> Builder {
879879
// We can special-case -1, 1-16
880-
if data == -1 || (data >= 1 && data <= 16) {
880+
if data == -1 || (1..=16).contains(&data) {
881881
let opcode = opcodes::All::from(
882882
(data - 1 + opcodes::OP_TRUE.into_u8() as i64) as u8
883883
);

src/blockdata/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ impl Transaction {
353353
// will result in the data written to the writer being hashed, however the correct
354354
// handling of the SIGHASH_SINGLE bug is to return the 'one array' - either implement
355355
// this behaviour manually or use `signature_hash()`.
356-
writer.write(b"[not a transaction] SIGHASH_SINGLE bug")?;
356+
writer.write_all(b"[not a transaction] SIGHASH_SINGLE bug")?;
357357
return Ok(())
358358
}
359359

src/blockdata/witness.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use serde;
2222
/// For serialization and deserialization performance it is stored internally as a single `Vec`,
2323
/// saving some allocations.
2424
///
25-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
25+
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
2626
pub struct Witness {
2727
/// contains the witness Vec<Vec<u8>> serialization without the initial varint indicating the
2828
/// number of elements (which is stored in `witness_elements`)
@@ -67,12 +67,12 @@ impl Decodable for Witness {
6767
let element_size = element_size_varint.0 as usize;
6868
let required_len = cursor
6969
.checked_add(element_size)
70-
.ok_or_else(|| self::Error::OversizedVectorAllocation {
70+
.ok_or(self::Error::OversizedVectorAllocation {
7171
requested: usize::max_value(),
7272
max: MAX_VEC_SIZE,
7373
})?
7474
.checked_add(element_size_varint_len)
75-
.ok_or_else(|| self::Error::OversizedVectorAllocation {
75+
.ok_or(self::Error::OversizedVectorAllocation {
7676
requested: usize::max_value(),
7777
max: MAX_VEC_SIZE,
7878
})?;
@@ -218,7 +218,7 @@ impl Witness {
218218
pub fn push_bitcoin_signature(&mut self, signature: &ecdsa::SerializedSignature, hash_type: EcdsaSighashType) {
219219
// Note that a maximal length ECDSA signature is 72 bytes, plus the sighash type makes 73
220220
let mut sig = [0; 73];
221-
sig[..signature.len()].copy_from_slice(&signature);
221+
sig[..signature.len()].copy_from_slice(signature);
222222
sig[signature.len()] = hash_type as u8;
223223
self.push(&sig[..signature.len() + 1]);
224224
}
@@ -249,19 +249,6 @@ impl Witness {
249249
}
250250
}
251251

252-
impl Default for Witness {
253-
fn default() -> Self {
254-
// from https://doc.rust-lang.org/std/vec/struct.Vec.html#method.new
255-
// The vector will not allocate until elements are pushed onto it.
256-
Witness {
257-
content: Vec::new(),
258-
witness_elements: 0,
259-
last: 0,
260-
second_to_last: 0,
261-
}
262-
}
263-
}
264-
265252
impl<'a> Iterator for Iter<'a> {
266253
type Item = &'a [u8];
267254

src/consensus/encode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ impl_int_encodable!(i16, read_i16, emit_i16);
400400
impl_int_encodable!(i32, read_i32, emit_i32);
401401
impl_int_encodable!(i64, read_i64, emit_i64);
402402

403+
#[allow(clippy::len_without_is_empty)] // VarInt has on concept of 'is_empty'.
403404
impl VarInt {
404405
/// Gets the length of this VarInt when encoded.
405406
/// Returns 1 for 0..=0xFC, 3 for 0xFD..=(2^16-1), 5 for 0x10000..=(2^32-1),

src/network/constants.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ impl From<u64> for ServiceFlags {
238238
}
239239
}
240240

241-
impl Into<u64> for ServiceFlags {
242-
fn into(self) -> u64 {
243-
self.0
241+
impl From<ServiceFlags> for u64 {
242+
fn from(flags: ServiceFlags) -> Self {
243+
flags.0
244244
}
245245
}
246246

src/util/address.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ impl Address {
621621
network: Network
622622
) -> Address {
623623
Address {
624-
network: network,
624+
network,
625625
payload: Payload::p2tr(secp, internal_key, merkle_root),
626626
}
627627
}

src/util/bip32.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ impl From<Vec<ChildNumber>> for DerivationPath {
283283
}
284284
}
285285

286-
impl Into<Vec<ChildNumber>> for DerivationPath {
287-
fn into(self) -> Vec<ChildNumber> {
288-
self.0
286+
impl From<DerivationPath> for Vec<ChildNumber> {
287+
fn from(path: DerivationPath) -> Self {
288+
path.0
289289
}
290290
}
291291

src/util/ecdsa.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl EcdsaSig {
5858
pub fn to_vec(&self) -> Vec<u8> {
5959
// TODO: add support to serialize to a writer to SerializedSig
6060
self.sig.serialize_der()
61-
.iter().map(|x| *x)
61+
.iter().copied()
6262
.chain(iter::once(self.hash_ty as u8))
6363
.collect()
6464
}

src/util/key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl FromStr for PublicKey {
228228
match s.len() {
229229
66 => PublicKey::from_slice(&<[u8; 33]>::from_hex(s)?),
230230
130 => PublicKey::from_slice(&<[u8; 65]>::from_hex(s)?),
231-
len => return Err(Error::Hex(hex::Error::InvalidLength(66, len)))
231+
len => Err(Error::Hex(hex::Error::InvalidLength(66, len))),
232232
}
233233
}
234234
}

src/util/psbt/macros.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ macro_rules! impl_psbtmap_consensus_enc_dec_oding {
9292
};
9393
}
9494

95-
#[cfg_attr(rustfmt, rustfmt_skip)]
95+
#[rustfmt::skip]
9696
macro_rules! impl_psbt_insert_pair {
9797
($slf:ident.$unkeyed_name:ident <= <$raw_key:ident: _>|<$raw_value:ident: $unkeyed_value_type:ty>) => {
9898
if $raw_key.key.is_empty() {
@@ -122,8 +122,7 @@ macro_rules! impl_psbt_insert_pair {
122122
};
123123
}
124124

125-
126-
#[cfg_attr(rustfmt, rustfmt_skip)]
125+
#[rustfmt::skip]
127126
macro_rules! impl_psbt_get_pair {
128127
($rv:ident.push($slf:ident.$unkeyed_name:ident, $unkeyed_typeval:ident)) => {
129128
if let Some(ref $unkeyed_name) = $slf.$unkeyed_name {

0 commit comments

Comments
 (0)