From b4fbbe50c2fd96939be444f6397e44572e4262a3 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 3 Apr 2024 14:57:18 +0200 Subject: [PATCH] Run clippy pedantic fixes Done with: $ cargo +nightly clippy --tests --fix --allow-dirty -- -W clippy::pedantic --- src/find/matchers/delete.rs | 2 +- src/find/matchers/exec.rs | 2 +- src/find/matchers/glob.rs | 2 +- src/find/matchers/lname.rs | 3 +-- src/find/matchers/logical_matchers.rs | 12 +++++++++--- src/find/matchers/mod.rs | 16 +++++++++------- src/find/matchers/name.rs | 3 +-- src/find/matchers/perm.rs | 2 +- src/find/matchers/printf.rs | 27 ++++++++++----------------- src/find/matchers/regex.rs | 2 +- src/find/matchers/size.rs | 3 +-- src/find/matchers/time.rs | 8 +++----- src/find/matchers/type_matcher.rs | 6 ++---- src/find/mod.rs | 6 +++--- src/xargs/mod.rs | 20 ++++++++++---------- tests/common/test_helpers.rs | 2 +- tests/find_cmd_tests.rs | 15 +++++---------- tests/xargs_tests.rs | 20 ++++++++++---------- 18 files changed, 70 insertions(+), 81 deletions(-) diff --git a/src/find/matchers/delete.rs b/src/find/matchers/delete.rs index 7869215..cd9ba7b 100644 --- a/src/find/matchers/delete.rs +++ b/src/find/matchers/delete.rs @@ -44,7 +44,7 @@ impl Matcher for DeleteMatcher { } match self.delete(path, file_info.file_type()) { - Ok(_) => true, + Ok(()) => true, Err(e) => { writeln!(&mut stderr(), "Failed to delete {path_str}: {e}").unwrap(); false diff --git a/src/find/matchers/exec.rs b/src/find/matchers/exec.rs index d0e4311..9f14fe4 100644 --- a/src/find/matchers/exec.rs +++ b/src/find/matchers/exec.rs @@ -100,5 +100,5 @@ impl Matcher for SingleExecMatcher { #[cfg(test)] /// No tests here, because we need to call out to an external executable. See -/// tests/exec_unit_tests.rs instead. +/// `tests/exec_unit_tests.rs` instead. mod tests {} diff --git a/src/find/matchers/glob.rs b/src/find/matchers/glob.rs index bfecc19..601a679 100644 --- a/src/find/matchers/glob.rs +++ b/src/find/matchers/glob.rs @@ -201,7 +201,7 @@ mod tests { #[test] fn incomplete_escape() { - assert_eq!(glob_to_regex(r"foo\"), r"$.") + assert_eq!(glob_to_regex(r"foo\"), r"$."); } #[test] diff --git a/src/find/matchers/lname.rs b/src/find/matchers/lname.rs index abf007c..9519239 100644 --- a/src/find/matchers/lname.rs +++ b/src/find/matchers/lname.rs @@ -74,8 +74,7 @@ mod tests { if let Err(e) = symlink("abbbc", "test_data/links/link-f") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } #[cfg(windows)] diff --git a/src/find/matchers/logical_matchers.rs b/src/find/matchers/logical_matchers.rs index fa63084..349b487 100644 --- a/src/find/matchers/logical_matchers.rs +++ b/src/find/matchers/logical_matchers.rs @@ -47,7 +47,9 @@ impl Matcher for AndMatcher { } fn has_side_effects(&self) -> bool { - self.submatchers.iter().any(|x| x.has_side_effects()) + self.submatchers + .iter() + .any(super::Matcher::has_side_effects) } fn finished_dir(&self, dir: &Path) { @@ -121,7 +123,9 @@ impl Matcher for OrMatcher { } fn has_side_effects(&self) -> bool { - self.submatchers.iter().any(|x| x.has_side_effects()) + self.submatchers + .iter() + .any(super::Matcher::has_side_effects) } fn finished_dir(&self, dir: &Path) { @@ -214,7 +218,9 @@ impl Matcher for ListMatcher { } fn has_side_effects(&self) -> bool { - self.submatchers.iter().any(|x| x.has_side_effects()) + self.submatchers + .iter() + .any(super::Matcher::has_side_effects) } fn finished_dir(&self, dir: &Path) { diff --git a/src/find/matchers/mod.rs b/src/find/matchers/mod.rs index 1725da0..6e4fceb 100644 --- a/src/find/matchers/mod.rs +++ b/src/find/matchers/mod.rs @@ -74,6 +74,7 @@ impl<'a> MatcherIO<'a> { self.should_skip_dir = true; } + #[must_use] pub fn should_skip_current_dir(&self) -> bool { self.should_skip_dir } @@ -82,10 +83,12 @@ impl<'a> MatcherIO<'a> { self.quit = true; } + #[must_use] pub fn should_quit(&self) -> bool { self.quit } + #[must_use] pub fn now(&self) -> SystemTime { self.deps.now() } @@ -140,11 +143,11 @@ impl Matcher for Box { } fn finished_dir(&self, finished_directory: &Path) { - (**self).finished_dir(finished_directory) + (**self).finished_dir(finished_directory); } fn finished(&self) { - (**self).finished() + (**self).finished(); } } @@ -546,8 +549,8 @@ mod tests { use crate::find::tests::FakeDependencies; use walkdir::WalkDir; - /// Helper function for tests to get a DirEntry object. directory should - /// probably be a string starting with "test_data/" (cargo's tests run with + /// Helper function for tests to get a `DirEntry` object. directory should + /// probably be a string starting with `test_data/` (cargo's tests run with /// a working directory set to the root findutils folder). pub fn get_dir_entry_for(directory: &str, filename: &str) -> DirEntry { for wrapped_dir_entry in WalkDir::new(fix_up_slashes(directory)) { @@ -562,7 +565,7 @@ mod tests { return dir_entry; } } - panic!("Couldn't find {} in {}", filename, directory); + panic!("Couldn't find {filename} in {directory}"); } #[test] @@ -1052,8 +1055,7 @@ mod tests { if let Err(e) = build_top_level_matcher(&["-ctime", "-123."], &mut config) { assert!( e.to_string().contains("Expected a decimal integer"), - "bad description: {}", - e + "bad description: {e}" ); } else { panic!("parsing a bad ctime value should fail"); diff --git a/src/find/matchers/name.rs b/src/find/matchers/name.rs index 87cb318..a455937 100644 --- a/src/find/matchers/name.rs +++ b/src/find/matchers/name.rs @@ -48,8 +48,7 @@ mod tests { if let Err(e) = symlink("abbbc", "test_data/links/link-f") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } #[cfg(windows)] diff --git a/src/find/matchers/perm.rs b/src/find/matchers/perm.rs index 6a8e44f..2ece453 100644 --- a/src/find/matchers/perm.rs +++ b/src/find/matchers/perm.rs @@ -41,7 +41,7 @@ impl ComparisonType { #[cfg(unix)] mod parsing { - use super::*; + use super::{parse_numeric, parse_symbolic, ComparisonType, Error}; pub fn split_comparison_type(pattern: &str) -> (ComparisonType, &str) { let mut chars = pattern.chars(); diff --git a/src/find/matchers/printf.rs b/src/find/matchers/printf.rs index f4a53cd..6929ccc 100644 --- a/src/find/matchers/printf.rs +++ b/src/find/matchers/printf.rs @@ -158,10 +158,9 @@ impl FormatStringParser<'_> { // Try parsing an octal sequence first. let first = self.front()?; if first.is_digit(OCTAL_RADIX) { - if let Ok(code) = self - .peek(OCTAL_LEN) - .and_then(|octal| u32::from_str_radix(octal, OCTAL_RADIX).map_err(|e| e.into())) - { + if let Ok(code) = self.peek(OCTAL_LEN).and_then(|octal| { + u32::from_str_radix(octal, OCTAL_RADIX).map_err(std::convert::Into::into) + }) { // safe to unwrap: .peek() already succeeded above. let octal = self.advance_by(OCTAL_LEN).unwrap(); return match char::from_u32(code) { @@ -388,7 +387,7 @@ fn format_directive<'entry>( // symlink itself instead. file_info.path().symlink_metadata() } else { - file_info.metadata().map_err(|e| e.into()) + file_info.metadata().map_err(std::convert::Into::into) } }) }; @@ -468,8 +467,7 @@ fn format_directive<'entry>( fs_list .into_iter() .find(|fs| fs.dev_id == dev_id) - .map(|fs| fs.fs_type) - .unwrap_or_else(String::new) + .map_or_else(String::new, |fs| fs.fs_type) .into() } @@ -949,36 +947,31 @@ mod tests { if let Err(e) = symlink("abbbc", "test_data/links/link-f") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } if let Err(e) = symlink("subdir", "test_data/links/link-d") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } if let Err(e) = symlink("missing", "test_data/links/link-missing") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } if let Err(e) = symlink("abbbc/x", "test_data/links/link-notdir") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } if let Err(e) = symlink("link-loop", "test_data/links/link-loop") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } } diff --git a/src/find/matchers/regex.rs b/src/find/matchers/regex.rs index d7d1f34..9959087 100644 --- a/src/find/matchers/regex.rs +++ b/src/find/matchers/regex.rs @@ -23,7 +23,7 @@ impl fmt::Display for ParseRegexTypeError { self.0, RegexType::VALUES .iter() - .map(|t| format!("'{}'", t)) + .map(|t| format!("'{t}'")) .collect::>() .join(", ") ) diff --git a/src/find/matchers/size.rs b/src/find/matchers/size.rs index bb5c6ce..5c6a9e1 100644 --- a/src/find/matchers/size.rs +++ b/src/find/matchers/size.rs @@ -147,8 +147,7 @@ mod tests { if let Err(e) = SizeMatcher::new(ComparableValue::EqualTo(2), "xyz") { assert!( e.to_string().contains("Invalid suffix") && e.to_string().contains("xyz"), - "bad description: {}", - e + "bad description: {e}" ); } else { panic!("parsing a unit string should fail"); diff --git a/src/find/matchers/time.rs b/src/find/matchers/time.rs index 95f89e9..3f0ab57 100644 --- a/src/find/matchers/time.rs +++ b/src/find/matchers/time.rs @@ -337,7 +337,7 @@ mod tests { } } - /// helper function for file_time_matcher_modified_created_accessed + /// helper function for `file_time_matcher_modified_created_accessed` fn test_matcher_for_file_time_type( file_info: &DirEntry, file_time: SystemTime, @@ -350,15 +350,13 @@ mod tests { deps.set_time(file_time); assert!( matcher.matches(file_info, &mut deps.new_matcher_io()), - "{:?} time matcher should match", - file_time_type + "{file_time_type:?} time matcher should match" ); deps.set_time(file_time - Duration::from_secs(1)); assert!( !matcher.matches(file_info, &mut deps.new_matcher_io()), - "{:?} time matcher shouldn't match a second before", - file_time_type + "{file_time_type:?} time matcher shouldn't match a second before" ); } } diff --git a/src/find/matchers/type_matcher.rs b/src/find/matchers/type_matcher.rs index 5c7a103..b923215 100644 --- a/src/find/matchers/type_matcher.rs +++ b/src/find/matchers/type_matcher.rs @@ -109,15 +109,13 @@ mod tests { if let Err(e) = symlink("abbbc", "test_data/links/link-f") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } if let Err(e) = symlink("subdir", "test_data/links/link-d") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } }; diff --git a/src/find/mod.rs b/src/find/mod.rs index 08c94f4..e42e49e 100644 --- a/src/find/mod.rs +++ b/src/find/mod.rs @@ -51,6 +51,7 @@ pub struct StandardDependencies { } impl StandardDependencies { + #[must_use] pub fn new() -> Self { Self { output: Rc::new(RefCell::new(stdout())), @@ -338,8 +339,7 @@ mod tests { if let Err(e) = symlink("abbbc", "test_data/links/link-f") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } #[cfg(windows)] @@ -692,7 +692,7 @@ mod tests { } } - /// Helper function for the find_ctime/find_atime/find_mtime tests. + /// Helper function for the `find_ctime/find_atime/find_mtime` tests. fn file_time_helper(file_time: SystemTime, arg: &str) { // check file time matches a file that's old enough { diff --git a/src/xargs/mod.rs b/src/xargs/mod.rs index 74144b7..0c420a3 100644 --- a/src/xargs/mod.rs +++ b/src/xargs/mod.rs @@ -79,7 +79,7 @@ trait CommandSizeLimiter { } /// A pointer to the next limiter. A limiter should *always* call the cursor's -/// try_next *before* updating its own state, to ensure that all other limiters +/// `try_next` *before* updating its own state, to ensure that all other limiters /// are okay with the argument first. struct LimiterCursor<'collection> { limiters: &'collection mut [Box], @@ -353,7 +353,7 @@ impl CommandBuilderOptions { replace: Option, ) -> Result { let initial_args = match &action { - ExecAction::Command(args) => args.iter().map(|arg| arg.as_ref()).collect(), + ExecAction::Command(args) => args.iter().map(std::convert::AsRef::as_ref).collect(), ExecAction::Echo => vec![OsStr::new("echo")], }; @@ -726,10 +726,10 @@ fn process_input( fn parse_delimiter(s: &str) -> Result { match s.strip_prefix('\\') { Some(hex) if hex.starts_with('x') => { - u8::from_str_radix(&hex[1..], 16).map_err(|e| format!("Invalid hex sequence: {}", e)) + u8::from_str_radix(&hex[1..], 16).map_err(|e| format!("Invalid hex sequence: {e}")) } Some(oct) if oct.starts_with('0') => { - u8::from_str_radix(&oct[1..], 8).map_err(|e| format!("Invalid octal sequence: {}", e)) + u8::from_str_radix(&oct[1..], 8).map_err(|e| format!("Invalid octal sequence: {e}")) } Some(special) => match special { "a" => Ok(b'\x07'), @@ -741,7 +741,7 @@ fn parse_delimiter(s: &str) -> Result { "v" => Ok(b'\x0B'), "\\" => Ok(b'\\'), "0" => Ok(b'\0'), - _ => Err(format!("Invalid escape sequence: \\{}", special)), + _ => Err(format!("Invalid escape sequence: \\{special}")), }, None if s.len() == 1 => Ok(s.as_bytes()[0]), None => Err("Delimiter must be one byte".to_owned()), @@ -880,7 +880,7 @@ fn do_xargs(args: &[&str]) -> Result { let options = Options { arg_file: matches .get_one::(options::ARG_FILE) - .map(|value| value.to_owned()), + .map(std::borrow::ToOwned::to_owned), delimiter: matches.get_one::(options::DELIMITER).copied(), exit_if_pass_char_limit: matches.get_flag(options::EXIT), max_args: matches.get_one::(options::MAX_ARGS).copied(), @@ -894,8 +894,7 @@ fn do_xargs(args: &[&str]) -> Result { matches.contains_id(option).then(|| { matches .get_one::(option) - .map(|value| value.to_owned()) - .unwrap_or_else(|| "{}".to_string()) + .map_or_else(|| "{}".to_string(), std::borrow::ToOwned::to_owned) }) }), verbose: matches.get_flag(options::VERBOSE), @@ -918,7 +917,7 @@ fn do_xargs(args: &[&str]) -> Result { let action = match matches.get_many::(options::COMMAND) { Some(args) if args.len() > 0 => { - ExecAction::Command(args.map(|arg| arg.to_owned()).collect()) + ExecAction::Command(args.map(std::borrow::ToOwned::to_owned).collect()) } _ => ExecAction::Echo, }; @@ -960,7 +959,7 @@ fn do_xargs(args: &[&str]) -> Result { builder_options.close_stdin = options.arg_file.is_none(); let args_file: Box = if let Some(path) = &options.arg_file { - Box::new(fs::File::open(path).map_err(|e| format!("Failed to open {}: {}", path, e))?) + Box::new(fs::File::open(path).map_err(|e| format!("Failed to open {path}: {e}"))?) } else { Box::new(io::stdin()) }; @@ -975,6 +974,7 @@ fn do_xargs(args: &[&str]) -> Result { Ok(result) } +#[must_use] pub fn xargs_main(args: &[&str]) -> i32 { match do_xargs(args) { Ok(CommandResult::Success) => 0, diff --git a/tests/common/test_helpers.rs b/tests/common/test_helpers.rs index 122430e..72b076e 100644 --- a/tests/common/test_helpers.rs +++ b/tests/common/test_helpers.rs @@ -90,5 +90,5 @@ pub fn get_dir_entry_for(directory: &str, filename: &str) -> DirEntry { return dir_entry; } } - panic!("Couldn't find {} in {}", directory, filename); + panic!("Couldn't find {directory} in {filename}"); } diff --git a/tests/find_cmd_tests.rs b/tests/find_cmd_tests.rs index a38c26f..78e7b86 100644 --- a/tests/find_cmd_tests.rs +++ b/tests/find_cmd_tests.rs @@ -285,36 +285,31 @@ fn find_printf() { if let Err(e) = symlink("abbbc", "test_data/links/link-f") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } if let Err(e) = symlink("subdir", "test_data/links/link-d") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } if let Err(e) = symlink("missing", "test_data/links/link-missing") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } if let Err(e) = symlink("abbbc/x", "test_data/links/link-notdir") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } if let Err(e) = symlink("link-loop", "test_data/links/link-loop") { assert!( e.kind() == ErrorKind::AlreadyExists, - "Failed to create sym link: {:?}", - e + "Failed to create sym link: {e:?}" ); } } diff --git a/tests/xargs_tests.rs b/tests/xargs_tests.rs index fb6aa9a..4e447c9 100644 --- a/tests/xargs_tests.rs +++ b/tests/xargs_tests.rs @@ -220,11 +220,11 @@ fn xargs_exec() { ]) .write_stdin("a b c\nd") .output(); - assert!(result.is_ok(), "xargs failed: {:?}", result); + assert!(result.is_ok(), "xargs failed: {result:?}"); let result = result.unwrap(); assert_eq!(result.status.code(), Some(0)); - assert!(result.stderr.is_empty(), "stderr: {:?}", result); + assert!(result.stderr.is_empty(), "stderr: {result:?}"); let stdout_string = String::from_utf8(result.stdout).expect("Found invalid UTF-8"); @@ -255,11 +255,11 @@ fn xargs_exec_stdin_open() { .write_stdin("test") .output(); - assert!(result.is_ok(), "xargs failed: {:?}", result); + assert!(result.is_ok(), "xargs failed: {result:?}"); let result = result.unwrap(); assert_eq!(result.status.code(), Some(0)); - assert!(result.stderr.is_empty(), "stderr: {:?}", result); + assert!(result.stderr.is_empty(), "stderr: {result:?}"); let stdout_string = String::from_utf8(result.stdout).expect("Found invalid UTF-8"); @@ -283,11 +283,11 @@ fn xargs_exec_failure() { .write_stdin("a b") .output(); - assert!(result.is_ok(), "xargs failed: {:?}", result); + assert!(result.is_ok(), "xargs failed: {result:?}"); let result = result.unwrap(); assert_eq!(result.status.code(), Some(123)); - assert!(result.stderr.is_empty(), "stderr: {:?}", result); + assert!(result.stderr.is_empty(), "stderr: {result:?}"); let stdout_string = String::from_utf8(result.stdout).expect("Found invalid UTF-8"); @@ -312,11 +312,11 @@ fn xargs_exec_urgent_failure() { .write_stdin("a b") .output(); - assert!(result.is_ok(), "xargs failed: {:?}", result); + assert!(result.is_ok(), "xargs failed: {result:?}"); let result = result.unwrap(); assert_eq!(result.status.code(), Some(124)); - assert!(!result.stderr.is_empty(), "stderr: {:?}", result); + assert!(!result.stderr.is_empty(), "stderr: {result:?}"); let stdout_string = String::from_utf8(result.stdout).expect("Found invalid UTF-8"); @@ -341,10 +341,10 @@ fn xargs_exec_with_signal() { .write_stdin("a b") .output(); - assert!(result.is_ok(), "xargs failed: {:?}", result); + assert!(result.is_ok(), "xargs failed: {result:?}"); let result = result.unwrap(); assert_eq!(result.status.code(), Some(125)); - assert!(!result.stderr.is_empty(), "stderr: {:?}", result); + assert!(!result.stderr.is_empty(), "stderr: {result:?}"); let stdout_string = String::from_utf8(result.stdout).expect("Found invalid UTF-8");