Fix some clippy lints, and format

This commit is contained in:
Alexandru Macovei 2020-05-01 12:22:30 +03:00 committed by David Peter
parent f31fb9bf68
commit 4f4330167a
5 changed files with 22 additions and 28 deletions

View file

@ -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)]

View file

@ -201,7 +201,7 @@ impl ArgumentTemplate {
}
}
pub fn generate<'a>(&'a self, path: impl AsRef<Path>) -> OsString {
pub fn generate(&self, path: impl AsRef<Path>) -> OsString {
use self::Token::*;
match *self {

View file

@ -65,9 +65,8 @@ fn run() -> Result<ExitCode> {
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<ExitCode> {
));
}
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<ExitCode> {
};
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<ExitCode> {
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::<Result<Vec<_>>>()?
} else {

View file

@ -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)
}

View file

@ -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