From 876609f2cb8c32cb76a868c8a118d103a42edabd Mon Sep 17 00:00:00 2001 From: bootandy Date: Sun, 30 Jun 2019 20:05:03 +0100 Subject: [PATCH] Obey new clippy Clippy is like having a reviewer fix your dodgy code. --- src/display.rs | 39 ++++++++++++++++++++++------------- src/main.rs | 8 +++++++- src/tests.rs | 16 +++++++++++++-- src/utils/mod.rs | 47 ++++++++++++++++++++----------------------- src/utils/platform.rs | 2 +- 5 files changed, 69 insertions(+), 43 deletions(-) diff --git a/src/display.rs b/src/display.rs index 3cbc284..0571d5d 100644 --- a/src/display.rs +++ b/src/display.rs @@ -12,7 +12,7 @@ pub fn draw_it( depth: Option, base_dirs: HashSet, to_display: Vec<(String, u64)>, -) -> () { +) { if !permissions { eprintln!("Did not have permissions for all directories"); } @@ -20,12 +20,20 @@ pub fn draw_it( for &(ref k, _) in to_display.iter() { if base_dirs.contains(k) { - display_node(&k, &mut found, &to_display, true, short_paths, depth, "─┬"); + display_node( + &k, + &mut found, + &to_display, + true, + short_paths, + depth, + "─┬", + ); } } } -fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option { +fn get_size(nodes: &[(String, u64)], node_to_print: &str) -> Option { for &(ref k, ref v) in nodes.iter() { if *k == *node_to_print { return Some(*v); @@ -35,9 +43,9 @@ fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option { } fn display_node>( - node_to_print: &String, + node_to_print: &str, found: &mut HashSet, - to_display: &Vec<(String, u64)>, + to_display: &[(String, u64)], is_biggest: bool, short_paths: bool, depth: Option, @@ -56,7 +64,7 @@ fn display_node>( match get_size(to_display, node_to_print) { None => println!("Can not find path: {}", node_to_print), Some(size) => { - let ntp: &str = node_to_print.as_ref(); + let ntp: &str = node_to_print; let num_slashes = node_to_print.matches('/').count(); let is = indentation_str.into(); @@ -96,7 +104,7 @@ fn clean_indentation_string>(s: S) -> String { is } -fn count_siblings(to_display: &Vec<(String, u64)>, num_slashes: usize, ntp: &str) -> u64 { +fn count_siblings(to_display: &[(String, u64)], num_slashes: usize, ntp: &str) -> u64 { to_display.iter().fold(0, |a, b| { if b.0.starts_with(ntp) && b.0.matches('/').count() == num_slashes + 1 { a + 1 @@ -106,7 +114,12 @@ fn count_siblings(to_display: &Vec<(String, u64)>, num_slashes: usize, ntp: &str }) } -fn has_children(to_display: &Vec<(String, u64)>, new_depth: Option, ntp: &str, num_slashes: usize) -> bool { +fn has_children( + to_display: &[(String, u64)], + new_depth: Option, + ntp: &str, + num_slashes: usize, +) -> bool { if new_depth.is_none() || new_depth.unwrap() != 1 { for &(ref k2, _) in to_display.iter() { if k2.starts_with(ntp) && k2.matches('/').count() == num_slashes + 1 { @@ -114,7 +127,7 @@ fn has_children(to_display: &Vec<(String, u64)>, new_depth: Option, ntp: &s } } } - return false; + false } fn get_tree_chars(has_smaller_siblings: bool, has_children: bool) -> &'static str { @@ -124,12 +137,10 @@ fn get_tree_chars(has_smaller_siblings: bool, has_children: bool) -> &'static st } else { "└──" } + } else if has_children { + "├─┬" } else { - if has_children { - "├─┬" - } else { - "├──" - } + "├──" } } diff --git a/src/main.rs b/src/main.rs index 2df3565..5e4888a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,7 +91,13 @@ fn main() { Some(d) => trim_deep_ones(sorted_data, d, &simplified_dirs), } }; - draw_it(permissions, !use_full_path, depth, simplified_dirs, biggest_ones); + draw_it( + permissions, + !use_full_path, + depth, + simplified_dirs, + biggest_ones, + ); } #[cfg(test)] diff --git a/src/tests.rs b/src/tests.rs index bb969fd..d2469ea 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -45,7 +45,13 @@ fn main_output(short_paths: bool) -> String { {} {}", format_string("src/test_dir", true, short_paths, " 4.0K", "─┬"), - format_string("src/test_dir/many", true, short_paths, " 4.0K", " └─┬",), + format_string( + "src/test_dir/many", + true, + short_paths, + " 4.0K", + " └─┬", + ), format_string( "src/test_dir/many/hello_file", true, @@ -71,7 +77,13 @@ fn main_output(short_paths: bool) -> String { {} {}", format_string("src/test_dir", true, short_paths, " 12K", "─┬"), - format_string("src/test_dir/many", true, short_paths, " 8.0K", " └─┬",), + format_string( + "src/test_dir/many", + true, + short_paths, + " 8.0K", + " └─┬", + ), format_string( "src/test_dir/many/hello_file", true, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index dd1a512..5d634d7 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -50,49 +50,46 @@ pub fn get_dir_tree( fn strip_end_slashes(s: &str) -> String { let mut new_name = String::from(s); - while new_name.chars().last() == Some('/') && new_name.len() != 1 { + while new_name.ends_with('/') && new_name.len() != 1 { new_name.pop(); } new_name } fn examine_dir( - top_dir: &String, + top_dir: &str, apparent_size: bool, inodes: &mut HashSet<(u64, u64)>, data: &mut HashMap, permissions: &mut u64, ) { for entry in WalkDir::new(top_dir) { - match entry { - Ok(e) => { - let maybe_size_and_inode = get_metadata(&e, apparent_size); + if let Ok(e) = entry { + let maybe_size_and_inode = get_metadata(&e, apparent_size); - match maybe_size_and_inode { - Some((size, maybe_inode)) => { - if !apparent_size { - if let Some(inode_dev_pair) = maybe_inode { - if inodes.contains(&inode_dev_pair) { - continue; - } - inodes.insert(inode_dev_pair); + match maybe_size_and_inode { + Some((size, maybe_inode)) => { + if !apparent_size { + if let Some(inode_dev_pair) = maybe_inode { + if inodes.contains(&inode_dev_pair) { + continue; } - } - let mut e_path = e.path().to_path_buf(); - loop { - let path_name = e_path.to_string_lossy().to_string(); - let s = data.entry(path_name.clone()).or_insert(0); - *s += size; - if path_name == *top_dir { - break; - } - e_path.pop(); + inodes.insert(inode_dev_pair); } } - None => *permissions += 1, + let mut e_path = e.path().to_path_buf(); + loop { + let path_name = e_path.to_string_lossy().to_string(); + let s = data.entry(path_name.clone()).or_insert(0); + *s += size; + if path_name == *top_dir { + break; + } + e_path.pop(); + } } + None => *permissions += 1, } - _ => {} } } } diff --git a/src/utils/platform.rs b/src/utils/platform.rs index 9945604..49cba52 100644 --- a/src/utils/platform.rs +++ b/src/utils/platform.rs @@ -9,7 +9,7 @@ fn get_block_size() -> u64 { #[cfg(target_family = "unix")] pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { use std::os::unix::fs::MetadataExt; - d.metadata().ok().map_or(None, |md| { + d.metadata().ok().and_then(|md| { let inode = Some((md.ino(), md.dev())); if use_apparent_size { Some((md.len(), inode))