Skip to content

Commit

Permalink
Merge pull request #5740 from jbencin/chore/clippy-single-match-and-r…
Browse files Browse the repository at this point in the history
…edundant-pattern-matching

chore: Apply Clippy lints `single_match` and `redundant_pattern_matching`
  • Loading branch information
jbencin authored Feb 1, 2025
2 parents fb6f2b7 + d64b017 commit d2ed454
Show file tree
Hide file tree
Showing 48 changed files with 680 additions and 1,053 deletions.
30 changes: 9 additions & 21 deletions stackslib/src/burnchains/bitcoin/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,40 +265,31 @@ impl BitcoinIndexer {
Ok(s) => {
// Disable Nagle algorithm
s.set_nodelay(true).map_err(|_e| {
test_debug!("Failed to set TCP_NODELAY: {:?}", &_e);
test_debug!("Failed to set TCP_NODELAY: {_e:?}");
btc_error::ConnectionError
})?;

// set timeout
s.set_read_timeout(Some(Duration::from_secs(self.runtime.timeout)))
.map_err(|_e| {
test_debug!("Failed to set TCP read timeout: {:?}", &_e);
test_debug!("Failed to set TCP read timeout: {_e:?}");
btc_error::ConnectionError
})?;

s.set_write_timeout(Some(Duration::from_secs(self.runtime.timeout)))
.map_err(|_e| {
test_debug!("Failed to set TCP write timeout: {:?}", &_e);
test_debug!("Failed to set TCP write timeout: {_e:?}");
btc_error::ConnectionError
})?;

match self.runtime.sock.take() {
Some(s) => {
let _ = s.shutdown(Shutdown::Both);
}
None => {}
if let Some(s_old) = self.runtime.sock.replace(s) {
let _ = s_old.shutdown(Shutdown::Both);
}

self.runtime.sock = Some(s);
Ok(())
}
Err(_e) => {
let s = self.runtime.sock.take();
match s {
Some(s) => {
let _ = s.shutdown(Shutdown::Both);
}
None => {}
if let Some(s) = self.runtime.sock.take() {
let _ = s.shutdown(Shutdown::Both);
}
Err(btc_error::ConnectionError)
}
Expand Down Expand Up @@ -926,11 +917,8 @@ impl BitcoinIndexer {

impl Drop for BitcoinIndexer {
fn drop(&mut self) {
match self.runtime.sock {
Some(ref mut s) => {
let _ = s.shutdown(Shutdown::Both);
}
None => {}
if let Some(ref mut s) = self.runtime.sock {
let _ = s.shutdown(Shutdown::Both);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion stackslib/src/burnchains/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,9 +1193,10 @@ impl BurnchainDB {
let ops: Vec<BlockstackOperationType> =
query_rows(&self.conn, qry, args).expect("FATAL: burnchain DB query error");
for op in ops {
if let Some(_) = indexer
if indexer
.find_burnchain_header_height(&op.burn_header_hash())
.expect("FATAL: burnchain DB query error")
.is_some()
{
// this is the op on the canonical fork
return Some(op);
Expand Down
11 changes: 4 additions & 7 deletions stackslib/src/burnchains/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,10 @@ impl TestBurnchainBlock {
pub fn patch_from_chain_tip(&mut self, parent_snapshot: &BlockSnapshot) {
assert_eq!(parent_snapshot.block_height + 1, self.block_height);

for i in 0..self.txs.len() {
match self.txs[i] {
BlockstackOperationType::LeaderKeyRegister(ref mut data) => {
assert_eq!(data.block_height, self.block_height);
data.consensus_hash = parent_snapshot.consensus_hash.clone();
}
_ => {}
for tx in self.txs.iter_mut() {
if let BlockstackOperationType::LeaderKeyRegister(ref mut data) = tx {
assert_eq!(data.block_height, self.block_height);
data.consensus_hash = parent_snapshot.consensus_hash.clone();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/chainstate/nakamoto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2469,7 +2469,7 @@ impl NakamotoChainState {
) -> Result<bool, ChainstateError> {
test_debug!("Consider Nakamoto block {}", &block.block_id());
// do nothing if we already have this block
if let Some(_) = Self::get_block_header(headers_conn, &block.header.block_id())? {
if Self::get_block_header(headers_conn, &block.header.block_id())?.is_some() {
debug!("Already have block {}", &block.header.block_id());
return Ok(false);
}
Expand Down
50 changes: 22 additions & 28 deletions stackslib/src/chainstate/stacks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,13 @@ impl StacksMessageCodec for StacksBlock {
// must be only one coinbase
let mut coinbase_count = 0;
for tx in txs.iter() {
match tx.payload {
TransactionPayload::Coinbase(..) => {
coinbase_count += 1;
if coinbase_count > 1 {
return Err(codec_error::DeserializeError(
"Invalid block: multiple coinbases found".to_string(),
));
}
if let TransactionPayload::Coinbase(..) = tx.payload {
coinbase_count += 1;
if coinbase_count > 1 {
return Err(codec_error::DeserializeError(
"Invalid block: multiple coinbases found".to_string(),
));
}
_ => {}
}
}

Expand Down Expand Up @@ -515,26 +512,23 @@ impl StacksBlock {
let mut found_coinbase = false;
let mut coinbase_index = 0;
for (i, tx) in txs.iter().enumerate() {
match tx.payload {
TransactionPayload::Coinbase(..) => {
if !check_present {
warn!("Found unexpected coinbase tx {}", tx.txid());
return false;
}

if found_coinbase {
warn!("Found duplicate coinbase tx {}", tx.txid());
return false;
}

if tx.anchor_mode != TransactionAnchorMode::OnChainOnly {
warn!("Invalid coinbase tx {}: not on-chain only", tx.txid());
return false;
}
found_coinbase = true;
coinbase_index = i;
if let TransactionPayload::Coinbase(..) = tx.payload {
if !check_present {
warn!("Found unexpected coinbase tx {}", tx.txid());
return false;
}

if found_coinbase {
warn!("Found duplicate coinbase tx {}", tx.txid());
return false;
}

if tx.anchor_mode != TransactionAnchorMode::OnChainOnly {
warn!("Invalid coinbase tx {}: not on-chain only", tx.txid());
return false;
}
_ => {}
found_coinbase = true;
coinbase_index = i;
}
}

Expand Down
2 changes: 1 addition & 1 deletion stackslib/src/chainstate/stacks/boot/contract_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ impl BurnStateDB for TestSimBurnStateDB {
height: u32,
sortition_id: &SortitionId,
) -> Option<(Vec<TupleData>, u128)> {
if let Some(_) = self.get_burn_header_hash(height, sortition_id) {
if self.get_burn_header_hash(height, sortition_id).is_some() {
let first_block = self.get_burn_start_height();
let prepare_len = self.get_pox_prepare_length();
let rc_len = self.get_pox_reward_cycle_length();
Expand Down
29 changes: 13 additions & 16 deletions stackslib/src/chainstate/stacks/boot/pox_2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,23 +1371,20 @@ fn test_simple_pox_2_auto_unlock(alice_first: bool) {
coinbase_txs.push(r);
continue;
}
match r.transaction {
TransactionOrigin::Stacks(ref t) => {
let addr = t.auth.origin().address_testnet();
eprintln!("TX addr: {}", addr);
if addr == alice_address {
alice_txs.insert(t.auth.get_origin_nonce(), r);
} else if addr == bob_address {
bob_txs.insert(t.auth.get_origin_nonce(), r);
} else if addr == charlie_address {
assert!(
r.execution_cost != ExecutionCost::ZERO,
"Execution cost is not zero!"
);
charlie_txs.insert(t.auth.get_origin_nonce(), r);
}
if let TransactionOrigin::Stacks(ref t) = r.transaction {
let addr = t.auth.origin().address_testnet();
eprintln!("TX addr: {}", addr);
if addr == alice_address {
alice_txs.insert(t.auth.get_origin_nonce(), r);
} else if addr == bob_address {
bob_txs.insert(t.auth.get_origin_nonce(), r);
} else if addr == charlie_address {
assert!(
r.execution_cost != ExecutionCost::ZERO,
"Execution cost is not zero!"
);
charlie_txs.insert(t.auth.get_origin_nonce(), r);
}
_ => {}
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions stackslib/src/chainstate/stacks/boot/pox_3_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,16 +930,13 @@ fn pox_auto_unlock(alice_first: bool) {
coinbase_txs.push(r);
continue;
}
match r.transaction {
TransactionOrigin::Stacks(ref t) => {
let addr = t.auth.origin().address_testnet();
if addr == alice_address {
alice_txs.insert(t.auth.get_origin_nonce(), r);
} else if addr == bob_address {
bob_txs.insert(t.auth.get_origin_nonce(), r);
}
if let TransactionOrigin::Stacks(ref t) = r.transaction {
let addr = t.auth.origin().address_testnet();
if addr == alice_address {
alice_txs.insert(t.auth.get_origin_nonce(), r);
} else if addr == bob_address {
bob_txs.insert(t.auth.get_origin_nonce(), r);
}
_ => {}
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions stackslib/src/chainstate/stacks/boot/pox_4_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9570,16 +9570,13 @@ fn missed_slots_no_unlock() {
coinbase_txs.push(r);
continue;
}
match r.transaction {
TransactionOrigin::Stacks(ref t) => {
let addr = t.auth.origin().address_testnet();
if addr == alice_address {
alice_txs.insert(t.auth.get_origin_nonce(), r);
} else if addr == bob_address {
bob_txs.insert(t.auth.get_origin_nonce(), r);
}
if let TransactionOrigin::Stacks(ref t) = r.transaction {
let addr = t.auth.origin().address_testnet();
if addr == alice_address {
alice_txs.insert(t.auth.get_origin_nonce(), r);
} else if addr == bob_address {
bob_txs.insert(t.auth.get_origin_nonce(), r);
}
_ => {}
}
}
}
Expand Down
32 changes: 13 additions & 19 deletions stackslib/src/chainstate/stacks/db/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5137,7 +5137,7 @@ impl StacksChainState {
) {
Ok(miner_rewards_opt) => miner_rewards_opt,
Err(e) => {
if let Some(_) = miner_id_opt {
if miner_id_opt.is_some() {
return Err(e);
} else {
let msg = format!("Failed to load miner rewards: {:?}", &e);
Expand Down Expand Up @@ -11196,15 +11196,12 @@ pub mod test {

let (_, burn_header_hash, consensus_hash) = peer.next_burnchain_block(burn_ops.clone());

match (stacks_block_opt, microblocks_opt) {
(Some(stacks_block), Some(microblocks)) => {
peer.process_stacks_epoch_at_tip(&stacks_block, &microblocks);
last_block_id = StacksBlockHeader::make_index_block_hash(
&consensus_hash,
&stacks_block.block_hash(),
);
}
_ => {}
if let (Some(stacks_block), Some(microblocks)) = (stacks_block_opt, microblocks_opt) {
peer.process_stacks_epoch_at_tip(&stacks_block, &microblocks);
last_block_id = StacksBlockHeader::make_index_block_hash(
&consensus_hash,
&stacks_block.block_hash(),
);
}

let tip =
Expand Down Expand Up @@ -11879,15 +11876,12 @@ pub mod test {

let (_, burn_header_hash, consensus_hash) = peer.next_burnchain_block(burn_ops.clone());

match (stacks_block_opt, microblocks_opt) {
(Some(stacks_block), Some(microblocks)) => {
peer.process_stacks_epoch_at_tip(&stacks_block, &microblocks);
last_block_id = StacksBlockHeader::make_index_block_hash(
&consensus_hash,
&stacks_block.block_hash(),
);
}
_ => {}
if let (Some(stacks_block), Some(microblocks)) = (stacks_block_opt, microblocks_opt) {
peer.process_stacks_epoch_at_tip(&stacks_block, &microblocks);
last_block_id = StacksBlockHeader::make_index_block_hash(
&consensus_hash,
&stacks_block.block_hash(),
);
}

let tip =
Expand Down
26 changes: 7 additions & 19 deletions stackslib/src/chainstate/stacks/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,10 +1844,7 @@ impl StacksChainState {
let nakamoto_staging_blocks_conn =
StacksChainState::open_nakamoto_staging_blocks(&nakamoto_staging_blocks_path, true)?;

let init_required = match fs::metadata(&clarity_state_index_marf) {
Ok(_) => false,
Err(_) => true,
};
let init_required = fs::metadata(&clarity_state_index_marf).is_err();

let state_index = StacksChainState::open_db(mainnet, chain_id, &header_index_root)?;

Expand Down Expand Up @@ -2754,11 +2751,8 @@ pub mod test {
balances: Vec<(StacksAddress, u64)>,
) -> StacksChainState {
let path = chainstate_path(test_name);
match fs::metadata(&path) {
Ok(_) => {
fs::remove_dir_all(&path).unwrap();
}
Err(_) => {}
if fs::metadata(&path).is_ok() {
fs::remove_dir_all(&path).unwrap();
};

let initial_balances = balances
Expand Down Expand Up @@ -2874,11 +2868,8 @@ pub mod test {
};

let path = chainstate_path(function_name!());
match fs::metadata(&path) {
Ok(_) => {
fs::remove_dir_all(&path).unwrap();
}
Err(_) => {}
if fs::metadata(&path).is_ok() {
fs::remove_dir_all(&path).unwrap();
};

let mut chainstate =
Expand Down Expand Up @@ -2964,11 +2955,8 @@ pub mod test {
};

let path = chainstate_path(function_name!());
match fs::metadata(&path) {
Ok(_) => {
fs::remove_dir_all(&path).unwrap();
}
Err(_) => {}
if fs::metadata(&path).is_ok() {
fs::remove_dir_all(&path).unwrap();
};

let mut chainstate =
Expand Down
14 changes: 6 additions & 8 deletions stackslib/src/chainstate/stacks/index/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,11 @@ impl<T: MarfTrieId> TrieCache<T> {
TrieCache::Everything(ref mut state) => {
state.store_node_and_hash(block_id, trieptr, node, hash);
}
TrieCache::Node256(ref mut state) => match node {
TrieNodeType::Node256(data) => {
TrieCache::Node256(ref mut state) => {
if let TrieNodeType::Node256(data) = node {
state.store_node_and_hash(block_id, trieptr, TrieNodeType::Node256(data), hash);
}
_ => {}
},
}
}
}

Expand All @@ -273,12 +272,11 @@ impl<T: MarfTrieId> TrieCache<T> {
match self {
TrieCache::Noop(_) => {}
TrieCache::Everything(ref mut state) => state.store_node(block_id, trieptr, node),
TrieCache::Node256(ref mut state) => match node {
TrieNodeType::Node256(data) => {
TrieCache::Node256(ref mut state) => {
if let TrieNodeType::Node256(data) = node {
state.store_node(block_id, trieptr, TrieNodeType::Node256(data))
}
_ => {}
},
}
}
}

Expand Down
Loading

0 comments on commit d2ed454

Please sign in to comment.