Skip to content

Commit

Permalink
Fix warnings and clippy lints (#185)
Browse files Browse the repository at this point in the history
* Fix `cargo check` warnings

* Placate clippy

* Invert conditional
  • Loading branch information
CosmicHorrorDev authored May 10, 2023
1 parent 9d1f97b commit c4a50a2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 8 additions & 11 deletions src/replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Replacer {
(
look_for,
utils::unescape(&replace_with)
.unwrap_or_else(|| replace_with)
.unwrap_or(replace_with)
.into_bytes(),
)
};
Expand All @@ -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);
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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(),
Expand All @@ -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(());
}

Expand Down Expand Up @@ -162,7 +159,7 @@ impl Replacer {
mod tests {
use super::*;

fn replace<'a>(
fn replace(
look_for: impl Into<String>,
replace_with: impl Into<String>,
literal: bool,
Expand Down
26 changes: 13 additions & 13 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ 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(())
}

#[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(())
}
Expand All @@ -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(())
Expand All @@ -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");
Expand All @@ -94,7 +94,7 @@ mod cli {

#[test]
fn stdin() -> Result<()> {
sd().args(&["abc\\d+", ""])
sd().args(["abc\\d+", ""])
.write_stdin("abc123def")
.assert()
.success()
Expand Down

0 comments on commit c4a50a2

Please sign in to comment.