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

Pedantic linting #1679

Open
wants to merge 41 commits into
base: main
Choose a base branch
from

Conversation

van-sprundel
Copy link

@van-sprundel van-sprundel commented Oct 12, 2024

  • nts-pool-ke
  • ntp-proto
  • ntpd

Changes for #1658.

There are some opinionated changes here, so feel free to call out anything that's not wanted.
All changes are first ran with cargo clippy --fix -p [project] --all-targets --all-features

I've left out clippy::module_name_repetitions as it doesn't add much here.

Some notable lints from the pedantic group:

  • Functions that can error/panic now have error docs when relevant.
  • Big functions (>100 lines) are split into different functions.
    • Except test modules, which are marked with #[allow]
  • ntpd::ctl::NtpCtlOptions now uses the action state instead of bools.
  • Some match cases became if () { } else { } where relevant.
  • Some let foo = if ... else became let ... else where relevant.
  • async and Result<_> are removed on places where it wasn't used and relevant.
  • const and enum mid-function are moved up at the beginning of the function, or moved up a scope.
  • ntpd::daemon:;ntp_source::SourceTask::run has been split up into multiple functions.
  • Conversions that could truncate or lose sign are marked as allowed where relevant.
    • For arm and darwin, all truncation is allowed.

This became a pretty big PR in the end. I'm not sure how we could keep the impact minimal. Splitting it into multiple branches based on crates doesn't help much, because the ntp-proto crate has ~1000 lines of changes.

Add semicolon on non-returning functions

Remove redundant else block

We already break in the check

Put variable in format macro

Move consts up in function

ignore lint for incomplete function

Add error to function docs
Replaced bools with action
Copy link

codecov bot commented Oct 12, 2024

Codecov Report

Attention: Patch coverage is 80.55556% with 203 lines in your changes missing coverage. Please review.

Project coverage is 81.36%. Comparing base (eba70a7) to head (7f66c7b).

Files with missing lines Patch % Lines
ntpd/src/daemon/ntp_source.rs 63.97% 58 Missing ⚠️
ntp-proto/src/nts_record.rs 92.24% 18 Missing ⚠️
ntpd/src/daemon/system.rs 0.00% 17 Missing ⚠️
ntpd/src/force_sync/mod.rs 0.00% 16 Missing ⚠️
ntp-proto/src/packet/mod.rs 88.70% 14 Missing ⚠️
ntp-proto/src/keyset.rs 64.28% 10 Missing ⚠️
ntp-proto/src/nts_pool_ke.rs 0.00% 9 Missing ⚠️
ntpd/src/daemon/config/mod.rs 72.72% 9 Missing ⚠️
ntpd/src/force_sync/algorithm.rs 0.00% 9 Missing ⚠️
nts-pool-ke/src/cli.rs 0.00% 6 Missing ⚠️
... and 16 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1679      +/-   ##
==========================================
+ Coverage   81.33%   81.36%   +0.02%     
==========================================
  Files          64       64              
  Lines       19751    19880     +129     
==========================================
+ Hits        16065    16175     +110     
- Misses       3686     3705      +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@van-sprundel van-sprundel marked this pull request as ready for review October 15, 2024 20:48
@davidv1992 davidv1992 self-assigned this Oct 16, 2024
Copy link
Member

@davidv1992 davidv1992 left a comment

Choose a reason for hiding this comment

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

Thank you for picking this up. The changes make very clear that this was a good idea, even though it will still be some work to fully get these changes to land. Below I have made a number of comments. Note that most of these are notes to ourselves that we need to improve code in some places more and those need not be blockers for landing this in my eyes. I have marked those that do require more attention.

Please also note that given the wide scope of these changes, I am not comfortable merging these only on my opinion, so once this is further along there is going to be a review by a second person.

Again, thank you for doing the legwork on this. By the looks of it, you saved us quite some work. I hope you can bear with us while we make the push to get this in reviewable enough to merge it.

Comment on lines +19 to +22
/// # Panics
///
/// Will panic if `addr` is not a valid ip address.
#[must_use]
Copy link
Member

@davidv1992 davidv1992 Oct 16, 2024

