Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Apply Clippy lints single_match and redundant_pattern_matching #5740

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

jbencin
Copy link
Contributor

@jbencin jbencin commented Jan 23, 2025

Description

Apply the following Clippy lints:

  • single_match
  • redundant_pattern_matching

These lints reduce certain match and if let statements into simpler forms, and result in 370 fewer lines of code

@jbencin jbencin added the lint Related to linting/clippy/cargo warns label Jan 23, 2025
@jbencin jbencin requested a review from a team as a code owner January 23, 2025 19:01
obycode
obycode previously approved these changes Jan 23, 2025
Comment on lines 285 to 289
match self.runtime.sock.take() {
Some(s) => {
let _ = s.shutdown(Shutdown::Both);
}
None => {}
if let Some(s) = self.runtime.sock.take() {
let _ = s.shutdown(Shutdown::Both);
}

self.runtime.sock = Some(s);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe makes more sense to rewrite this segment as:

if let Some(mut socket) = self.runtime.sock {
   let _ = s.shutdown(Shutdown::Both);
}

Then we don't need the .take() -> replace() pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it makes more sense to use replace() instead of take() since we re-assign self.runtime.sock in the next line anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually is the call to shutdown() even necessary? The socket should close when we drop the TcpStream, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use replace() in 7e2d60e

obycode
obycode previously approved these changes Jan 24, 2025
Comment on lines 524 to 532
for commit_op in miner.block_commits.iter().rev() {
match SortitionDB::get_block_snapshot_for_winning_stacks_block(
if let Some(sn) = SortitionDB::get_block_snapshot_for_winning_stacks_block(
ic,
&fork_tip.sortition_id,
&commit_op.block_header_hash,
)
.unwrap()
{
Some(sn) => {
return Some(sn);
}
None => {}
return Some(sn);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about find_map?

miner.block_commits.iter().rev().find_map(|commit_op| {
   SortitionDB::get_block_snapshot_for_winning_stacks_block(
      ic,
      &fork_tip.sortition_id,
      &commit_op.block_header_hash,
   ).unwrap()
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's much better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kantai
kantai previously approved these changes Jan 24, 2025
Copy link
Member

@kantai kantai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just had a suggestion for a few additional opportunistic refactors.

@@ -264,41 +264,32 @@ impl BitcoinIndexer {
match net::TcpStream::connect((self.config.peer_host.as_str(), self.config.peer_port)) {
Ok(s) => {
// Disable Nagle algorithm
s.set_nodelay(true).map_err(|_e| {
test_debug!("Failed to set TCP_NODELAY: {:?}", &_e);
s.set_nodelay(true).map_err(|e| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like test_debug doesn't count towards variable use :/ I find this surprising, but clippy is complaining.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, but I'll undo the change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbencin jbencin requested a review from jferrant January 25, 2025 01:46
Copy link
Contributor

@obycode obycode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I just triggered a retry of the failing tests.

  • tests::nakamoto_integrations::follower_bootup_across_multiple_cycles
  • tests::signer::v0::no_reorg_due_to_successive_block_validation_ok
  • tests::signer::v0::single_miner_empty_sortition
  • net::tests::convergence::test_walk_star_allowed_15
  • net::tests::convergence::test_walk_star_15_plain

Copy link
Contributor

@obycode obycode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint Related to linting/clippy/cargo warns
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants