From 840ddaffac9da016ee28a91e06c52f5dce4bd955 Mon Sep 17 00:00:00 2001 From: John Simon Date: Sat, 16 Mar 2019 16:55:58 -0700 Subject: [PATCH] running clippy linter on source for ls ok --- src/ls/ls.rs | 78 +++++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/src/ls/ls.rs b/src/ls/ls.rs index 658be8d2cb6..7acbe8f727f 100644 --- a/src/ls/ls.rs +++ b/src/ls/ls.rs @@ -9,12 +9,12 @@ // extern crate getopts; +extern crate number_prefix; extern crate term_grid; extern crate termsize; extern crate time; extern crate unicode_width; -extern crate number_prefix; -use number_prefix::{Standalone, Prefixed, decimal_prefix}; +use number_prefix::{decimal_prefix, Prefixed, Standalone}; use term_grid::{Cell, Direction, Filling, Grid, GridOptions}; use time::{strftime, Timespec}; @@ -25,20 +25,19 @@ extern crate lazy_static; #[macro_use] extern crate uucore; #[cfg(unix)] -use uucore::libc::{mode_t, S_ISGID, S_ISUID, S_ISVTX, S_IWOTH, - S_IXGRP, S_IXOTH, S_IXUSR}; +use uucore::libc::{mode_t, S_ISGID, S_ISUID, S_ISVTX, S_IWOTH, S_IXGRP, S_IXOTH, S_IXUSR}; -use std::fs; -use std::fs::{DirEntry, FileType, Metadata}; -use std::path::{Path, PathBuf}; use std::cmp::Reverse; #[cfg(unix)] use std::collections::HashMap; +use std::fs; +use std::fs::{DirEntry, FileType, Metadata}; +use std::path::{Path, PathBuf}; -#[cfg(any(unix, target_os = "redox"))] -use std::os::unix::fs::MetadataExt; #[cfg(unix)] use std::os::unix::fs::FileTypeExt; +#[cfg(any(unix, target_os = "redox"))] +use std::os::unix::fs::MetadataExt; #[cfg(unix)] use unicode_width::UnicodeWidthStr; @@ -58,12 +57,13 @@ static DEFAULT_COLORS: &str = "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do #[cfg(unix)] lazy_static! { - static ref LS_COLORS: String = std::env::var("LS_COLORS").unwrap_or(DEFAULT_COLORS.to_string()); + static ref LS_COLORS: String = + std::env::var("LS_COLORS").unwrap_or_else(|_| DEFAULT_COLORS.to_string()); static ref COLOR_MAP: HashMap<&'static str, &'static str> = { - let codes = LS_COLORS.split(":"); + let codes = LS_COLORS.split(':'); let mut map = HashMap::new(); for c in codes { - let p: Vec<_> = c.split("=").collect(); + let p: Vec<_> = c.split('=').collect(); if p.len() == 2 { map.insert(p[0], p[1]); } @@ -169,7 +169,7 @@ fn list(options: getopts::Matches) { let locs: Vec = if options.free.is_empty() { vec![String::from(".")] } else { - options.free.iter().cloned().collect() + options.free.to_vec() }; let mut files = Vec::::new(); @@ -275,20 +275,18 @@ fn max(lhs: usize, rhs: usize) -> usize { fn should_display(entry: &DirEntry, options: &getopts::Matches) -> bool { let ffi_name = entry.file_name(); let name = ffi_name.to_string_lossy(); - if !options.opt_present("a") && !options.opt_present("A") { - if name.starts_with('.') { - return false; - } + if !options.opt_present("a") && !options.opt_present("A") && name.starts_with('.') { + return false; } if options.opt_present("B") && name.ends_with('~') { return false; } - return true; + true } fn enter_directory(dir: &PathBuf, options: &getopts::Matches) { - let mut entries = - safe_unwrap!(fs::read_dir(dir).and_then(|e| e.collect::, _>>())); + let mut entries: std::vec::Vec = + safe_unwrap!(fs::read_dir(dir).and_then(std::iter::Iterator::collect)); entries.retain(|e| should_display(e, options)); @@ -314,7 +312,7 @@ fn enter_directory(dir: &PathBuf, options: &getopts::Matches) { fn get_metadata(entry: &PathBuf, options: &getopts::Matches) -> std::io::Result { if options.opt_present("L") { - entry.metadata().or(entry.symlink_metadata()) + entry.metadata().or_else(|_| entry.symlink_metadata()) } else { entry.symlink_metadata() } @@ -341,7 +339,7 @@ fn pad_left(string: String, count: usize) -> String { } } -fn display_items(items: &Vec, strip: Option<&Path>, options: &getopts::Matches) { +fn display_items(items: &[PathBuf], strip: Option<&Path>, options: &getopts::Matches) { if options.opt_present("long") || options.opt_present("numeric-uid-gid") { let (mut max_links, mut max_size) = (1, 1); for item in items { @@ -354,19 +352,17 @@ fn display_items(items: &Vec, strip: Option<&Path>, options: &getopts:: } } else { if !options.opt_present("1") { - let names = items - .iter() - .filter_map(|i| { - let md = get_metadata(i, options); - match md { - Err(e) => { - let filename = get_file_name(i, strip); - show_error!("{}: {}", filename, e); - None - } - Ok(md) => Some(display_file_name(&i, strip, &md, options)), + let names = items.iter().filter_map(|i| { + let md = get_metadata(i, options); + match md { + Err(e) => { + let filename = get_file_name(i, strip); + show_error!("{}: {}", filename, e); + None } - }); + Ok(md) => Some(display_file_name(&i, strip, &md, options)), + } + }); if let Some(size) = termsize::get() { let mut grid = Grid::new(GridOptions { @@ -452,7 +448,7 @@ fn display_uname(metadata: &Metadata, options: &getopts::Matches) -> String { if options.opt_present("numeric-uid-gid") { metadata.uid().to_string() } else { - entries::uid2usr(metadata.uid()).unwrap_or(metadata.uid().to_string()) + entries::uid2usr(metadata.uid()).unwrap_or_else(|_| metadata.uid().to_string()) } } @@ -461,7 +457,7 @@ fn display_group(metadata: &Metadata, options: &getopts::Matches) -> String { if options.opt_present("numeric-uid-gid") { metadata.gid().to_string() } else { - entries::gid2grp(metadata.gid()).unwrap_or(metadata.gid().to_string()) + entries::gid2grp(metadata.gid()).unwrap_or_else(|_| metadata.gid().to_string()) } } @@ -509,7 +505,7 @@ fn display_file_size(metadata: &Metadata, options: &getopts::Matches) -> String if options.opt_present("human-readable") { match decimal_prefix(metadata.len() as f64) { Standalone(bytes) => bytes.to_string(), - Prefixed(prefix, bytes) => format!("{:.2}{}", bytes, prefix).to_uppercase() + Prefixed(prefix, bytes) => format!("{:.2}{}", bytes, prefix).to_uppercase(), } } else { metadata.len().to_string() @@ -531,7 +527,7 @@ fn get_file_name(name: &Path, strip: Option<&Path>) -> String { Some(prefix) => name.strip_prefix(prefix).unwrap_or(name), None => name, }; - if name.as_os_str().len() == 0 { + if name.as_os_str().is_empty() { name = Path::new("."); } name.to_string_lossy().into_owned() @@ -593,9 +589,9 @@ fn color_name(name: String, typ: &str) -> String { #[cfg(unix)] macro_rules! has { - ($mode:expr, $perm:expr) => ( + ($mode:expr, $perm:expr) => { $mode & ($perm as mode_t) != 0 - ) + }; } #[cfg(unix)] @@ -688,7 +684,7 @@ fn display_file_name( Cell { contents: name, - width: width, + width, } }