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

Copy link

codecov bot commented Oct 12, 2024

Codecov Report

Attention: Patch coverage is 79.67033% with 185 lines in your changes missing coverage. Please review.

Project coverage is 81.60%. Comparing base (419832f) to head (240d6f8).

Files with missing lines Patch % Lines
ntpd/src/daemon/ntp_source.rs 43.58% 22 Missing ⚠️
ntp-proto/src/nts_record.rs 92.27% 18 Missing ⚠️
ntpd/src/daemon/system.rs 0.00% 17 Missing ⚠️
ntpd/src/force_sync/mod.rs 0.00% 17 Missing ⚠️
ntp-proto/src/packet/mod.rs 88.70% 14 Missing ⚠️
ntp-proto/src/nts_pool_ke.rs 0.00% 9 Missing ⚠️
ntpd/src/daemon/config/mod.rs 73.52% 9 Missing ⚠️
ntpd/src/force_sync/algorithm.rs 0.00% 9 Missing ⚠️
ntpd/src/metrics/exporter.rs 46.15% 7 Missing ⚠️
nts-pool-ke/src/condcompile/cli.rs 0.00% 6 Missing ⚠️
... and 22 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1679      +/-   ##
==========================================
+ Coverage   81.55%   81.60%   +0.05%     
==========================================
  Files          66       66              
  Lines       20690    20790     +100     
==========================================
+ Hits        16874    16966      +92     
- Misses       3816     3824       +8     

☔ 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 +23
/// # 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.

ntp-proto/src/nts_record.rs Outdated Show resolved Hide resolved
@@ -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

ntp-proto/src/packet/mod.rs Show resolved Hide resolved
ntpd/src/ctl.rs Show resolved Hide resolved
ntpd/src/daemon/ntp_source.rs Outdated Show resolved Hide resolved
@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?

@davidv1992
Copy link
Member

Yes, those are things that need attention before this can move forward.

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
One was for string conversion that can be ignored, but there's a lot of
casts that might introduce truncation and sign loss, which is now
explicitly allowed.
We might eventually need this
This can be rewritten, but this is not important for this issue.
I'm sure there's a prettier way to cast properly.
@van-sprundel
Copy link
Author

While refactoring I did found that test_deny_stops_poll will not always succeed (even on main). Seems like a timing issue. Could it be that the cast to u32 is causing this?

let cur =
std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH)?;
Ok(NtpTimestamp::from_seconds_nanos_since_ntp_era(
EPOCH_OFFSET.wrapping_add(cur.as_secs() as u32),
cur.subsec_nanos(),
))
}

or something in the test startup that's failing it? my initial changes made it always fail, but now it sometimes fails on main

cargo test test_deny_stops_poll --target x86_64-unknown-linux-gnu --features run_tokio_rustls_tests

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