diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/date.rs | 1 | ||||
| -rw-r--r-- | src/defer.rs | 2 | ||||
| -rw-r--r-- | src/export_fun.rs | 32 | ||||
| -rw-r--r-- | src/history.rs | 7 | ||||
| -rw-r--r-- | src/linebuf.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 3 | ||||
| -rw-r--r-- | src/parse/mod.rs | 26 | ||||
| -rw-r--r-- | src/raw.rs | 2 | ||||
| -rw-r--r-- | src/reload.rs | 1 | ||||
| -rw-r--r-- | src/run/builtin.rs | 22 | ||||
| -rw-r--r-- | src/run/mod.rs | 23 |
11 files changed, 58 insertions, 67 deletions
diff --git a/src/date.rs b/src/date.rs index 8083da8..1170702 100644 --- a/src/date.rs +++ b/src/date.rs @@ -49,6 +49,7 @@ impl DateTime { .as_secs() } + #[allow(clippy::blocks_in_conditions)] pub fn relative_to(&self, other: &Self) -> String { let a = self.unix(); let b = other.unix(); diff --git a/src/defer.rs b/src/defer.rs index 362c7d1..4f23ed1 100644 --- a/src/defer.rs +++ b/src/defer.rs @@ -11,6 +11,6 @@ impl<T: FnOnce()> Drop for Defer<T> { #[macro_export] macro_rules! defer { ($($x:tt)*) => { - let _defer = crate::defer::Defer(Some(move || {$($x)*})); + let _defer = $crate::defer::Defer(Some(move || {$($x)*})); }; } diff --git a/src/export_fun.rs b/src/export_fun.rs index ff1feeb..26ab88c 100644 --- a/src/export_fun.rs +++ b/src/export_fun.rs @@ -49,14 +49,9 @@ fn handle_server(session: Arc<Mutex<Session>>, mut stream: UnixStream) -> io::Re let bytelen = stream.recv_vectored_with_ancillary(&mut iov, &mut ancillary)?; for msg in ancillary.messages() { - if let Ok(msg) = msg { - match msg { - AncillaryData::ScmRights(rights) => { - for fd in rights { - fds.push(fd); - } - } - _ => (), + if let Ok(AncillaryData::ScmRights(rights)) = msg { + for fd in rights { + fds.push(fd); } } } @@ -155,13 +150,14 @@ pub fn prepare_command(session: Arc<Mutex<Session>>, cmd: &mut Command) { pub fn maybe_run_defined_function() { if let Some(program_name) = std::env::args_os().next() { let program_name = program_name.as_bytes(); - if !program_name.contains(&b'/') && program_name != b"pish" { - if let Some(socket) = std::env::var_os("PISH_SOCKET") { - if let Ok(stream) = UnixStream::connect(socket) { - let _ = handle_client(stream); - } - exit(-1); + if !program_name.contains(&b'/') + && program_name != b"pish" + && let Some(socket) = std::env::var_os("PISH_SOCKET") + { + if let Ok(stream) = UnixStream::connect(socket) { + let _ = handle_client(stream); } + exit(-1); } } } @@ -267,11 +263,9 @@ pub fn listen(session: Arc<Mutex<Session>>) -> impl Drop { _ => (), } - if is_ready { - if let Ok((stream, _addr)) = listener.accept() { - let se = se.clone(); - thread::spawn(move || handle_server(se, stream)); - } + if is_ready && let Ok((stream, _addr)) = listener.accept() { + let se = se.clone(); + thread::spawn(move || handle_server(se, stream)); } } }); diff --git a/src/history.rs b/src/history.rs index 8d7ebf4..dbf2584 100644 --- a/src/history.rs +++ b/src/history.rs @@ -3,7 +3,6 @@ use sqlite::{Connection, State}; use crate::BString; use crate::date::DateTime; use std::env::current_dir; -use std::i64; use std::path::PathBuf; fn db_file() -> PathBuf { @@ -209,10 +208,8 @@ pub fn local_history_filter( if path_strict { let canon_path = canonical_path(entry.loc.clone()); return &canon_path == path_prefix; - } else { - if !entry.loc.starts_with(&path_prefix) { - return false; - } + } else if !entry.loc.starts_with(path_prefix) { + return false; } } diff --git a/src/linebuf.rs b/src/linebuf.rs index 6ba619a..548ec75 100644 --- a/src/linebuf.rs +++ b/src/linebuf.rs @@ -7,6 +7,12 @@ pub struct LineBuf { dirty: bool, } +impl Default for LineBuf { + fn default() -> Self { + Self::new() + } +} + #[allow(unused)] impl LineBuf { pub fn new() -> Self { diff --git a/src/main.rs b/src/main.rs index 6b64f79..1a481c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ #![feature(unix_socket_ancillary_data)] +#![allow(clippy::needless_range_loop)] use std::collections::HashMap; use std::ffi::OsStr; @@ -429,7 +430,7 @@ fn event_loop() { // still more while { let last = seq[seq.len() - 1]; - last < 0x40 || last > 0x7E || seq.len() == 1 + !(0x40..=0x7E).contains(&last) || seq.len() == 1 } { seq.push(read1()); } diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 4250caf..0df624f 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -41,7 +41,7 @@ pub enum Ast<T: Stage> { pub fn decl(name: ExpString, body: Ast<PreExpansion>) -> Ast<PreExpansion> { Ast::FunDecl(FunDecl { - name: name, + name, body: FunBody { body: Box::new(body), }, @@ -411,10 +411,10 @@ impl ExpString { } fn is_symbol(x: u8) -> bool { - match x { - b';' | b'|' | b'{' | b'}' | b'$' | b'(' | b')' | b'\'' | b'"' => true, - _ => false, - } + matches!( + x, + b';' | b'|' | b'{' | b'}' | b'$' | b'(' | b')' | b'\'' | b'"' + ) } fn is_var_begin(x: u8) -> bool { @@ -618,10 +618,8 @@ impl Parse for ExpString { let cmd = Ast::parse(b)?; b.spaces(); - if b.is_empty() { - if !b.is_completion() { - return Err(ParseError::Expected(')')); - } + if b.is_empty() && !b.is_completion() { + return Err(ParseError::Expected(')')); } if b.has() && b.peek() == b')' { @@ -799,7 +797,7 @@ impl Pipes<PreExpansion> { } } -pub fn completion_context<'a, E: Expander>(x: &'a [u8], e: &mut E) -> CompletionContext { +pub fn completion_context<E: Expander>(x: &[u8], e: &mut E) -> CompletionContext { let mut cursor = Cursor::new(x, ParseMode::Completion); let ast = Ast::parse(&mut cursor); @@ -900,10 +898,7 @@ impl<'a> Cursor<'a> { } fn is_completion(&self) -> bool { - match self.mode { - ParseMode::Completion => true, - _ => false, - } + matches!(self.mode, ParseMode::Completion) } fn parse<T: Parse>(&mut self) -> Result<T> { @@ -946,8 +941,7 @@ impl Parse for Command<PreExpansion> { Err(e) => Err(e)?, } } - let x = Ok(Self { cmd: path, args }); - x + Ok(Self { cmd: path, args }) } } @@ -21,7 +21,7 @@ impl ScopedRawMode { } pub fn enable(&self) { - let mut settings = self.settings.clone(); + let mut settings = self.settings; cfmakeraw(&mut settings); tcsetattr(self.fd, TCSANOW, &settings).unwrap(); panic::enable_cr(); diff --git a/src/reload.rs b/src/reload.rs index 1d1aa3e..b7e7345 100644 --- a/src/reload.rs +++ b/src/reload.rs @@ -46,6 +46,7 @@ pub fn begin_reload() { panic::resume_unwind(Box::new(42)); } +/// # Safety /// ONLY TO BE CALLED FROM MAIN WHEN NOT A SINGLE RESOURCE IS HELD pub unsafe fn continue_reload() { if !RELOAD.load(Ordering::SeqCst) { diff --git a/src/run/builtin.rs b/src/run/builtin.rs index 24badd0..99e0103 100644 --- a/src/run/builtin.rs +++ b/src/run/builtin.rs @@ -34,13 +34,13 @@ fn read_args<T: ArgParse>(args: &[BString], w: &mut dyn Write) -> std::result::R w.write_all(b"\n")?; } ArgParseError::MissingArg(arg) => { - write!(w, "argument `{arg}` is missing\n")?; + writeln!(w, "argument `{arg}` is missing")?; } ArgParseError::MissingArgValue(arg) => { - write!(w, "argument `{arg}` is missing its value\n")?; + writeln!(w, "argument `{arg}` is missing its value")?; } ArgParseError::ArgValueParseError(arg, err) => { - write!(w, "failed to parse value of `{arg}`: {err}")?; + writeln!(w, "failed to parse value of `{arg}`: {err}")?; } } @@ -66,7 +66,7 @@ impl Builtin for cd { std::mem::swap(&mut dir, &mut se.lock().unwrap().prev_path); - let target_path: BString = match args.get(0).map(|v| &v[..]) { + let target_path: BString = match args.first().map(|v| &v[..]) { Some(b"-") => dir, Some(path) => path.to_vec(), None => { @@ -79,7 +79,7 @@ impl Builtin for cd { } }; - if let Err(_) = set_current_dir(OsStr::from_bytes(&target_path)) { + if set_current_dir(OsStr::from_bytes(&target_path)).is_err() { write!(stdout, "failed to cd into ")?; stdout.write_all(&target_path)?; writeln!(stdout, "\n")?; @@ -149,7 +149,7 @@ impl Builtin for Sink { stdin: &mut dyn Read, _stdout: &mut dyn Write, ) -> Result { - let Some(path) = args.get(0) else { + let Some(path) = args.first() else { return Err(Error::Exit(1)); }; let path = PathBuf::from(OsStr::from_bytes(path)); @@ -180,7 +180,7 @@ impl Builtin for from { _stdin: &mut dyn Read, stdout: &mut dyn Write, ) -> Result { - let Some(path) = args.get(0) else { + let Some(path) = args.first() else { return Err(Error::Exit(1)); }; let path = PathBuf::from(OsStr::from_bytes(path)); @@ -289,10 +289,8 @@ impl Builtin for history { let path_prefix = if args.here { Some(current_dir()?.as_os_str().as_bytes().to_vec()) - } else if let Some(path) = args.at { - Some(path.as_os_str().as_bytes().to_vec()) } else { - None + args.at.map(|path| path.as_os_str().as_bytes().to_vec()) }; let min_time = None; @@ -312,12 +310,12 @@ impl Builtin for history { ) } else { let Ok(hist) = crate::history::HistoryQueryer::new() else { - write!(stdout, "error opening global history file\n")?; + writeln!(stdout, "error opening global history file")?; return Err(Error::Exit(-1)); }; let Ok(it) = hist.query(min_time, max_time, path_prefix.as_deref(), args.strict) else { - write!(stdout, "error querying global history\n")?; + writeln!(stdout, "error querying global history")?; return Err(Error::Exit(-1)); }; diff --git a/src/run/mod.rs b/src/run/mod.rs index 6234b96..5666574 100644 --- a/src/run/mod.rs +++ b/src/run/mod.rs @@ -25,7 +25,7 @@ impl ExecError { pub fn error_message(&self) -> String { match self { ExecError::UnknownVariable(items) => { - format!("unknown variable: {}", String::from_utf8_lossy(&items)) + format!("unknown variable: {}", String::from_utf8_lossy(items)) } ExecError::ExecError(exit_code) => format!("{exit_code}"), ExecError::SpawnIO(cmd, error) => match error.kind() { @@ -43,8 +43,8 @@ impl ExecError { io::ErrorKind::InvalidFilename => { format!("{cmd} is not a valid file name") } - io::ErrorKind::ArgumentListTooLong => format!("too many arguments"), - io::ErrorKind::Interrupted => format!("got interrupted"), + io::ErrorKind::ArgumentListTooLong => String::from("too many arguments"), + io::ErrorKind::Interrupted => String::from("got interrupted"), io::ErrorKind::Unsupported => format!("{cmd} is not supported"), e => format!("I am surprised you can get this error here: {e:?}"), }, @@ -335,18 +335,17 @@ impl parse::Expander for Executor { return Err(ExecError::UnknownVariable(var)); } - if var[0].is_ascii_digit() { - if let Some(x) = String::from_utf8(var.clone()) + if var[0].is_ascii_digit() + && let Some(x) = String::from_utf8(var.clone()) .ok() .and_then(|x| x.parse::<usize>().ok()) - { - if let Some(args) = &self.args { - if x < args.len() { - return Ok(args[x].clone()); - } - } else if let Some(arg) = std::env::args_os().skip(x).next() { - return Ok(arg.into_encoded_bytes()); + { + if let Some(args) = &self.args { + if x < args.len() { + return Ok(args[x].clone()); } + } else if let Some(arg) = std::env::args_os().nth(x) { + return Ok(arg.into_encoded_bytes()); } } |
