From a823844538021cc24a87d9fb70ea10cb950aeab0 Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Wed, 20 Nov 2024 15:28:30 +0100 Subject: [PATCH] let clippy help --- Cargo.lock | 2 +- src/main.rs | 29 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a405e4d..af8bba4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,7 +99,7 @@ checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "mvw" -version = "0.1.0" +version = "0.1.3" dependencies = [ "dialoguer", "rand", diff --git a/src/main.rs b/src/main.rs index 0adf734..3f6f105 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,7 +48,7 @@ fn lock_dir() { fn unlock_dir() { if Path::new(LOCK_FILE).is_file() { - let _result = fs::remove_file(LOCK_FILE).expect("Could not remove LOCK_FILE"); + fs::remove_file(LOCK_FILE).expect("Could not remove LOCK_FILE"); } } @@ -68,7 +68,7 @@ fn create_temp_file(paths: &Vec) -> String { fn remove_temp_file(tmpfile: &String) { if Path::new(tmpfile).is_file() { - let _result = fs::remove_file(tmpfile).expect("Could not remove tempfile"); + fs::remove_file(tmpfile).expect("Could not remove tempfile"); } } @@ -88,12 +88,12 @@ fn read_temp_file(tmpfile: &String) -> Vec { .collect() } -fn unique_length(dst_paths: &Vec) -> usize { - let map = dst_paths.into_iter().map(|x| (x, x)).collect::>(); +fn unique_length(dst_paths: &[String]) -> usize { + let map = dst_paths.iter().map(|x| (x, x)).collect::>(); map.len() } -fn unique_filename(dst_paths: &Vec) -> String { +fn unique_filename(dst_paths: &[String]) -> String { let mut string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16); // Generate unique name that does not exist in dir and is not in dst_paths @@ -103,7 +103,7 @@ fn unique_filename(dst_paths: &Vec) -> String { string } -fn move_safely(src_paths: &Vec, dst_paths: &Vec) { +fn move_safely(src_paths: &[String], dst_paths: &[String]) { let src_len = src_paths.len(); let mut intermediate_files: HashMap = HashMap::new(); @@ -112,7 +112,7 @@ fn move_safely(src_paths: &Vec, dst_paths: &Vec) { // is the destination already in the source list? // if so, an intermediate file is needed if src_paths.iter().any(|j| j==&dst_paths[i]) { - let unique = unique_filename(&dst_paths); + let unique = unique_filename(dst_paths); intermediate_files.insert(unique.clone(), dst_paths[i].clone()); fs::rename(&src_paths[i], &unique).expect("failed to rename file"); println!("Moving {} -> {}", src_paths[i], unique); @@ -123,12 +123,12 @@ fn move_safely(src_paths: &Vec, dst_paths: &Vec) { } } for (src, dst) in intermediate_files.iter() { - fs::rename(&src, &dst).expect("failed to rename file"); + fs::rename(src, dst).expect("failed to rename file"); println!("Moving {} -> {}", src, dst); } } -fn run_checks(src_paths: &Vec, dst_paths: &Vec) -> bool { +fn run_checks(src_paths: &[String], dst_paths: &[String]) -> bool { // Make sure there are an equal number of sources and destinations if src_paths.len() != dst_paths.len() { println!("ERROR: Source and target list don't have the same number of lines."); @@ -136,26 +136,25 @@ fn run_checks(src_paths: &Vec, dst_paths: &Vec) -> bool { } // Make sure destination names are not empty - if dst_paths.iter().any(|i| i=="") { + if dst_paths.iter().any(|i| i.is_empty()) { println!("ERROR: You can't move to empty names."); return true } // Make sure all destination files are unique - let dst_paths_length_unique = unique_length(&dst_paths); + let dst_paths_length_unique = unique_length(dst_paths); if dst_paths.len() != dst_paths_length_unique { println!("ERROR: You're trying to move multiple files to the same name."); - show_doubles(&src_paths, &dst_paths); + show_doubles(src_paths, dst_paths); return true } false } -fn show_doubles(src_paths: &Vec, dst_paths: &Vec) { +fn show_doubles(src_paths: &[String], dst_paths: &[String]) { let mut paths = HashMap::new(); - let dst_len = dst_paths.len(); - for i in 0..dst_len { + for (i, _) in dst_paths.iter().enumerate() { paths.entry(&dst_paths[i]).or_insert(Vec::new()); paths.get_mut(&dst_paths[i]).unwrap().push(i); }