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 lints to catch missing traits #364

Merged
merged 4 commits into from
Jan 7, 2022

Conversation

tcharding
Copy link
Member

We can use the linters to help us catch type definitions that are missing 'standard' derives. 'standard' is project defined to be

  • Copy
  • Clone
  • Debug
  • PartialEq and Eq
  • PartialOrd and Ord
  • Hash

(I've assumed this to be true based on the code and an open PR in rust-bitcoin.)

While neither Rustc nor Clippy can find all of these, Rustc can warn for missing Copy and Debug implementations and these warnings can assist us find types that may need additional derives.

First two patches are trivial Clippy fixes in preparation for using the linter to improve type definitions crate wide.

Patch 3 adds

#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]

and fixes newly emitted warnings.

@tcharding
Copy link
Member Author

This PR came about from discussion here.

Clippy emits a few warnings:

 warning: unneeded `return` statement

As suggested, remove the unneeded return statements.
Clippy emits:

 warning: useless use of `format!`

As suggested, remove the useless call to `format!`.
Rustc can warn us when we forget to add `Copy` and `Deubg` trait
implementations to types.

Add lint directives to enable warnings for missing `Copy` and `Debug`
implementations. Use the newly emitted warnings to find types that do
not implement our 'standard' traits. These 'standard' traits are defined
as the set of attributes that it has been found beneficial to
opportunistically add to all types, these are

- Copy
- Clone
- Debug
- PartialEq and Eq
- PartialOrd and Ord
- Hash
Currently we have an implementation of `Debug` (also used by `Display`)
for `Signature` that first converts the sig to a `SerializedSignature`
then prints it as hex.

We would like to have an implementation of `Debug` for
`SerializedSignature`, this cannot be derived because of the `data: [u8;
field]`. We can manually implement `Debug` for `SerializedSignature`
exactly as it is currently done for `Signature` and call this new
implementation from `Signature::fmt()`.

This code path is already tested in `lib.rs` in the test function
`signature_display`.
Copy link
Contributor

@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.

ACK 69f44d9

I don't think we currently fail the build on warnings so this is mere a reminder for devs while working locally but that is already an improvement.

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 69f44d9

lol @ deriving traits on types that are impossible to instantiate, but if it lets us run these lints cleanly it's good by me.

@apoelstra apoelstra merged commit db11cf9 into rust-bitcoin:master Jan 7, 2022
@sanket1729
Copy link
Member

The lints for KeyPair were originally present in 0.20.3 and were removed in 0.21.0 during #327 . I think it is worth making yet another point release from the master branch because we have not merged anything breaking yet! What do you think @apoelstra?

@dr-orlovsky
Copy link
Contributor

@apoelstra
Copy link
Member

Another point release sounds good to me.

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.

5 participants