From 4f4330167a875fdba7775542dc2898855b558668 Mon Sep 17 00:00:00 2001 From: Alexandru Macovei Date: Fri, 1 May 2020 12:22:30 +0300 Subject: [PATCH] Fix some clippy lints, and format --- src/exec/input.rs | 6 +++--- src/exec/mod.rs | 2 +- src/main.rs | 21 ++++++++++++--------- src/output.rs | 2 +- tests/tests.rs | 19 +++++-------------- 5 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/exec/input.rs b/src/exec/input.rs index 1026c17..d803eae 100644 --- a/src/exec/input.rs +++ b/src/exec/input.rs @@ -5,13 +5,13 @@ use crate::filesystem::strip_current_dir; /// Removes the parent component of the path pub fn basename(path: &Path) -> &OsStr { - path.file_name().unwrap_or(path.as_os_str()) + path.file_name().unwrap_or_else(|| path.as_os_str()) } /// Removes the extension from the path pub fn remove_extension(path: &Path) -> OsString { let dirname = dirname(path); - let stem = path.file_stem().unwrap_or(path.as_os_str()); + let stem = path.file_stem().unwrap_or_else(|| path.as_os_str()); let path = PathBuf::from(dirname).join(stem); @@ -28,7 +28,7 @@ pub fn dirname(path: &Path) -> OsString { p.as_os_str().to_owned() } }) - .unwrap_or(path.as_os_str().to_owned()) + .unwrap_or_else(|| path.as_os_str().to_owned()) } #[cfg(test)] diff --git a/src/exec/mod.rs b/src/exec/mod.rs index 5da8807..129498a 100644 --- a/src/exec/mod.rs +++ b/src/exec/mod.rs @@ -201,7 +201,7 @@ impl ArgumentTemplate { } } - pub fn generate<'a>(&'a self, path: impl AsRef) -> OsString { + pub fn generate(&self, path: impl AsRef) -> OsString { use self::Token::*; match *self { diff --git a/src/main.rs b/src/main.rs index 4909743..377a698 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,9 +65,8 @@ fn run() -> Result { let pattern = matches .value_of_os("pattern") .map(|p| { - p.to_str().ok_or(anyhow!( - "The search pattern includes invalid UTF-8 sequences." - )) + p.to_str() + .ok_or_else(|| anyhow!("The search pattern includes invalid UTF-8 sequences.")) }) .transpose()? .unwrap_or(""); @@ -122,7 +121,7 @@ fn run() -> Result { )); } - let pattern_regex = if matches.is_present("glob") && pattern.len() > 0 { + let pattern_regex = if matches.is_present("glob") && !pattern.is_empty() { let glob = GlobBuilder::new(pattern).literal_separator(true).build()?; glob.regex().to_owned() } else if matches.is_present("fixed-strings") { @@ -175,7 +174,13 @@ fn run() -> Result { }; let cmd: Vec<&str> = if cfg!(unix) { - if !cfg!(any(target_os = "macos", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")) { + if !cfg!(any( + target_os = "macos", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + )) { // Assume ls is GNU ls gnu_ls("ls") } else { @@ -239,10 +244,8 @@ fn run() -> Result { let size_limits = if let Some(vs) = matches.values_of("size") { vs.map(|sf| { - SizeFilter::from_string(sf).ok_or(anyhow!( - "'{}' is not a valid size constraint. See 'fd --help'.", - sf - )) + SizeFilter::from_string(sf) + .ok_or_else(|| anyhow!("'{}' is not a valid size constraint. See 'fd --help'.", sf)) }) .collect::>>()? } else { diff --git a/src/output.rs b/src/output.rs index 175b1dd..d2737c6 100644 --- a/src/output.rs +++ b/src/output.rs @@ -11,7 +11,7 @@ use crate::exit_codes::ExitCode; use crate::filesystem::strip_current_dir; use crate::options::Options; -pub fn replace_path_separator<'a>(path: &str, new_path_separator: &str) -> String { +pub fn replace_path_separator(path: &str, new_path_separator: &str) -> String { path.replace(std::path::MAIN_SEPARATOR, &new_path_separator) } diff --git a/tests/tests.rs b/tests/tests.rs index d541304..8dedd61 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -9,9 +9,9 @@ use regex::escape; use crate::testenv::TestEnv; -static DEFAULT_DIRS: &'static [&'static str] = &["one/two/three", "one/two/three/directory_foo"]; +static DEFAULT_DIRS: &[&str] = &["one/two/three", "one/two/three/directory_foo"]; -static DEFAULT_FILES: &'static [&'static str] = &[ +static DEFAULT_FILES: &[&str] = &[ "a.foo", "one/b.foo", "one/two/c.foo", @@ -88,18 +88,9 @@ fn test_empty_pattern() { one/two/three/directory_foo symlink"; - te.assert_output( - &["--regex"], - expected, - ); - te.assert_output( - &["--fixed-strings"], - expected, - ); - te.assert_output( - &["--glob"], - expected, - ); + te.assert_output(&["--regex"], expected); + te.assert_output(&["--fixed-strings"], expected); + te.assert_output(&["--glob"], expected); } /// Test multiple directory searches