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

.cargo: Run clippy on ALL the source files #2949

Merged
merged 9 commits into from
Oct 4, 2022
Merged

Conversation

thomaseizinger
Copy link
Contributor

@thomaseizinger thomaseizinger commented Sep 28, 2022

Description

image

I turns out you need a PhD in clippology to figure out how to have it lint every source file in a repository. Previously, we were not passing --workspace to clippy which means it didn't lint a lot of source files. I am not sure which subset it was linting and which one it skipped but with this PR, we should be linting every source file in this repository.

Review guide

This PR is best reviewed patch-by-patch. The massive diff comes from the gossipsub tests but most of that is just removing one layer of modules which is done in a separate commit.

  • The first commit is completely auto-generated by running clippy --fix. Probably not worth reviewing. If you run the command mentioned in the commit locally and follow it with cargo fmt, you should get the same result!
  • The second commit is manual changes. Those should probably be reviewed.
  • The third and forth commit just moves everything from the tests module one layer up, fixing the last clippy lint. I tried to split this up so the diff shows how minimal the changes are but Git chokes on it and likes to see unchanged newlines instead of just showing the whitespace diff. You can verify locally that it is just whitespace changes by running: git diff 9443c7a4 9443c7a4^ --ignore-all-space
  • The fifth commit runs cargo fmt. You can again verify this locally if you want. Splitting this out makes the previous diff command work.
  • The sixth commit enforces clippy across the entire workspace.

Links to any relevant issues

Open Questions

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

Previously, we were running clippy without --workspace which left
out examples and tests of all sub-crates.
Copy link
Contributor Author

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Made some mistakes apparently when fixing the lints.

protocols/gossipsub/src/peer_score/tests.rs Outdated Show resolved Hide resolved
protocols/gossipsub/src/peer_score/tests.rs Outdated Show resolved Hide resolved
protocols/gossipsub/src/behaviour/tests.rs Outdated Show resolved Hide resolved
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

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

First off, thanks for doing this!

Thanks for making it easy to review.

Thanks for making it very entertaining!

Goodbye to all those .clone() calls.

if num_nodes < 2 || num_nodes > 50 {
if !(2..=50).contains(&num_nodes) {
Copy link
Member

Choose a reason for hiding this comment

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

Not that it is important, though I am curious, do folks find this easier to read?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pretty sure this one was done by clippy, I don't remember writing that.

If I were to write this test, I would probably split it into two ifs:

if num_nodes < 2 {
	return TestResult::discard();
}

if num_nodes > 50 {
	return TestResult::discard();
}

Copy link
Contributor Author

@thomaseizinger thomaseizinger Sep 30, 2022

Choose a reason for hiding this comment

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

Or make a newtype that implements Arbitrary that only generates numbers between 3 and 49. We can call it NonZeroOneTwoU6Minus15!

Copy link
Member

Choose a reason for hiding this comment

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

👍 on the above suggestions in general, though I think in this particular case leaving it as is is just fine.

Copy link
Member

@jxs jxs left a comment

Choose a reason for hiding this comment

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

LGTM, great effort Thomas!

@@ -497,7 +489,8 @@ async fn test_outbound_failure() {
}

let inactive = servers.split_off(1);
// Drop the handles of the inactive servers to kill them.

#[allow(clippy::needless_collect)] // Drop the handles of the inactive servers to kill them.
Copy link
Member

Choose a reason for hiding this comment

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

out of curiosity to understand in this case why clippy was complaining I commented the allow and ran the cargo +nightly clippy --all-features --all-targets -- -A clippy::type_complexity -A clippy::pedantic -D warnings command. But clippy didn't complain, is it not required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You need the --workspace flag! My best guess is that this is because our root Cargo.toml is a crate and a workspace declaration.

I wonder whether we could avoid this if we put the meta crate in a directory too.

Thoughts @mxinden?

Copy link
Member

Choose a reason for hiding this comment

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

yeah, was running outdated rust version 😅 sorry. Btw with the latest nightly there are new lints triggered for large_enum_variant.

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, was running outdated rust version 😅 sorry. Btw with the latest nightly there are new lints triggered for large_enum_variant.

Yeah I know :)

I opened #2951 for this recently!

@thomaseizinger thomaseizinger merged commit 1b79324 into master Oct 4, 2022
@thomaseizinger thomaseizinger deleted the clippy-workspace branch October 4, 2022 07:24
mergify bot pushed a commit that referenced this pull request Mar 8, 2023
Currently, our top-level `Cargo.toml` manifest represents a crate AND a workspace. This causes surprising behaviour (e.g. #2949) where we need to explicitly pass `--workpace` to every command to run it on the entire workspace and not just the meta crate.

My moving the meta crate into its own directory, the root manifest file is a virtual manifest only and thus, every `cargo` command will automatically default to running on the entire workspace.

On top of this, I personally find it easier to understand if workspace and crate manifests are not mixed.

Pull-Request: #3536.
umgefahren pushed a commit to umgefahren/rust-libp2p that referenced this pull request Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants