Clippy nursery (#681)

This commit is contained in:
Félix Saparelli 2023-11-26 15:40:57 +13:00 committed by GitHub
parent a13bc429eb
commit 89e3d60ecf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 43 additions and 40 deletions

View File

@ -881,11 +881,12 @@ impl<const UNITLESS_NANOS_MULTIPLIER: u64> FromStr for TimeSpan<UNITLESS_NANOS_M
type Err = humantime::DurationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.parse::<u64>() {
Ok(unitless) => Ok(Duration::from_nanos(unitless * UNITLESS_NANOS_MULTIPLIER)),
Err(_) => humantime::parse_duration(s),
}
.map(TimeSpan)
s.parse::<u64>()
.map_or_else(
|_| humantime::parse_duration(s),
|unitless| Ok(Duration::from_nanos(unitless * UNITLESS_NANOS_MULTIPLIER)),
)
.map(TimeSpan)
}
}

View File

@ -29,16 +29,13 @@ impl RotatingTempFile {
}
pub fn rotate(&self) -> Result<()> {
let mut file = self.0.lock().unwrap();
*file = NamedTempFile::new().into_diagnostic()?;
// implicitly drops the old file
*self.0.lock().unwrap() = NamedTempFile::new().into_diagnostic()?;
Ok(())
}
pub fn write(&self, data: &[u8]) -> Result<()> {
let mut file = self.0.lock().unwrap();
file.write_all(data).into_diagnostic()?;
Ok(())
self.0.lock().unwrap().write_all(data).into_diagnostic()
}
pub fn path(&self) -> PathBuf {

View File

@ -172,6 +172,7 @@ impl From<Tag> for SerdeTag {
}
}
#[allow(clippy::fallible_impl_from)] // due to the unwraps
impl From<SerdeTag> for Tag {
fn from(value: SerdeTag) -> Self {
match value {
@ -314,6 +315,7 @@ impl From<SerdeTag> for Tag {
..
} if code != 0 && i32::try_from(code).is_ok() => {
Self::ProcessCompletion(Some(ProcessEnd::ExitStop(unsafe {
// SAFETY&UNWRAP: checked above
NonZeroI32::new_unchecked(code.try_into().unwrap())
})))
}
@ -324,6 +326,7 @@ impl From<SerdeTag> for Tag {
..
} if exc != 0 && i32::try_from(exc).is_ok() => {
Self::ProcessCompletion(Some(ProcessEnd::Exception(unsafe {
// SAFETY&UNWRAP: checked above
NonZeroI32::new_unchecked(exc.try_into().unwrap())
})))
}

View File

@ -319,7 +319,7 @@ impl TaggedFilterer {
fn match_tag(&self, filter: &Filter, tag: &Tag) -> Result<Option<bool>, TaggedFiltererError> {
trace!(matcher=?filter.on, "matching filter to tag");
fn sig_match(sig: Signal) -> (&'static str, i32) {
const fn sig_match(sig: Signal) -> (&'static str, i32) {
match sig {
Signal::Hangup | Signal::Custom(1) => ("HUP", 1),
Signal::ForceStop | Signal::Custom(9) => ("KILL", 9),

View File

@ -378,14 +378,14 @@ mod serde_support {
impl From<SerdeSignal> for Signal {
fn from(signal: SerdeSignal) -> Self {
match signal {
SerdeSignal::Named(NamedSignal::Hangup) => Signal::Hangup,
SerdeSignal::Named(NamedSignal::ForceStop) => Signal::ForceStop,
SerdeSignal::Named(NamedSignal::Interrupt) => Signal::Interrupt,
SerdeSignal::Named(NamedSignal::Quit) => Signal::Quit,
SerdeSignal::Named(NamedSignal::Terminate) => Signal::Terminate,
SerdeSignal::Named(NamedSignal::User1) => Signal::User1,
SerdeSignal::Named(NamedSignal::User2) => Signal::User2,
SerdeSignal::Number(number) => Signal::Custom(number),
SerdeSignal::Named(NamedSignal::Hangup) => Self::Hangup,
SerdeSignal::Named(NamedSignal::ForceStop) => Self::ForceStop,
SerdeSignal::Named(NamedSignal::Interrupt) => Self::Interrupt,
SerdeSignal::Named(NamedSignal::Quit) => Self::Quit,
SerdeSignal::Named(NamedSignal::Terminate) => Self::Terminate,
SerdeSignal::Named(NamedSignal::User1) => Self::User1,
SerdeSignal::Named(NamedSignal::User2) => Self::User2,
SerdeSignal::Number(number) => Self::Custom(number),
}
}
}

View File

@ -112,7 +112,7 @@ impl std::fmt::Debug for Control {
}
#[derive(Debug)]
pub(crate) struct ControlMessage {
pub struct ControlMessage {
pub control: Control,
pub done: Flag,
}

View File

@ -11,21 +11,21 @@ use crate::flag::Flag;
use super::{messages::ControlMessage, Control};
#[derive(Debug, Copy, Clone)]
pub(crate) enum Priority {
pub enum Priority {
Normal,
High,
Urgent,
}
#[derive(Debug)]
pub(crate) struct PriorityReceiver {
pub struct PriorityReceiver {
pub normal: UnboundedReceiver<ControlMessage>,
pub high: UnboundedReceiver<ControlMessage>,
pub urgent: UnboundedReceiver<ControlMessage>,
}
#[derive(Debug, Clone)]
pub(crate) struct PrioritySender {
pub struct PrioritySender {
pub normal: UnboundedSender<ControlMessage>,
pub high: UnboundedSender<ControlMessage>,
pub urgent: UnboundedSender<ControlMessage>,
@ -82,7 +82,7 @@ impl PriorityReceiver {
}
}
pub(crate) fn new() -> (PrioritySender, PriorityReceiver) {
pub fn new() -> (PrioritySender, PriorityReceiver) {
let (normal_tx, normal_rx) = unbounded_channel();
let (high_tx, high_rx) = unbounded_channel();
let (urgent_tx, urgent_rx) = unbounded_channel();
@ -102,7 +102,7 @@ pub(crate) fn new() -> (PrioritySender, PriorityReceiver) {
}
#[derive(Debug, Clone)]
pub(crate) struct Timer {
pub struct Timer {
pub until: Instant,
pub done: Flag,
pub is_restart: bool,

View File

@ -53,17 +53,17 @@ pub enum CommandState {
impl CommandState {
/// Whether the command is pending, i.e. not running or finished.
pub fn is_pending(&self) -> bool {
pub const fn is_pending(&self) -> bool {
matches!(self, Self::Pending)
}
/// Whether the command is running.
pub fn is_running(&self) -> bool {
pub const fn is_running(&self) -> bool {
matches!(self, Self::Running { .. })
}
/// Whether the command is finished.
pub fn is_finished(&self) -> bool {
pub const fn is_finished(&self) -> bool {
matches!(self, Self::Finished { .. })
}

View File

@ -1,4 +1,4 @@
use std::{future::Future, sync::Arc, time::Instant};
use std::{future::Future, mem::take, sync::Arc, time::Instant};
use tokio::{process::Command as TokioCommand, select, task::JoinHandle};
use watchexec_signals::Signal;
@ -20,6 +20,7 @@ use super::{
/// Spawn a job task and return a [`Job`] handle and a [`JoinHandle`].
///
/// The job task immediately starts in the background: it does not need polling.
#[must_use]
pub fn start_job(command: Arc<Command>) -> (Job, JoinHandle<()>) {
let (sender, mut receiver) = priority::new();
@ -54,7 +55,7 @@ pub fn start_job(command: Arc<Command>) -> (Job, JoinHandle<()>) {
}
Ok(true) => {
stop_timer = None;
for done in on_end.drain(..) {
for done in take(&mut on_end).into_iter() {
done.raise();
}
@ -126,7 +127,7 @@ pub fn start_job(command: Arc<Command>) -> (Job, JoinHandle<()>) {
finished: Instant::now(),
};
for done in on_end.drain(..) {
for done in take(&mut on_end).into_iter() {
done.raise();
}
}
@ -151,7 +152,7 @@ pub fn start_job(command: Arc<Command>) -> (Job, JoinHandle<()>) {
};
previous_run = Some(command_state.reset());
for done in on_end.drain(..) {
for done in take(&mut on_end).into_iter() {
done.raise();
}
@ -189,7 +190,7 @@ pub fn start_job(command: Arc<Command>) -> (Job, JoinHandle<()>) {
finished: Instant::now(),
};
for done in on_end.drain(..) {
for done in take(&mut on_end).into_iter() {
done.raise();
}
}
@ -273,7 +274,7 @@ pub fn start_job(command: Arc<Command>) -> (Job, JoinHandle<()>) {
macro_rules! sync_async_callbox {
($name:ident, $synct:ty, $asynct:ty, ($($argname:ident : $argtype:ty),*)) => {
pub(crate) enum $name {
pub enum $name {
None,
Sync($synct),
Async($asynct),
@ -306,17 +307,17 @@ pub struct JobTaskContext<'task> {
pub previous: Option<&'task CommandState>,
}
pub(crate) type SyncFunc = Box<dyn FnOnce(&JobTaskContext<'_>) + Send + Sync + 'static>;
pub(crate) type AsyncFunc = Box<
pub type SyncFunc = Box<dyn FnOnce(&JobTaskContext<'_>) + Send + Sync + 'static>;
pub type AsyncFunc = Box<
dyn (FnOnce(&JobTaskContext<'_>) -> Box<dyn Future<Output = ()> + Send + Sync>)
+ Send
+ Sync
+ 'static,
>;
pub(crate) type SyncSpawnHook =
pub type SyncSpawnHook =
Arc<dyn Fn(&mut TokioCommand, &JobTaskContext<'_>) + Send + Sync + 'static>;
pub(crate) type AsyncSpawnHook = Arc<
pub type AsyncSpawnHook = Arc<
dyn (Fn(&mut TokioCommand, &JobTaskContext<'_>) -> Box<dyn Future<Output = ()> + Send + Sync>)
+ Send
+ Sync
@ -325,13 +326,14 @@ pub(crate) type AsyncSpawnHook = Arc<
sync_async_callbox!(SpawnHook, SyncSpawnHook, AsyncSpawnHook, (command: &mut TokioCommand, context: &JobTaskContext<'_>));
pub(crate) type SyncErrorHandler = Arc<dyn Fn(SyncIoError) + Send + Sync + 'static>;
pub(crate) type AsyncErrorHandler = Arc<
pub type SyncErrorHandler = Arc<dyn Fn(SyncIoError) + Send + Sync + 'static>;
pub type AsyncErrorHandler = Arc<
dyn (Fn(SyncIoError) -> Box<dyn Future<Output = ()> + Send + Sync>) + Send + Sync + 'static,
>;
sync_async_callbox!(ErrorHandler, SyncErrorHandler, AsyncErrorHandler, (error: SyncIoError));
#[cfg_attr(not(windows), allow(clippy::needless_pass_by_ref_mut))] // needed for start_kill()
async fn signal_child(
signal: Signal,
#[cfg(test)] child: &mut super::TestChild,