Skip to content

Commit

Permalink
chore: fix a bunch of annoying clippy lints (#4558)
Browse files Browse the repository at this point in the history
## Motivation

Recent Clippy releases have added some new lints that trigger on some
code in Tokio. These aren't a big deal, but seeing them in my editor is
mildly annoying.

## Solution

This branch fixes the following issues flagged by Clippy:

* manual `Option::map` implementation
* use of `.map(...).flatten(...)` that could be replaced with
  `.and_then(...)`
* manual implementation of saturating arithmetic on `Duration`s
* simplify some boolean expressions in assertions (`!res.is_ok()` can be
`res.is_err()`)
* fix redundant field names in initializers
* replace an unnecessary cast to `usize` with an explicitly typed
  integer literal

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Mar 8, 2022
1 parent 2f944df commit dee26c9
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 14 deletions.
6 changes: 1 addition & 5 deletions tokio/src/io/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,7 @@ cfg_metrics! {
where
F: Fn(&IoDriverMetrics) -> R,
{
if let Some(inner) = self.inner() {
Some(f(&inner.metrics))
} else {
None
}
self.inner().map(|inner| f(&inner.metrics))
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions tokio/src/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ impl sealed::ToSocketAddrsPriv for &[SocketAddr] {
type Future = ReadyFuture<Self::Iter>;

fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future {
// Clippy doesn't like the `to_vec()` call here (as it will allocate,
// while `self.iter().copied()` would not), but it's actually necessary
// in order to ensure that the returned iterator is valid for the
// `'static` lifetime, which the borrowed `slice::Iter` iterator would
// not be.
// Note that we can't actually add an `allow` attribute for
// `clippy::unnecessary_to_owned` here, as Tokio's CI runs clippy lints
// on Rust 1.52 to avoid breaking LTS releases of Tokio. Users of newer
// Rust versions who see this lint should just ignore it.
let iter = self.to_vec().into_iter();
future::ready(Ok(iter))
}
Expand Down
3 changes: 1 addition & 2 deletions tokio/src/runtime/metrics/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,7 @@ cfg_net! {
self.handle
.io_handle
.as_ref()
.map(|h| h.with_io_driver_metrics(f))
.flatten()
.and_then(|h| h.with_io_driver_metrics(f))
.unwrap_or(0)
}
}
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/sync/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Barrier {
n,
wait,
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: resource_span,
resource_span,
}
}

Expand Down
2 changes: 1 addition & 1 deletion tokio/src/sync/batch_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl<'a> Acquire<'a> {

tracing::trace!(
target: "runtime::resource::async_op::state_update",
permits_obtained = 0 as usize,
permits_obtained = 0usize,
permits.op = "override",
);

Expand Down
2 changes: 1 addition & 1 deletion tokio/src/sync/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
let rx = Receiver {
inner: Some(inner),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: resource_span,
resource_span,
#[cfg(all(tokio_unstable, feature = "tracing"))]
async_op_span,
#[cfg(all(tokio_unstable, feature = "tracing"))]
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/time/driver/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Sleep {
let inner = {
let time_source = handle.time_source().clone();
let deadline_tick = time_source.deadline_to_tick(deadline);
let duration = deadline_tick.checked_sub(time_source.now()).unwrap_or(0);
let duration = deadline_tick.saturating_sub(time_source.now());

let location = location.expect("should have location if tracing");
let resource_span = tracing::trace_span!(
Expand Down Expand Up @@ -373,7 +373,7 @@ impl Sleep {
let duration = {
let now = me.inner.time_source.now();
let deadline_tick = me.inner.time_source.deadline_to_tick(deadline);
deadline_tick.checked_sub(now).unwrap_or(0)
deadline_tick.saturating_sub(now)
};

tracing::trace!(
Expand Down
2 changes: 1 addition & 1 deletion tokio/tests/sync_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn try_lock() {
let g1 = m.try_lock();
assert!(g1.is_ok());
let g2 = m.try_lock();
assert!(!g2.is_ok());
assert!(g2.is_err());
}
let g3 = m.try_lock();
assert!(g3.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion tokio/tests/sync_mutex_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn try_lock_owned() {
let g1 = m.clone().try_lock_owned();
assert!(g1.is_ok());
let g2 = m.clone().try_lock_owned();
assert!(!g2.is_ok());
assert!(g2.is_err());
}
let g3 = m.try_lock_owned();
assert!(g3.is_ok());
Expand Down

0 comments on commit dee26c9

Please sign in to comment.