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

Add clippy to CI #448

Merged
merged 10 commits into from Jun 17, 2022
Merged

Add clippy to CI #448

merged 10 commits into from Jun 17, 2022

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Jun 10, 2022

The first 8 patches clear clippy warnings. Next we add a CI job to run clippy. Finally we add a githooks directory that includes running clippy, also adds a section to the README on how to use the githooks. This is identical to the text in the open PR on rust-bitcoin that adds githooks without yet adding clippy.

Note: The new clippy CI job runs and is green :)

@tcharding tcharding mentioned this pull request Jun 10, 2022
apoelstra
apoelstra previously approved these changes Jun 10, 2022
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 5551f2c

@apoelstra
Copy link
Member

Needs rebase now, sorry.

Clippy emits:

  warning: this import is redundant

This is a remnant of edition 2015, now we have edition 2018 we no longer
need this import statement.
Clippy emits:

  warning: statics have by default a `'static` lifetime

Static strings no longer need an explicit lifetime, remove it.
Clippy emits:

  warning: this expression creates a reference which is immediately
  dereferenced by the compiler

Remove the explicit reference.
@tcharding
Copy link
Member Author

No changes in force push, rebase only, there was a merge conflict in one of the unit tests in commit: 35d59e7 Remove explicit 'static lifetime.

elichai
elichai previously approved these changes Jun 16, 2022
Copy link
Member

@elichai elichai left a comment

Choose a reason for hiding this comment

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

ACK d81d2fe

# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
Copy link
Contributor

Choose a reason for hiding this comment

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

that's already the name of the file?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch, I modified the whole comment to better describe the hook. Thanks.

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

The exec should be fixed, I believe.

exec git diff-index --check --cached $against --

# Check that code lints cleanly.
exec cargo clippy --features=rand-std,recovery,lowmemory,global-context --all-targets -- -D warnings
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't work since exec replaces the process (just like the exec syscall) thus this exec never executes

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks man, seems I tested whitespace but not clippy before pushing this. Out of interest I tried removing both the execs and adding set -e but that did not work, I then tried the form cmd || exit 1 for both and that works.

src/context.rs Outdated
@@ -191,7 +188,8 @@ mod alloc_only {
/// ctx.seeded_randomize(&seed);
/// # }
/// ```
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
#[allow(clippy::let_and_return)] // Triggers when `rand-std` is not enabled.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps cfg_attr to limit the scope? Not a big deal though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice trick! I had to think about your suggestion for a bit then I worked it out

    #[cfg_attr(not(feature = "rand-std"), allow(clippy::let_and_return, unused_mut))]

For the second time today; thanks for the lesson :)

src/ecdsa/mod.rs Outdated
let le_counter_bytes : [u8; 4] = mem::transmute(le_counter);
for (i, b) in le_counter_bytes.iter().enumerate() {
let le_counter = counter.to_le_bytes();
for (i, b) in le_counter.iter().enumerate() {
extra_entropy[i] = *b;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This whole chunk could be just extra_entropy[..4].copy_from_slice(&counter.to_le_bytes);

Also below (can't comment there) cast is probably better than as. extra_entropy.as_ptr().cast::<ffi::types::c_void>();

Copy link
Member Author

Choose a reason for hiding this comment

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

I used both suggestions, I was unable to justify the as -> cast change in the commit log, can you explain why its better please, just for my learning?

apoelstra
apoelstra previously approved these changes Jun 16, 2022
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK d81d2fe

@apoelstra
Copy link
Member

Will hold off on merging since @Kixunil's nits look good (and the exec thing might be more than a nit)

@Kixunil
Copy link
Collaborator

Kixunil commented Jun 16, 2022

exec is definitely not a nit. It breaks the main purpose of this PR. :)

@apoelstra
Copy link
Member

Well, it breaks the main purpose of the pre-commit hook :) but the CI job is not affected. But yes, I see your point.

@tcharding tcharding dismissed stale reviews from apoelstra and elichai via fddca7c June 16, 2022 23:45
@tcharding tcharding force-pushed the 06-10-clippy branch 2 times, most recently from fddca7c to 6c6a577 Compare June 16, 2022 23:58
Since we bumped the MSRV we have `to_le_bytes` available now, use it
instead of `mem::transmute`.

Remove code comment suggesting to do exactly this and clear clippy
warning.

While we are at it, use `cast` instead of `as`.
We have a whole bunch of unsafe code that calls down to the FFI layer.
It would be nice to have clippy running on CI, these safety docs
warnings are prohibiting that. Until we can add the docs add a compiler
attribute to allow the lint.
Clippy emits:

  warning: returning the result of a `let` binding from a block

This is due to feature guarded code, add 'allow' attribute. Use
`cfg_attr` to restrict the allow to when it is needed. Add the already
present `unused_mut` inside the `cfg_attr` guard also.
Clippy emits:

  warning: manual implementation of `str::repeat` using iterators

As suggested, use `"a".repeats()`.
We are explicitly testing various boolean statements, tell clippy to
allow less than minimal statements.
In order to keep the codebase linting cleanly add a CI job to run
clippy.
Add `githooks` directory and:

- Copy the default pre-commit hook into new githooks directory
- Add a call to clippy to the pre-commit hook
- Add a section to the README instructing devs how to use the new
  githooks
@tcharding
Copy link
Member Author

Solid reviewing crew, thank you. Force push includes the various changes above.

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 65186e7

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 65186e7

@apoelstra apoelstra merged commit a1ac3fb into rust-bitcoin:master Jun 17, 2022
@tcharding tcharding deleted the 06-10-clippy branch June 20, 2022 22:03
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 65186e7

@apoelstra
Copy link
Member

Lol, sorry, I meant to test and ack 488.

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.

None yet

5 participants