From aefc4f4462ecd2a1de166db80865b80b120a4cfd Mon Sep 17 00:00:00 2001 From: Cosmic Horror Date: Tue, 9 May 2023 23:00:57 -0600 Subject: [PATCH 1/3] Fix `cargo check` warnings --- src/cli.rs | 2 +- src/replacer.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index ab092db..243d79d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -6,7 +6,7 @@ use structopt::{clap::AppSettings, StructOpt}; setting(AppSettings::NextLineHelp), setting(AppSettings::UnifiedHelpMessage) )] -pub(crate) struct Options { +pub struct Options { #[structopt(short = "p", long = "preview")] /// Output result into stdout and do not modify files. pub preview: bool, diff --git a/src/replacer.rs b/src/replacer.rs index fa3f6c2..f15b3d1 100644 --- a/src/replacer.rs +++ b/src/replacer.rs @@ -103,7 +103,7 @@ impl Replacer { self.regex.split(content).for_each(|sur_text| { use regex::bytes::Replacer; - &v.extend(sur_text); + v.extend(sur_text); if let Some(capture) = captures.next() { v.extend_from_slice( ansi_term::Color::Green.prefix().to_string().as_bytes(), From 68281650b738f290688ee47ee527153f2858096d Mon Sep 17 00:00:00 2001 From: Cosmic Horror Date: Tue, 9 May 2023 23:07:00 -0600 Subject: [PATCH 2/3] Placate clippy --- src/input.rs | 3 +-- src/main.rs | 2 +- src/replacer.rs | 19 ++++++++----------- tests/cli.rs | 26 +++++++++++++------------- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/input.rs b/src/input.rs index 9751a6d..27a872b 100644 --- a/src/input.rs +++ b/src/input.rs @@ -72,8 +72,7 @@ impl App { let print_path = paths.len() > 1; paths.iter().try_for_each(|path| { - if let Err(_) = Replacer::check_not_empty(File::open(path)?) - { + if Replacer::check_not_empty(File::open(path)?).is_err() { return Ok(()); } let file = diff --git a/src/main.rs b/src/main.rs index 436904a..a707bbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ fn main() -> Result<()> { let source = if options.recursive { Source::recursive()? - } else if options.files.len() > 0 { + } else if options.files.is_empty() { Source::Files(options.files) } else { Source::Stdin diff --git a/src/replacer.rs b/src/replacer.rs index f15b3d1..d4ae845 100644 --- a/src/replacer.rs +++ b/src/replacer.rs @@ -23,7 +23,7 @@ impl Replacer { ( look_for, utils::unescape(&replace_with) - .unwrap_or_else(|| replace_with) + .unwrap_or(replace_with) .into_bytes(), ) }; @@ -40,7 +40,7 @@ impl Replacer { 'm' => {}, 'e' => { regex.multi_line(false); }, 's' => { - if !flags.contains("m") { + if !flags.contains('m') { regex.multi_line(false); } regex.dot_matches_new_line(true); @@ -80,16 +80,13 @@ impl Replacer { ) -> std::borrow::Cow<'a, [u8]> { if self.is_literal { self.regex.replacen( - &content, + content, self.replacements, regex::bytes::NoExpand(&self.replace_with), ) } else { - self.regex.replacen( - &content, - self.replacements, - &*self.replace_with, - ) + self.regex + .replacen(content, self.replacements, &*self.replace_with) } } @@ -127,7 +124,7 @@ impl Replacer { use memmap::{Mmap, MmapMut}; use std::ops::DerefMut; - if let Err(_) = Self::check_not_empty(File::open(path)?) { + if Self::check_not_empty(File::open(path)?).is_err() { return Ok(()); } @@ -145,7 +142,7 @@ impl Replacer { file.set_permissions(meta.permissions())?; if !replaced.is_empty() { - let mut mmap_target = unsafe { MmapMut::map_mut(&file)? }; + let mut mmap_target = unsafe { MmapMut::map_mut(file)? }; mmap_target.deref_mut().write_all(&replaced)?; mmap_target.flush_async()?; } @@ -162,7 +159,7 @@ impl Replacer { mod tests { use super::*; - fn replace<'a>( + fn replace( look_for: impl Into, replace_with: impl Into, literal: bool, diff --git a/tests/cli.rs b/tests/cli.rs index 8efe355..29756b3 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -28,13 +28,13 @@ mod cli { #[test] fn in_place() -> Result<()> { let mut file = tempfile::NamedTempFile::new()?; - file.write(b"abc123def")?; + file.write_all(b"abc123def")?; let path = file.into_temp_path(); - sd().args(&["abc\\d+", "", path.to_str().unwrap()]) + sd().args(["abc\\d+", "", path.to_str().unwrap()]) .assert() .success(); - assert_file(&path.to_path_buf(), "def"); + assert_file(&path, "def"); Ok(()) } @@ -42,13 +42,13 @@ mod cli { #[test] fn in_place_with_empty_result_file() -> Result<()> { let mut file = tempfile::NamedTempFile::new()?; - file.write(b"a7c")?; + file.write_all(b"a7c")?; let path = file.into_temp_path(); - sd().args(&["a\\dc", "", path.to_str().unwrap()]) + sd().args(["a\\dc", "", path.to_str().unwrap()]) .assert() .success(); - assert_file(&path.to_path_buf(), ""); + assert_file(&path, ""); Ok(()) } @@ -63,11 +63,11 @@ mod cli { create_soft_link(&file, &link)?; std::fs::write(&file, "abc123def")?; - sd().args(&["abc\\d+", "", link.to_str().unwrap()]) + sd().args(["abc\\d+", "", link.to_str().unwrap()]) .assert() .success(); - assert_file(&file.to_path_buf(), "def"); + assert_file(&file, "def"); assert!(std::fs::symlink_metadata(link)?.file_type().is_symlink()); Ok(()) @@ -76,15 +76,15 @@ mod cli { #[test] fn replace_into_stdout() -> Result<()> { let mut file = tempfile::NamedTempFile::new()?; - file.write(b"abc123def")?; + file.write_all(b"abc123def")?; - sd().args(&["-p", "abc\\d+", "", file.path().to_str().unwrap()]) + sd().args(["-p", "abc\\d+", "", file.path().to_str().unwrap()]) .assert() .success() .stdout(format!( "{}{}def\n", - ansi_term::Color::Green.prefix().to_string(), - ansi_term::Color::Green.suffix().to_string() + ansi_term::Color::Green.prefix(), + ansi_term::Color::Green.suffix() )); assert_file(file.path(), "abc123def"); @@ -94,7 +94,7 @@ mod cli { #[test] fn stdin() -> Result<()> { - sd().args(&["abc\\d+", ""]) + sd().args(["abc\\d+", ""]) .write_stdin("abc123def") .assert() .success() From f9694f30eca70f79c5660d624e6230f0b0b24a40 Mon Sep 17 00:00:00 2001 From: Cosmic Horror Date: Tue, 9 May 2023 23:12:35 -0600 Subject: [PATCH 3/3] Invert conditional --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index a707bbe..3d6c1ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ fn main() -> Result<()> { let source = if options.recursive { Source::recursive()? - } else if options.files.is_empty() { + } else if !options.files.is_empty() { Source::Files(options.files) } else { Source::Stdin