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

Explore clippy rules and apply quality of life recommendations #122

Closed
wants to merge 57 commits into from

Conversation

xla
Copy link
Contributor

@xla xla commented Jan 3, 2020

First of, this is an attempt to codify clippy rules. And find the right amount to be applied for consistent code shape. Least it helps me explore the codebase. Will keep inventory which commits are driven by what rule. We can disregard any if too rigid.


  • Referenced an issue explaining the need for the change
  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Updated CHANGES.md

mod time;
mod validate;
mod version;
mod vote;
Copy link
Contributor

@tarcieri tarcieri Jan 3, 2020

Choose a reason for hiding this comment

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

The disadvantage of relying on re-exports of types defined in non-pub modules is rustc will refer to those types by their fully qualified path regardless of the visibility of the module it's contained in, which leaves someone trying to debug in the bizarre position that the compiler referring to a path which cannot be accessed and does not exist in rustdoc.

That said, I do sometimes do this myself, but it's usually when I'm trying to create a crate where the code is factored into modules internally but all types are exported at the toplevel (and when I do that, it's generally because the external API surface of the crate is small to begin with).

The other disadvantage is this approach eliminates the ability to have module-level documentation visible in the rustdoc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would you propose generally? I think amino_types module organisation is ripe for a review anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Re-export them, but leave the containing modules pub if they're part of the public API

@@ -3,7 +3,7 @@ use std::convert::TryInto;

/// Extend the original prost::Message trait with a few helper functions in order to
/// reduce boiler-plate code (and without modifying the prost-amino dependency).
pub trait AminoMessage: prost_amino::Message {
pub trait Message: prost_amino::Message {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍I'm a fan of eliminating module (and crate) names from type names. Some relevant API guidelines and issues:

Importing modules rather than individual types and referring to types by their module names can also help cut down on the number of imports.

It's hard to avoid multiple crate versions given current transient
dependencies, some of them we control and we are following up to bring
most of them on par:

tendermint/amino_rs#20
@xla xla marked this pull request as ready for review January 20, 2020 12:25
@xla xla requested a review from brapse January 20, 2020 12:31
Copy link
Member

@liamsi liamsi left a comment

Choose a reason for hiding this comment

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

Massive PR ... I'm about 3/4 through. Looks great! Hope this will lead to a more consistent code base. Will give it another pass tmrw.

fn from(height: Height) -> Self {
height.0 as Self
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Did we verify this isn't used in the KMS for instance?

Copy link
Member

Choose a reason for hiding this comment

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

Does it matter? Isn't this commit non-breaking?

Copy link
Member

Choose a reason for hiding this comment

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

Weird, I saw this comment on a different commit: 67f7358#r368718476.

The correct one is 7750986#r368718476.

So yes we should verify we can get rid of this

Ok(have) => assert_eq!(have, msg),
Err(err) => panic!(err.to_string()),
}
let have = PubKeyRequest::decode(want.as_ref()).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

Didn't 2 other commits turn all option/result unwraps into expects?

Ok(have) => assert_eq!(have, msg),
Err(err) => panic!(err),
}
let have = PubKeyResponse::decode(encoded.as_ref()).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

/// and trusted validator set is sufficient for a commit to be
/// accepted going forward.
/// The default implementation returns true, iff at least a third of the trusted
/// voting power signed (in other words at least one honest validator signed).
/// Some clients might require more than +1/3 and can implement their own
/// TrustLevel which can be passed into all relevant methods.
/// [`TrustLevel`] which can be passed into all relevant methods.
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we forgot to rename TrustLevel to TrustThreshold here. Running cargo +nightly doc --open --no-deps will error now with something like

`[TrustLevel]` cannot be resolved, ignoring it.

because of a #[deny(warnings)].

Does this mean building for https://docs.rs/ won't work now?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if docs.rs will break if you have intra-doc resolution failures and deny warnings but possibly.

At any rate, it's good to capture and correct these. Running cargo test --doc on nightly will catch them.

/// but there may be some intersection. The trust_level parameter allows clients to require more
/// than +1/3 by implementing the TrustLevel trait accordingly.
/// but there may be some intersection. The `trust_level` parameter allows clients to require more
/// than +1/3 by implementing the `TrustLevel` trait accordingly.
Copy link
Member

@liamsi liamsi Jan 22, 2020

Choose a reason for hiding this comment

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

Consistent would be [`TrustThreshold `]. Maybe a follow-up PR could deal solely with linkage in documentation.

Copy link
Member

@liamsi liamsi left a comment

Choose a reason for hiding this comment

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

Great work @xla!

I think these two comments require acting in form of replying to the comment or addressing the suggested changes:
https://github.com/interchainio/tendermint-rs/pull/122/files#r369395194
https://github.com/interchainio/tendermint-rs/pull/122/files#r368546339

Everything else looks good to me. As a general comment: even though chunking changes into commits addressing one lint at a time made this easier to review, I would vouch for splitting up a PR like this into separate PRs instead of a single (large one) for the future.

Ideally, I would let @tarcieri give it another pass. AFAIK he's trying to unblock some stuff on the kms. Although this PR looks like it doesn't break anything the kms uses, I would appreciate another pair of eyes here for confirmation in this context.

@@ -23,6 +23,7 @@ impl Transaction {
Self(into_vec.into())
}

#[allow(clippy::missing_const_for_fn)]
Copy link
Member

Choose a reason for hiding this comment

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

Why are there exceptions?

@@ -36,7 +36,13 @@ fn get_split_point(length: usize) -> usize {
0 => panic!("tree is empty!"),
1 => panic!("tree has only one element!"),
2 => 1,
_ => length.next_power_of_two() / 2,
_ => {
let (div, overflow) = length.next_power_of_two().overflowing_div(2);
Copy link
Member

Choose a reason for hiding this comment

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

Is this really necessary? length and next_power_of_two are both usize which will never overflow ... Same goes for the wrapping_div above

_ => return Err(err!(ErrorKind::Parse, "invalid units")),
};

let numeric_part = s.chars().take(s.len() - units.len()).collect::<String>();
let numeric_part = match units {
Copy link
Member

Choose a reason for hiding this comment

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

What does this enum have to do with Rework use of unreachable ?

Also, can the Unit variants contain their own string representation so we don't need a match here like before?

@ebuchman
Copy link
Member

ebuchman commented Feb 2, 2020

Reviewed this in full. Great work but I agree it should be broken up into smaller PRs with loosely themed lints and may need another pass/updates on certain commits.

Also we need to pay more attention to breaking changes and track them accordingly. Maybe the smaller PRs for this can also be distinguished by whether or not they're breaking.

@@ -19,17 +19,17 @@ pub enum Code {
}

impl Default for Code {
fn default() -> Code {
Copy link
Member

Choose a reason for hiding this comment

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

So in general I think this is probably good, but I wonder if in the case of large impl blocks if it would be better to use the explicit type name.

@brapse
Copy link
Contributor

brapse commented Mar 9, 2020

@gterzian Could you potentially pick this up and drive it home once #125 is merged int 🙏

@xla
Copy link
Contributor Author

xla commented Mar 9, 2020

@brapse Currently working on incremental changes on top of latest master to superseed this PR.

@brapse
Copy link
Contributor

brapse commented Mar 9, 2020

@xla Can/should we link the new PR to this one and close this?

@ebuchman
Copy link
Member

Let's track in #193 and work on it in the follow ups

@ebuchman ebuchman closed this Mar 28, 2020
@xla xla deleted the xla/clippy branch March 29, 2020 03:59
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.

6 participants