Skip to content

Fix clippy warnings#241

Closed
tcharding wants to merge 23 commits intobitcoindevkit:masterfrom
tcharding:126-clippy
Closed

Fix clippy warnings#241
tcharding wants to merge 23 commits intobitcoindevkit:masterfrom
tcharding:126-clippy

Conversation

@tcharding
Copy link
Copy Markdown
Contributor

@tcharding tcharding commented Dec 21, 2020

Description

As described in issue #126 we have a bunch of clippy warnings. This PR clears the warnings.

Notes to the reviewers

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature
  • I've updated CHANGELOG.md

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

Fixes: #126

(I think this resolves the issue, the CI description looks stale but perhaps I'm misunderstanding it, please review the issue description to see if I have missed anything).

Thanks

@tcharding tcharding changed the title 126 clippy Fix clippy warnings Dec 21, 2020
@tcharding tcharding marked this pull request as draft December 21, 2020 09:31
@afilini
Copy link
Copy Markdown
Member

afilini commented Dec 21, 2020

Concept ACK, you've probably missed something in testutils-macros/src/lib.rs where the tests are generated for Blockchain backends. That's causing the CI failure in the Electrum test.

@tcharding tcharding marked this pull request as ready for review December 30, 2020 02:16
As directed by clippy use `unwrap_or_else` in order to take advantage of
lazy evaluation.
As directed by clippy use `!a.is_empty()` instead of `a.len() > 0`.
Its more idiomatic to implement `Default` instead of a constructor with
no args `new`. Found by clippy.
const str types do not need an explicit lifetime, remove it. Found by
clippy.
Clippy emits warning:

	warning: manual `!RangeInclusive::contains` implementation

Use `RangeInclusive::contains` as suggested.
Clippy emits warning about unnecessary wraps. If a function never fails
no need to return a `Result`. In the case where we require a function
signature that includes an error return use `allow` attribute.
No need to clone copy types, found by clippy.
Clippy emits warning:

	warning: needlessly taken reference of both operands

Remove the explicit reference's as suggested.
Clippy warns a bunch of times due to float comparison. All are within
unit tests and we test against values with one single zero in the
decimal place. We can safely convert to `u32` within the assertion.
Clippy emits warning:

	warning: avoid using `collect()` when not needed

As suggested by clippy just use `count` directly on the iterator instead
of `collect` followed by `len`.
As suggested by clippy we can use `.next()` on an iterator instead of
`nth(0)`. Although arguably no clearer, and certainly no worse, it keeps
clippy quiet and a clean lint is a good thing.
Clippy emits warning:

  warning: field assignment outside of initializer for an instance
  created with Default::default()

Do as suggested by clippy and use the default init pattern.

```
    let foo = Foo {
    	bar: ...,
        Default::default()
    }
```
Clippy emits warning:

    warning: the `err @ _` pattern can be written as just `err`

As suggested by clippy remove the `@ _`.

Note: This patch contains Cargo cult programming. I do not actually
understand what this patter is meant to do, blindly following clippy.
Functions that never err do not need to return and error type. Remove
the unnecessary error type and fix all the call sites.
Clippy emits warning:

    warning: this `.into_iter()` call is equivalent to `.iter()` and
    will not consume the `Vec`

Use `iter` as suggested by clippy.
Manually matching can be replace with the `matches!` macro. Found by
clippy.
Redundant closure can be removed as suggested by clippy.
Clippy warns due to manual implementation of saturating subtraction. As
suggested by clippy use `saturating_sub` instead.
Calling a function within `unwrap_or` is sub-optimal because the
function is always evaluated. By using `unwrap_or_else` we get lazy
evaluation. Found by clippy.
 Clippy complains about use of a mutex, suggesting we use an
 `AtomicUsize`. While the same functionality _could_ be achieved using an
 `AtomicUsize` and a CAS loop it makes the code harder to reason about
 for little gain. Lets just quieten clippy with an allow attribute and
 document why we did so.
The `ChunksIterator` constructor is only used when either `electrum` or
`esplora` features are enabled. Conditionally build it so that we do not
get a clippy warning when building without these features.
Remove the TODO; refactor matching to correctly handle conditionally
built `Sled` variants. Use `unreachable` instead of `unimplemented` with
a comment hinting that this is a bug, this makes it explicit, both at
runtime and when reading the code, that this match arm should not be hit.
@tcharding tcharding marked this pull request as draft December 30, 2020 02:30
Clippy emits error:

 comparing trait object pointers compares a non-unique vtable address

The vtable is an implementation detail, it may change in future. we
should not be comparing vtable addresses for equality. Instead we can
get a pointer to the data field of a fat pointer and compare on that.
@tcharding tcharding marked this pull request as ready for review December 30, 2020 04:39
@notmandatory
Copy link
Copy Markdown
Member

notmandatory commented Dec 31, 2020

I think most of these fixes are for the nightly version of clippy. As long as the changes experimental recommends are compatible with stable clippy they're still good to fix, but you just needs to be double for each of the features in the build-test cont_integration.yml workflow:

cargo +stable clippy --features <features> -- -D warnings

Comment thread src/wallet/signer.rs
);
assert!(matches!(signers.find(id1), Some(signer) if is_equal(signer, &signer1)));
assert!(matches!(signers.find(id2), Some(signer) if is_equal(signer, &signer2)));
assert!(matches!(signers.find(id3.clone()), Some(signer) if is_equal(signer, &signer3)));
Copy link
Copy Markdown
Collaborator

@LLFourn LLFourn Dec 31, 2020

Choose a reason for hiding this comment

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

❓ Can't we just make dyn Signer Eq?

@afilini is there some coherent way we can identify a signer i.e. by it's xprv/xpub or something more clever?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, makes sense. I would add an .id() method to Signer that returns a SignerId. Then we can implement Eq by checking the id returned by both signers.

Let me open an issue for this

@tcharding
Copy link
Copy Markdown
Contributor Author

tcharding commented Jan 4, 2021

I think most of these fixes are for the nightly version of clippy.

Ok, I think I need to learn how to correctly set up my dev environment for new projects before attempting to contribute :)

As long as the changes experimental recommends are compatible with stable clippy they're still good to fix, but you just needs to be double for each of the features in the build-test cont_integration.yml workflow:

Closing for now.

cargo +stable clippy --features <features> -- -D warnings

Thanks for schooling me. This was not the only project where I was wondering why so many clippy warnings were there on master - turns out its me :(

I will pull out the relevant changes and re-open a new PR later on. Sorry for the noise.

@tcharding tcharding closed this Jan 4, 2021
@notmandatory
Copy link
Copy Markdown
Member

Hey no problem, rust tooling is great but does have it's own learning curve to deal with. For this project we run our CI build/test pipeline against minimum version 1.45.0 and current stable. You can set your default rust toolchain version to stable with the command:

rustup default stable

@afilini afilini mentioned this pull request Jan 5, 2021
@tcharding tcharding mentioned this pull request Feb 13, 2021
9 tasks
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.

Fix clippy warnings for all optional features

4 participants