Skip to content

Fix public API clippy warnings#1161

Merged
apoelstra merged 3 commits intorust-bitcoin:masterfrom
tcharding:08-02-clippy-public-api
Aug 2, 2022
Merged

Fix public API clippy warnings#1161
apoelstra merged 3 commits intorust-bitcoin:masterfrom
tcharding:08-02-clippy-public-api

Conversation

@tcharding
Copy link
Copy Markdown
Member

@tcharding tcharding commented Aug 1, 2022

Turns out by default clippy does not lint certain parts of the public API.

Specifically, by setting avoid-breaking-exported-api = false we can get clippy to lint the public API for various things including
wrong_self_convention.

This means the saga of to/into/as continues ... we have used the wrong from for many of our to_* methods, they should consme self. Patch 1 fixes them. Patch 2 adds an allow. Patch 3 fixes a few error enum variant identifiers (removes Error suffix).

Turns out by default clippy does not lint certain parts of the public
API. Specifically, by setting

  avoid-breaking-exported-api = false

we can get clippy to lint the public API for various things including
`wrong_self_convention`.

Clippy emits:

 warning: methods with the following characteristics: (`to_*` and `self`
 type is `Copy`) usually take `self` by value

As suggested, take `self` by value for methods named `to_*`.
Clippy gives a warning about `wrong_self_convention` because we consume
self in a method called `is_*`. We have to consume self because the
object has a generic `E` (error type) that does not implement `Copy`.
Error variants should not end with the same identifier as the enum,
i.e., they should not stutter.

Found by clippy after setting:

  avoid-breaking-exported-api = false
@tcharding tcharding force-pushed the 08-02-clippy-public-api branch from cddfb63 to 5cef2f1 Compare August 1, 2022 22:55
@tcharding
Copy link
Copy Markdown
Member Author

Once I actually ran the tests (bad Tobin), I was sad to see that with all these pubic API changes this PR only includes changes to one unit test and one rustdoc example. I think we need more thorough testing of our public API if we are going to achieve stability.

@tcharding tcharding marked this pull request as ready for review August 1, 2022 22:57
Copy link
Copy Markdown
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 5cef2f1

/// }
/// ```
#[allow(clippy::wrong_self_convention)] // E is not Copy so we consume self.
pub fn is_sighash_single_bug(self) -> Result<bool, E> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I knew this was ugly when I saw it the first time. Sadly, I have no idea how to improve the name.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ha that's funny, I thought the same myself on both accounts.

Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

code review ACK 5cef2f1

Copy link
Copy Markdown
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 5cef2f1

@apoelstra apoelstra merged commit 5d44b2d into rust-bitcoin:master Aug 2, 2022
@apoelstra apoelstra mentioned this pull request Aug 2, 2022
3 tasks
@Kixunil Kixunil added the API break This PR requires a version bump for the next release label Aug 2, 2022
@Kixunil Kixunil added this to the 0.29.0 milestone Aug 2, 2022
@tcharding tcharding deleted the 08-02-clippy-public-api branch August 2, 2022 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API break This PR requires a version bump for the next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants