-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
base: main
Are you sure you want to change the base?
Pedantic linting #1679
Conversation
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
Codecov ReportAttention: Patch coverage is
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. |
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.
Box is not an iter
b117a79
to
36e52e4
Compare
I'm sure there's a prettier way to cast properly.
Breaking for msrv integration
There was a problem hiding this 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.
/// # Panics | ||
/// | ||
/// Will panic if `addr` is not a valid ip address. | ||
#[must_use] |
There was a problem hiding this comment.
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).
#[allow(clippy::cast_possible_truncation)] | ||
#[allow(clippy::cast_sign_loss)] |
There was a problem hiding this comment.
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.
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), |
There was a problem hiding this comment.
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.
/// # Panics | ||
/// | ||
/// Panics if `addr` has invalid octets. | ||
fn is_in4(&self, addr: Ipv4Addr) -> bool { |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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.
/// # Errors | ||
/// | ||
/// Returns error if getting the TLS bool `wants_write` fails. |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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
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 { |
There was a problem hiding this comment.
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?
|
||
/// 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; | ||
} | ||
} |
There was a problem hiding this comment.
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.
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" |
There was a problem hiding this comment.
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.
@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. |
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? |
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:
#[allow]
ntpd::ctl::NtpCtlOptions
now uses theaction
state instead of bools.match
cases becameif () { } else { }
where relevant.let foo = if ... else
becamelet ... else
where relevant.async
andResult<_>
are removed on places where it wasn't used and relevant.const
andenum
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.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.