Choose a reason for hiding this comment

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

Note to self: Hmm, this function will actually never panic, I think the comment is required because of a false positive on the unwrap (which will always succeed since we take 4 bytes in the indexing operation before it).

Comment on lines +90 to +91
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
Copy link
Member

@davidv1992 davidv1992 Oct 16, 2024

Choose a reason for hiding this comment

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

Note to self: We should probably try to move these to more specific statements, given how complex the function actually is.

Comment on lines 205 to +208
pub fn is_in(&self, addr: &IpAddr) -> bool {
match addr {
IpAddr::V4(addr) => self.is_in4(addr),
IpAddr::V6(addr) => self.is_in6(addr),
IpAddr::V4(addr) => self.is_in4(*addr),
IpAddr::V6(addr) => self.is_in6(*addr),
Copy link
Member

Choose a reason for hiding this comment

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

Note to self: consider moving the deref up even further.

Comment on lines +212 to +215
/// # Panics
///
/// Panics if `addr` has invalid octets.
fn is_in4(&self, addr: Ipv4Addr) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

Note to self: can this panic occur?

@@ -78,12 +81,12 @@ impl KeySetProvider {
}

/// Rotate a new key in as primary, forgetting an old one if needed
#[allow(clippy::cast_possible_truncation)]
Copy link
Member

Choose a reason for hiding this comment

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

Note to self: consider moving this into the function to avoid allowing this blanketly on the whole.

Comment on lines +1640 to +1642
/// # Errors
///
/// Returns error if getting the TLS bool `wants_write` fails.
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't actually error

@@ -1,3 +1,4 @@
#![allow(clippy::cast_possible_truncation)]
Copy link
Member

Choose a reason for hiding this comment

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

Note to self: Eliminate here, too general

Comment on lines -308 to -317
3 => {
let (header, header_size) =
NtpHeaderV3V4::deserialize(data).map_err(|e| e.generalize())?;
let mac = if header_size != data.len() {
Some(Mac::deserialize(&data[header_size..]).map_err(|e| e.generalize())?)
} else {
None
};
Ok((
NtpPacket {
Copy link
Member

Choose a reason for hiding this comment

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

Good refactor on this match, but again needs more involved review. Could you please make this a separate PR?

Comment on lines -130 to -146

/// from the arguments resolve which action should be performed
fn resolve_action(&mut self) {
if self.help {
self.action = NtpCtlAction::Help;
} else if self.version {
self.action = NtpCtlAction::Version;
} else if self.validate {
self.action = NtpCtlAction::Validate;
} else if self.status {
self.action = NtpCtlAction::Status;
} else if self.force_sync {
self.action = NtpCtlAction::ForceSync;
} else {
self.action = NtpCtlAction::Help;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Needs fix: it is actually quite subtle, but removing this, in combination with the changes above, actually changes behaviour, and in an unwanted way. In particular, it changes behaviour of the -v and -h flags overriding all other actions, which is undesired. Please revert.

Comment on lines -140 to -149
let actions = match selected {
SelectResult::Recv(result) => {
tracing::debug!("accept packet");
match accept_packet(result, &buf, &self.clock) {
AcceptResult::Accept(packet, recv_timestamp) => {
let send_timestamp = match self.last_send_timestamp {
Some(ts) => ts,
None => {
debug!(
"we received a message without having sent one; discarding"
Copy link
Member

Choose a reason for hiding this comment

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

Needs fix: Again, this is likely (part of) a good refactor, but too hard to review well in this context, please split this off into a separate PR.

@davidv1992
Copy link
Member

@rnijveld As a headsup, just so you are aware of this PR, I am going to ask you to review this once it is a bit further along. Most of it is relatively straightforward, and hopefully we can split the parts that aren't into separate PRs. It does highlight a few places to where we should probably take a second look at code, particularly around casts.

@van-sprundel
Copy link
Author

van-sprundel commented Nov 20, 2024

Sorry for the radio silence, I'll rebase the PR and take a look at the review over the weekend!

Am I safe to assume that all the reviews without "note to self" are something I can refactor?

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.

2 participants