From c4a50a2b8fad7821db82590011cc79c26e8ff23c Mon Sep 17 00:00:00 2001 From: CosmicHorror Date: Tue, 9 May 2023 23:22:22 -0600 Subject: [PATCH] Fix warnings and clippy lints (#185) * Fix `cargo check` warnings * Placate clippy * Invert conditional --- src/cli.rs | 2 +- src/input.rs | 3 +-- src/main.rs | 2 +- src/replacer.rs | 19 ++++++++----------- tests/cli.rs | 26 +++++++++++++------------- 5 files changed, 24 insertions(+), 28 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/input.rs b/src/input.rs index f526819..e5c715d 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..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.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 a95d983..f6d5d21 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) } } @@ -103,7 +100,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(), @@ -127,7 +124,7 @@ impl Replacer { use memmap2::{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(()); } @@ -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()