From f462368a451b2c8a957cdc9fef8fbe6b79b731f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ismael=20Gonz=C3=A1lez?= Date: Mon, 6 Dec 2021 20:21:33 +0100 Subject: [PATCH] chore: fix some clippy lints/update dependencies MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ismael González --- Cargo.lock | 139 +++++++++++++++++++++-------------------------- src/config.rs | 2 +- src/dumpfile.rs | 4 +- src/fileutils.rs | 13 +++-- src/output.rs | 14 ++--- src/renamer.rs | 4 +- src/solver.rs | 46 +++++++++------- 7 files changed, 106 insertions(+), 116 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a160dd..a81b960 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "aho-corasick" -version = "0.7.15" +version = "0.7.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" dependencies = [ "memchr", ] @@ -45,15 +45,15 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cfg-if" -version = "0.1.10" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" @@ -91,29 +91,29 @@ checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" [[package]] name = "getrandom" -version = "0.1.15" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" dependencies = [ "cfg-if", "libc", - "wasi 0.9.0+wasi-snapshot-preview1", + "wasi", ] [[package]] name = "hermit-abi" -version = "0.1.17" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] [[package]] name = "itoa" -version = "0.4.6" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "lazy_static" @@ -123,15 +123,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.80" +version = "0.2.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" +checksum = "f98a04dce437184842841303488f70d0188c5f51437d2a834dc097eafa909a01" [[package]] name = "memchr" -version = "2.3.4" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "num-integer" @@ -166,35 +166,34 @@ dependencies = [ [[package]] name = "ppv-lite86" -version = "0.2.10" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" [[package]] name = "proc-macro2" -version = "1.0.24" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +checksum = "fb37d2df5df740e582f28f8560cf425f52bb267d872fe58358eadb554909f07a" dependencies = [ "unicode-xid", ] [[package]] name = "quote" -version = "1.0.7" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" dependencies = [ "proc-macro2", ] [[package]] name = "rand" -version = "0.7.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" dependencies = [ - "getrandom", "libc", "rand_chacha", "rand_core", @@ -203,9 +202,9 @@ dependencies = [ [[package]] name = "rand_chacha" -version = "0.2.2" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", "rand_core", @@ -213,45 +212,47 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.5.1" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" dependencies = [ "getrandom", ] [[package]] name = "rand_hc" -version = "0.2.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" dependencies = [ "rand_core", ] [[package]] name = "redox_syscall" -version = "0.1.57" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" +dependencies = [ + "bitflags", +] [[package]] name = "regex" -version = "1.4.2" +version = "1.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" dependencies = [ "aho-corasick", "memchr", "regex-syntax", - "thread_local", ] [[package]] name = "regex-syntax" -version = "0.6.21" +version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" [[package]] name = "remove_dir_all" @@ -283,9 +284,9 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +checksum = "3c9613b5a66ab9ba26415184cfc41156594925a9cf3a2057e57f31ff145f6568" [[package]] name = "same-file" @@ -298,15 +299,15 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.117" +version = "1.0.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" [[package]] name = "serde_derive" -version = "1.0.117" +version = "1.0.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" +checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" dependencies = [ "proc-macro2", "quote", @@ -315,9 +316,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.59" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcac07dbffa1c65e7f816ab9eba78eb142c6d44410f4eeba1e26e4f5dfa56b95" +checksum = "d0ffa0837f2dfa6fb90868c2b5468cad482e175f7dad97e7421951e663f2b527" dependencies = [ "itoa", "ryu", @@ -348,9 +349,9 @@ checksum = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" [[package]] name = "syn" -version = "1.0.48" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac" +checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59" dependencies = [ "proc-macro2", "quote", @@ -359,9 +360,9 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.1.0" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" dependencies = [ "cfg-if", "libc", @@ -380,37 +381,27 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "thread_local" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" -dependencies = [ - "lazy_static", -] - [[package]] name = "time" -version = "0.1.44" +version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438" dependencies = [ "libc", - "wasi 0.10.0+wasi-snapshot-preview1", "winapi", ] [[package]] name = "unicode-width" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" [[package]] name = "unicode-xid" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" [[package]] name = "vec_map" @@ -420,9 +411,9 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" [[package]] name = "walkdir" -version = "2.3.1" +version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" dependencies = [ "same-file", "winapi", @@ -431,15 +422,9 @@ dependencies = [ [[package]] name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" - -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" +version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] name = "winapi" diff --git a/src/config.rs b/src/config.rs index ab1225d..8748cd7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -151,7 +151,7 @@ fn parse_arguments() -> Result { match matches.value_of("color").unwrap_or("auto") { "always" => Printer::color(), "never" => Printer::no_color(), - "auto" | _ => detect_output_color(), + _ => detect_output_color(), // Ignore non-valid values and use auto. } }; diff --git a/src/dumpfile.rs b/src/dumpfile.rs index e50015a..53565ed 100644 --- a/src/dumpfile.rs +++ b/src/dumpfile.rs @@ -3,7 +3,7 @@ use error::*; use serde_json; use solver::{Operation, Operations}; use std::fs::File; -use std::path::PathBuf; +use std::path::Path; /// Dump operations intto file in JSON format pub fn dump_to_file(operations: &[Operation]) -> Result<()> { @@ -36,7 +36,7 @@ pub fn dump_to_file(operations: &[Operation]) -> Result<()> { } /// Read operations from a dump file and generate a Operations vector -pub fn read_from_file(filepath: &PathBuf) -> Result { +pub fn read_from_file(filepath: &Path) -> Result { let file = match File::open(&filepath) { Ok(file) => file, Err(_) => { diff --git a/src/fileutils.rs b/src/fileutils.rs index 2253148..389255a 100644 --- a/src/fileutils.rs +++ b/src/fileutils.rs @@ -1,3 +1,4 @@ +use std::path::Path; use config::RunMode; use error::*; use path_abs::PathAbs; @@ -53,9 +54,9 @@ pub fn get_paths(mode: &RunMode) -> PathList { /// Generate a non-existing name adding numbers to the end of the file name. It also supports adding a /// suffix to the original name. -pub fn get_unique_filename(path: &PathBuf, suffix: &str) -> PathBuf { +pub fn get_unique_filename(path: &Path, suffix: &str) -> PathBuf { let base_name = format!("{}{}", path.file_name().unwrap().to_string_lossy(), suffix); - let mut unique_name = path.clone(); + let mut unique_name = path.to_path_buf(); unique_name.set_file_name(&base_name); let mut index = 0; @@ -68,7 +69,7 @@ pub fn get_unique_filename(path: &PathBuf, suffix: &str) -> PathBuf { } /// Create a backup of the file -pub fn create_backup(path: &PathBuf) -> Result { +pub fn create_backup(path: &Path) -> Result { let backup = get_unique_filename(path, ".bk"); match fs::copy(path, &backup) { Ok(_) => Ok(backup), @@ -106,7 +107,7 @@ pub fn cleanup_paths(paths: &mut PathList, keep_dirs: bool) { /// Wrapper to create symlink files without considering the OS explicitly #[allow(dead_code)] -pub fn create_symlink(source: &PathBuf, symlink_file: &PathBuf) -> Result<()> { +pub fn create_symlink(source: &Path, symlink_file: &Path) -> Result<()> { #[cfg(windows)] match ::std::os::windows::fs::symlink_file(source, symlink_file) { Ok(_) => Ok(()), @@ -126,7 +127,7 @@ pub fn create_symlink(source: &PathBuf, symlink_file: &PathBuf) -> Result<()> { } /// Check if the paths references the same file. This is useful in case insensitive systems. -pub fn is_same_file(source: &PathBuf, target: &PathBuf) -> bool { +pub fn is_same_file(source: &Path, target: &Path) -> bool { // Only perform a more exhaustive check for platform that support case insensitive and case // preserving file systems by default. #[cfg(any(windows, target_os = "macos"))] @@ -167,7 +168,7 @@ mod test { for file in &mock_files { fs::File::create(&file).expect("Error creating mock file..."); - create_backup(&file).expect("Error generating backup file..."); + create_backup(file).expect("Error generating backup file..."); } let backup_files: PathList = vec![ diff --git a/src/output.rs b/src/output.rs index 3b0f6e7..b3800e3 100644 --- a/src/output.rs +++ b/src/output.rs @@ -2,7 +2,7 @@ use ansi_term::Colour::*; use ansi_term::Style; use difference::{Changeset, Difference}; use error::*; -use std::path::PathBuf; +use std::path::Path; #[derive(PartialEq)] enum PrinterMode { @@ -99,7 +99,7 @@ impl Printer { /// Print error pretty printed pub fn print_error(&self, error: &Error) { - let error_value = error.value.to_owned().unwrap_or("".to_string()); + let error_value = error.value.to_owned().unwrap_or_else(|| String::from("")); self.eprint(&format!( "{}{}{}", @@ -110,7 +110,7 @@ impl Printer { } /// Pretty print operation - pub fn print_operation(&self, source: &PathBuf, target: &PathBuf) { + pub fn print_operation(&self, source: &Path, target: &Path) { // Avoid any additional processing costs if silent mode if self.mode == PrinterMode::Silent { return; @@ -133,14 +133,14 @@ impl Printer { source_name = self.colors.source.paint(&source_name).to_string(); - if source_parent != "" { + if !source_parent.is_empty() { source_parent = self .colors .source .paint(format!("{}/", source_parent)) .to_string(); } - if target_parent != "" { + if !target_parent.is_empty() { target_parent = self .colors .target @@ -157,8 +157,8 @@ impl Printer { /// Generate a colored diff from the given strings fn string_diff( &self, - original: &String, - changed: &String, + original: &str, + changed: &str, base_color: Style, diff_color: Style, ) -> String { diff --git a/src/renamer.rs b/src/renamer.rs index be408c5..298ea81 100644 --- a/src/renamer.rs +++ b/src/renamer.rs @@ -64,7 +64,7 @@ impl Renamer { } /// Replace file name matches in the given path using stored config. - fn replace_match(&self, path: &PathBuf) -> PathBuf { + fn replace_match(&self, path: &Path) -> PathBuf { let file_name = path.file_name().unwrap().to_str().unwrap(); let parent = path.parent(); @@ -94,7 +94,7 @@ impl Renamer { let mut error_string = String::new(); for path in paths { - let target = self.replace_match(&path); + let target = self.replace_match(path); // Discard paths with no changes if target != *path { if let Some(old_path) = rename_map.insert(target.clone(), path.clone()) { diff --git a/src/solver.rs b/src/solver.rs index 903bea9..e00353c 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -23,7 +23,7 @@ pub fn solve_rename_order(rename_map: &RenameMap) -> Result { .values() .map(|p| p.components().count()) .collect(); - level_list.sort(); + level_list.sort_unstable(); level_list.dedup(); level_list.reverse(); @@ -42,7 +42,7 @@ pub fn solve_rename_order(rename_map: &RenameMap) -> Result { }) .collect(); // Return existing targets in the list of original filenames - let mut existing_targets = get_existing_targets(&level_targets, &rename_map)?; + let mut existing_targets = get_existing_targets(&level_targets, rename_map)?; // Store first all non conflicting entries rename_order.append( @@ -58,7 +58,7 @@ pub fn solve_rename_order(rename_map: &RenameMap) -> Result { .collect(), ); // Order and append the rest of entries - match sort_existing_targets(&rename_map, &mut existing_targets) { + match sort_existing_targets(rename_map, &mut existing_targets) { Ok(mut targets) => rename_order.append(&mut targets), Err(err) => return Err(err), } @@ -95,26 +95,28 @@ pub fn revert_operations(operations: &[Operation]) -> Result { /// targets must be contained in the original file list for the renaming problem to be solvable. fn get_existing_targets(targets: &[PathBuf], rename_map: &RenameMap) -> Result { let mut existing_targets: PathList = Vec::new(); - let sources: PathList = rename_map.values().cloned().collect(); for target in targets { - if target.symlink_metadata().is_ok() { - if !sources.contains(&target) { - let source = rename_map.get(target).cloned().unwrap(); - - // The source and the target may be the same file in some conditions like case - // insensitive but case-preserving file systems. - if is_same_file(&source, &target) { - continue; - } + if target.symlink_metadata().is_err() { + continue; + } - return Err(Error { - kind: ErrorKind::ExistingPath, - value: Some(format!("{} -> {}", source.display(), target.display())), - }); + if !rename_map.values().any(|x| x == target) { + let source = rename_map.get(target).cloned().unwrap(); + + // The source and the target may be the same file in some conditions like case + // insensitive but case-preserving file systems. + if is_same_file(&source, target) { + continue; } - existing_targets.push(target.clone()); + + return Err(Error { + kind: ErrorKind::ExistingPath, + value: Some(format!("{} -> {}", source.display(), target.display())), + }); } + + existing_targets.push(target.clone()); } Ok(existing_targets) } @@ -131,17 +133,19 @@ fn sort_existing_targets( while !existing_targets.is_empty() { // Track selected index to extract value let mut selected_index: Option = None; + // Create a vector with all sources from existing targets using absolute paths + #[allow(clippy::needless_collect)] // Benchmark shows no diff, code is clearer this way. let sources: PathList = existing_targets .iter() .map(|x| rename_map.get(x).cloned().unwrap()) .map(|p| PathAbs::new(p).unwrap().to_path_buf()) .collect(); + // Select targets without conflicts in sources for (index, target) in existing_targets.iter().enumerate() { - if sources.contains(&PathAbs::new(target).unwrap().to_path_buf()) { - continue; - } else { + let absolute_target = PathAbs::new(target).unwrap().to_path_buf(); + if !sources.contains(&absolute_target) { selected_index = Some(index); break; }