Skip to content

Add lints to catch missing traits#364

Merged
apoelstra merged 4 commits intorust-bitcoin:masterfrom
tcharding:lint-derives
Jan 7, 2022
Merged

Add lints to catch missing traits#364
apoelstra merged 4 commits intorust-bitcoin:masterfrom
tcharding:lint-derives

Conversation

@tcharding
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
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 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
Copy Markdown
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
Copy Markdown
Contributor

@apoelstra
Copy link
Copy Markdown
Member

Another point release sounds good to me.

@tcharding tcharding deleted the lint-derives branch January 11, 2022 04:45
chain-forgexcr45 added a commit to chain-forgexcr45/rust-secp256k1 that referenced this pull request Sep 28, 2025
69f44d9301c54168e9eaaf046db3d478e6739173 Manually implement Debug for SerializedSignature (Tobin Harding)
26921a31b8a030fba1141aba9ee61201c21b2d6a Add lints to catch missing traits (Tobin Harding)
35556e22f20a47341a6f5c3c68416713fdf354c3 Remove useless call to format (Tobin Harding)
0ad414a9825f8a3e8d634ee3207e44538497b09b Remove unneeded return statements (Tobin Harding)

Pull request description:

  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](rust-bitcoin/rust-bitcoin#587) 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.

ACKs for top commit:
  thomaseizinger:
    ACK 69f44d9301c54168e9eaaf046db3d478e6739173
  apoelstra:
    ACK 69f44d9301c54168e9eaaf046db3d478e6739173

Tree-SHA512: 18f2c52d207f962ef7d6749a57a35e48eb18a18fac82d4df4ff3dce549b69661cb27f66c4cae516ae5477f5b919d9197f70a5c924955605c73f8545f430c3b42
william2332-limf added a commit to william2332-limf/rust-secp256k1 that referenced this pull request Oct 2, 2025
69f44d9301c54168e9eaaf046db3d478e6739173 Manually implement Debug for SerializedSignature (Tobin Harding)
26921a31b8a030fba1141aba9ee61201c21b2d6a Add lints to catch missing traits (Tobin Harding)
35556e22f20a47341a6f5c3c68416713fdf354c3 Remove useless call to format (Tobin Harding)
0ad414a9825f8a3e8d634ee3207e44538497b09b Remove unneeded return statements (Tobin Harding)

Pull request description:

  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](rust-bitcoin/rust-bitcoin#587) 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.

ACKs for top commit:
  thomaseizinger:
    ACK 69f44d9301c54168e9eaaf046db3d478e6739173
  apoelstra:
    ACK 69f44d9301c54168e9eaaf046db3d478e6739173

Tree-SHA512: 18f2c52d207f962ef7d6749a57a35e48eb18a18fac82d4df4ff3dce549b69661cb27f66c4cae516ae5477f5b919d9197f70a5c924955605c73f8545f430c3b42
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