Skip to content

Commit 6db0383

Browse files
committed
clippy: fix if_not_else lint
This one is a bit silly -- it complains if you do `if !x {} else {}` because it would supposedly be simpler to swap the if and the else. In fact, everywhere it triggers in this crate it *would* be an improvement, or at least neutral. So turn it on. In some cases we are using if-statements where we could instead use matches.
1 parent d2b998b commit 6db0383

6 files changed

Lines changed: 90 additions & 95 deletions

File tree

src/address.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,10 @@ fn find_prefix(bech32: &str) -> &str {
670670
/// The first prefix can be mixed case, but the second one is expected in
671671
/// lower case.
672672
fn match_prefix(prefix_mixed: &str, target: Hrp) -> bool {
673-
if target.len() != prefix_mixed.len() {
674-
false
675-
} else {
676-
target
677-
.lowercase_char_iter()
678-
.zip(prefix_mixed.chars())
679-
.all(|(char_lower, char_mixed)| char_lower == char_mixed.to_ascii_lowercase())
680-
}
673+
target.len() == prefix_mixed.len() && target
674+
.lowercase_char_iter()
675+
.zip(prefix_mixed.chars())
676+
.all(|(char_lower, char_mixed)| char_lower == char_mixed.to_ascii_lowercase())
681677
}
682678

683679
impl FromStr for Address {

src/confidential.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -841,13 +841,14 @@ impl<'de> Deserialize<'de> for AssetBlindingFactor {
841841
where
842842
E: ::serde::de::Error,
843843
{
844-
if v.len() != 32 {
845-
Err(E::invalid_length(v.len(), &stringify!($len)))
846-
} else {
847-
let mut ret = [0; 32];
848-
ret.copy_from_slice(v);
849-
let inner = Tweak::from_inner(ret).map_err(E::custom)?;
850-
Ok(AssetBlindingFactor(inner))
844+
use core::convert::TryFrom;
845+
846+
match <[u8; 32]>::try_from(v) {
847+
Ok(ret) => {
848+
let inner = Tweak::from_inner(ret).map_err(E::custom)?;
849+
Ok(AssetBlindingFactor(inner))
850+
}
851+
Err(_) => Err(E::invalid_length(v.len(), &stringify!($len))),
851852
}
852853
}
853854
}
@@ -1044,13 +1045,14 @@ impl<'de> Deserialize<'de> for ValueBlindingFactor {
10441045
where
10451046
E: ::serde::de::Error,
10461047
{
1047-
if v.len() != 32 {
1048-
Err(E::invalid_length(v.len(), &stringify!($len)))
1049-
} else {
1050-
let mut ret = [0; 32];
1051-
ret.copy_from_slice(v);
1052-
let inner = Tweak::from_inner(ret).map_err(E::custom)?;
1053-
Ok(ValueBlindingFactor(inner))
1048+
use core::convert::TryFrom;
1049+
1050+
match <[u8; 32]>::try_from(v) {
1051+
Ok(ret) => {
1052+
let inner = Tweak::from_inner(ret).map_err(E::custom)?;
1053+
Ok(ValueBlindingFactor(inner))
1054+
}
1055+
Err(_) => Err(E::invalid_length(v.len(), &stringify!($len))),
10541056
}
10551057
}
10561058
}

src/issuance.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,10 @@ impl<'de> ::serde::Deserialize<'de> for AssetId {
244244
where
245245
E: ::serde::de::Error,
246246
{
247-
if v.len() != 32 {
248-
Err(E::invalid_length(v.len(), &stringify!($len)))
249-
} else {
250-
let mut ret = [0; 32];
251-
ret.copy_from_slice(v);
252-
Ok(AssetId(sha256::Midstate::from_byte_array(ret)))
247+
use core::convert::TryFrom;
248+
match <[u8; 32]>::try_from(v) {
249+
Ok(ret) => Ok(AssetId(sha256::Midstate::from_byte_array(ret))),
250+
Err(_) => Err(E::invalid_length(v.len(), &stringify!($len))),
253251
}
254252
}
255253
}

src/pset/map/global.rs

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,10 @@ impl Map for Global {
169169
if prop_key.is_pset_key() && prop_key.subtype == PSBT_ELEMENTS_GLOBAL_SCALAR {
170170
if raw_value.is_empty() && prop_key.key.len() == 32 {
171171
let scalar = Tweak::from_slice(&prop_key.key)?;
172-
if !self.scalars.contains(&scalar) {
173-
self.scalars.push(scalar);
174-
} else {
172+
if self.scalars.contains(&scalar) {
175173
return Err(Error::DuplicateKey(raw_key).into());
176174
}
175+
self.scalars.push(scalar);
177176
} else {
178177
return Err(Error::InvalidKey(raw_key))?;
179178
}
@@ -415,41 +414,41 @@ impl Decodable for Global {
415414
}
416415
}
417416
PSET_GLOBAL_XPUB => {
418-
if !raw_key.key.is_empty() {
419-
let xpub = Xpub::decode(&raw_key.key)
420-
.map_err(|_| encode::Error::ParseFailed(
421-
"Can't deserialize Xpub from global XPUB key data"
422-
))?;
423-
424-
if raw_value.is_empty() || raw_value.len() % 4 != 0 {
425-
return Err(encode::Error::ParseFailed(
426-
"Incorrect length of global xpub derivation data",
427-
));
428-
}
429-
430-
let child_count = raw_value.len() / 4 - 1;
431-
let mut decoder = Cursor::new(raw_value);
432-
let mut fingerprint = [0u8; 4];
433-
decoder.read_exact(&mut fingerprint[..])?;
434-
let mut path = Vec::<ChildNumber>::with_capacity(child_count);
435-
while let Ok(index) = u32::consensus_decode(&mut decoder) {
436-
path.push(ChildNumber::from(index))
437-
}
438-
let derivation = DerivationPath::from(path);
439-
// Keys, according to BIP-174, must be unique
440-
if xpub_map
441-
.insert(xpub, (Fingerprint::from(fingerprint), derivation))
442-
.is_some()
443-
{
444-
return Err(encode::Error::ParseFailed(
445-
"Repeated global xpub key",
446-
));
447-
}
448-
} else {
417+
if raw_key.key.is_empty() {
449418
return Err(encode::Error::ParseFailed(
450419
"Xpub global key must contain serialized Xpub data",
451420
));
452421
}
422+
423+
let xpub = Xpub::decode(&raw_key.key)
424+
.map_err(|_| encode::Error::ParseFailed(
425+
"Can't deserialize Xpub from global XPUB key data"
426+
))?;
427+
428+
if raw_value.is_empty() || raw_value.len() % 4 != 0 {
429+
return Err(encode::Error::ParseFailed(
430+
"Incorrect length of global xpub derivation data",
431+
));
432+
}
433+
434+
let child_count = raw_value.len() / 4 - 1;
435+
let mut decoder = Cursor::new(raw_value);
436+
let mut fingerprint = [0u8; 4];
437+
decoder.read_exact(&mut fingerprint[..])?;
438+
let mut path = Vec::<ChildNumber>::with_capacity(child_count);
439+
while let Ok(index) = u32::consensus_decode(&mut decoder) {
440+
path.push(ChildNumber::from(index))
441+
}
442+
let derivation = DerivationPath::from(path);
443+
// Keys, according to BIP-174, must be unique
444+
if xpub_map
445+
.insert(xpub, (Fingerprint::from(fingerprint), derivation))
446+
.is_some()
447+
{
448+
return Err(encode::Error::ParseFailed(
449+
"Repeated global xpub key",
450+
));
451+
}
453452
}
454453
PSET_GLOBAL_VERSION => {
455454
impl_pset_insert_pair! {
@@ -463,10 +462,10 @@ impl Decodable for Global {
463462
{
464463
if raw_value.is_empty() && prop_key.key.len() == 32 {
465464
let scalar = Tweak::from_slice(&prop_key.key)?;
466-
if !scalars.contains(&scalar) {
467-
scalars.push(scalar);
468-
} else {
465+
if scalars.contains(&scalar) {
469466
return Err(Error::DuplicateKey(raw_key).into());
467+
} else {
468+
scalars.push(scalar);
470469
}
471470
} else {
472471
return Err(Error::InvalidKey(raw_key))?;

src/serde_utils.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ pub mod btreemap_byte_values {
1818
use serde::ser::SerializeMap;
1919

2020
// Don't do anything special when not human readable.
21-
if !s.is_human_readable() {
22-
serde::Serialize::serialize(v, s)
23-
} else {
21+
if s.is_human_readable() {
2422
let mut map = s.serialize_map(Some(v.len()))?;
2523
for (key, value) in v {
2624
map.serialize_entry(key, &value.to_hex())?;
2725
}
2826
map.end()
27+
} else {
28+
serde::Serialize::serialize(v, s)
2929
}
3030
}
3131

@@ -58,10 +58,10 @@ pub mod btreemap_byte_values {
5858
}
5959

6060
// Don't do anything special when not human readable.
61-
if !d.is_human_readable() {
62-
serde::Deserialize::deserialize(d)
63-
} else {
61+
if d.is_human_readable() {
6462
d.deserialize_map(Visitor(PhantomData))
63+
} else {
64+
serde::Deserialize::deserialize(d)
6565
}
6666
}
6767
}
@@ -85,14 +85,14 @@ pub mod btreemap_as_seq {
8585
use serde::ser::SerializeSeq;
8686

8787
// Don't do anything special when not human readable.
88-
if !s.is_human_readable() {
89-
serde::Serialize::serialize(v, s)
90-
} else {
88+
if s.is_human_readable() {
9189
let mut seq = s.serialize_seq(Some(v.len()))?;
9290
for pair in v {
9391
seq.serialize_element(&pair)?;
9492
}
9593
seq.end()
94+
} else {
95+
serde::Serialize::serialize(v, s)
9696
}
9797
}
9898

@@ -127,10 +127,10 @@ pub mod btreemap_as_seq {
127127
}
128128

129129
// Don't do anything special when not human readable.
130-
if !d.is_human_readable() {
131-
serde::Deserialize::deserialize(d)
132-
} else {
130+
if d.is_human_readable() {
133131
d.deserialize_seq(Visitor(PhantomData))
132+
} else {
133+
serde::Deserialize::deserialize(d)
134134
}
135135
}
136136
}
@@ -171,14 +171,14 @@ pub mod btreemap_as_seq_byte_values {
171171
use serde::ser::SerializeSeq;
172172

173173
// Don't do anything special when not human readable.
174-
if !s.is_human_readable() {
175-
serde::Serialize::serialize(v, s)
176-
} else {
174+
if s.is_human_readable() {
177175
let mut seq = s.serialize_seq(Some(v.len()))?;
178176
for (key, value) in v {
179177
seq.serialize_element(&BorrowedPair(key, value))?;
180178
}
181179
seq.end()
180+
} else {
181+
serde::Serialize::serialize(v, s)
182182
}
183183
}
184184

@@ -211,10 +211,10 @@ pub mod btreemap_as_seq_byte_values {
211211
}
212212

213213
// Don't do anything special when not human readable.
214-
if !d.is_human_readable() {
215-
serde::Deserialize::deserialize(d)
216-
} else {
214+
if d.is_human_readable() {
217215
d.deserialize_seq(Visitor(PhantomData))
216+
} else {
217+
serde::Deserialize::deserialize(d)
218218
}
219219
}
220220
}
@@ -230,10 +230,10 @@ pub mod hex_bytes {
230230
where T: serde::Serialize + AsRef<[u8]>, S: serde::Serializer
231231
{
232232
// Don't do anything special when not human readable.
233-
if !s.is_human_readable() {
234-
serde::Serialize::serialize(bytes, s)
235-
} else {
233+
if s.is_human_readable() {
236234
s.serialize_str(&bytes.as_ref().to_hex())
235+
} else {
236+
serde::Serialize::serialize(bytes, s)
237237
}
238238
}
239239

@@ -267,10 +267,10 @@ pub mod hex_bytes {
267267
}
268268

269269
// Don't do anything special when not human readable.
270-
if !d.is_human_readable() {
271-
serde::Deserialize::deserialize(d)
272-
} else {
270+
if d.is_human_readable() {
273271
d.deserialize_str(Visitor(::std::marker::PhantomData))
272+
} else {
273+
serde::Deserialize::deserialize(d)
274274
}
275275
}
276276
}

src/sighash.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,10 @@ impl<R: Deref<Target = Transaction>> SighashCache<R> {
515515

516516
self.tx.version.consensus_encode(&mut writer)?;
517517

518-
if !anyone_can_pay {
519-
self.segwit_cache().prevouts.consensus_encode(&mut writer)?;
520-
} else {
518+
if anyone_can_pay {
521519
zero_hash.consensus_encode(&mut writer)?;
520+
} else {
521+
self.segwit_cache().prevouts.consensus_encode(&mut writer)?;
522522
}
523523

524524
if !anyone_can_pay && sighash != EcdsaSighashType::Single && sighash != EcdsaSighashType::None {
@@ -529,10 +529,10 @@ impl<R: Deref<Target = Transaction>> SighashCache<R> {
529529

530530
// Elements: Push the hash issuance zero hash as required
531531
// If required implement for issuance, but not necessary as of now
532-
if !anyone_can_pay {
533-
self.segwit_cache().issuances.consensus_encode(&mut writer)?;
534-
} else {
532+
if anyone_can_pay {
535533
zero_hash.consensus_encode(&mut writer)?;
534+
} else {
535+
self.segwit_cache().issuances.consensus_encode(&mut writer)?;
536536
}
537537

538538
// input specific values

0 commit comments

Comments
 (0)