Skip to content

Commit 840ddaf

Browse files
committed
running clippy linter on source for ls
ok
1 parent 39b5760 commit 840ddaf

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

src/ls/ls.rs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
//
1010

1111
extern crate getopts;
12+
extern crate number_prefix;
1213
extern crate term_grid;
1314
extern crate termsize;
1415
extern crate time;
1516
extern crate unicode_width;
16-
extern crate number_prefix;
17-
use number_prefix::{Standalone, Prefixed, decimal_prefix};
17+
use number_prefix::{decimal_prefix, Prefixed, Standalone};
1818
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
1919
use time::{strftime, Timespec};
2020

@@ -25,20 +25,19 @@ extern crate lazy_static;
2525
#[macro_use]
2626
extern crate uucore;
2727
#[cfg(unix)]
28-
use uucore::libc::{mode_t, S_ISGID, S_ISUID, S_ISVTX, S_IWOTH,
29-
S_IXGRP, S_IXOTH, S_IXUSR};
28+
use uucore::libc::{mode_t, S_ISGID, S_ISUID, S_ISVTX, S_IWOTH, S_IXGRP, S_IXOTH, S_IXUSR};
3029

31-
use std::fs;
32-
use std::fs::{DirEntry, FileType, Metadata};
33-
use std::path::{Path, PathBuf};
3430
use std::cmp::Reverse;
3531
#[cfg(unix)]
3632
use std::collections::HashMap;
33+
use std::fs;
34+
use std::fs::{DirEntry, FileType, Metadata};
35+
use std::path::{Path, PathBuf};
3736

38-
#[cfg(any(unix, target_os = "redox"))]
39-
use std::os::unix::fs::MetadataExt;
4037
#[cfg(unix)]
4138
use std::os::unix::fs::FileTypeExt;
39+
#[cfg(any(unix, target_os = "redox"))]
40+
use std::os::unix::fs::MetadataExt;
4241
#[cfg(unix)]
4342
use unicode_width::UnicodeWidthStr;
4443

@@ -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
5857

5958
#[cfg(unix)]
6059
lazy_static! {
61-
static ref LS_COLORS: String = std::env::var("LS_COLORS").unwrap_or(DEFAULT_COLORS.to_string());
60+
static ref LS_COLORS: String =
61+
std::env::var("LS_COLORS").unwrap_or_else(|_| DEFAULT_COLORS.to_string());
6262
static ref COLOR_MAP: HashMap<&'static str, &'static str> = {
63-
let codes = LS_COLORS.split(":");
63+
let codes = LS_COLORS.split(':');
6464
let mut map = HashMap::new();
6565
for c in codes {
66-
let p: Vec<_> = c.split("=").collect();
66+
let p: Vec<_> = c.split('=').collect();
6767
if p.len() == 2 {
6868
map.insert(p[0], p[1]);
6969
}
@@ -169,7 +169,7 @@ fn list(options: getopts::Matches) {
169169
let locs: Vec<String> = if options.free.is_empty() {
170170
vec![String::from(".")]
171171
} else {
172-
options.free.iter().cloned().collect()
172+
options.free.to_vec()
173173
};
174174

175175
let mut files = Vec::<PathBuf>::new();
@@ -275,20 +275,18 @@ fn max(lhs: usize, rhs: usize) -> usize {
275275
fn should_display(entry: &DirEntry, options: &getopts::Matches) -> bool {
276276
let ffi_name = entry.file_name();
277277
let name = ffi_name.to_string_lossy();
278-
if !options.opt_present("a") && !options.opt_present("A") {
279-
if name.starts_with('.') {
280-
return false;
281-
}
278+
if !options.opt_present("a") && !options.opt_present("A") && name.starts_with('.') {
279+
return false;
282280
}
283281
if options.opt_present("B") && name.ends_with('~') {
284282
return false;
285283
}
286-
return true;
284+
true
287285
}
288286

289287
fn enter_directory(dir: &PathBuf, options: &getopts::Matches) {
290-
let mut entries =
291-
safe_unwrap!(fs::read_dir(dir).and_then(|e| e.collect::<Result<Vec<_>, _>>()));
288+
let mut entries: std::vec::Vec<std::fs::DirEntry> =
289+
safe_unwrap!(fs::read_dir(dir).and_then(std::iter::Iterator::collect));
292290

293291
entries.retain(|e| should_display(e, options));
294292

@@ -314,7 +312,7 @@ fn enter_directory(dir: &PathBuf, options: &getopts::Matches) {
314312

315313
fn get_metadata(entry: &PathBuf, options: &getopts::Matches) -> std::io::Result<Metadata> {
316314
if options.opt_present("L") {
317-
entry.metadata().or(entry.symlink_metadata())
315+
entry.metadata().or_else(|_| entry.symlink_metadata())
318316
} else {
319317
entry.symlink_metadata()
320318
}
@@ -341,7 +339,7 @@ fn pad_left(string: String, count: usize) -> String {
341339
}
342340
}
343341

344-
fn display_items(items: &Vec<PathBuf>, strip: Option<&Path>, options: &getopts::Matches) {
342+
fn display_items(items: &[PathBuf], strip: Option<&Path>, options: &getopts::Matches) {
345343
if options.opt_present("long") || options.opt_present("numeric-uid-gid") {
346344
let (mut max_links, mut max_size) = (1, 1);
347345
for item in items {
@@ -354,19 +352,17 @@ fn display_items(items: &Vec<PathBuf>, strip: Option<&Path>, options: &getopts::
354352
}
355353
} else {
356354
if !options.opt_present("1") {
357-
let names = items
358-
.iter()
359-
.filter_map(|i| {
360-
let md = get_metadata(i, options);
361-
match md {
362-
Err(e) => {
363-
let filename = get_file_name(i, strip);
364-
show_error!("{}: {}", filename, e);
365-
None
366-
}
367-
Ok(md) => Some(display_file_name(&i, strip, &md, options)),
355+
let names = items.iter().filter_map(|i| {
356+
let md = get_metadata(i, options);
357+
match md {
358+
Err(e) => {
359+
let filename = get_file_name(i, strip);
360+
show_error!("{}: {}", filename, e);
361+
None
368362
}
369-
});
363+
Ok(md) => Some(display_file_name(&i, strip, &md, options)),
364+
}
365+
});
370366

371367
if let Some(size) = termsize::get() {
372368
let mut grid = Grid::new(GridOptions {
@@ -452,7 +448,7 @@ fn display_uname(metadata: &Metadata, options: &getopts::Matches) -> String {
452448
if options.opt_present("numeric-uid-gid") {
453449
metadata.uid().to_string()
454450
} else {
455-
entries::uid2usr(metadata.uid()).unwrap_or(metadata.uid().to_string())
451+
entries::uid2usr(metadata.uid()).unwrap_or_else(|_| metadata.uid().to_string())
456452
}
457453
}
458454

@@ -461,7 +457,7 @@ fn display_group(metadata: &Metadata, options: &getopts::Matches) -> String {
461457
if options.opt_present("numeric-uid-gid") {
462458
metadata.gid().to_string()
463459
} else {
464-
entries::gid2grp(metadata.gid()).unwrap_or(metadata.gid().to_string())
460+
entries::gid2grp(metadata.gid()).unwrap_or_else(|_| metadata.gid().to_string())
465461
}
466462
}
467463

@@ -509,7 +505,7 @@ fn display_file_size(metadata: &Metadata, options: &getopts::Matches) -> String
509505
if options.opt_present("human-readable") {
510506
match decimal_prefix(metadata.len() as f64) {
511507
Standalone(bytes) => bytes.to_string(),
512-
Prefixed(prefix, bytes) => format!("{:.2}{}", bytes, prefix).to_uppercase()
508+
Prefixed(prefix, bytes) => format!("{:.2}{}", bytes, prefix).to_uppercase(),
513509
}
514510
} else {
515511
metadata.len().to_string()
@@ -531,7 +527,7 @@ fn get_file_name(name: &Path, strip: Option<&Path>) -> String {
531527
Some(prefix) => name.strip_prefix(prefix).unwrap_or(name),
532528
None => name,
533529
};
534-
if name.as_os_str().len() == 0 {
530+
if name.as_os_str().is_empty() {
535531
name = Path::new(".");
536532
}
537533
name.to_string_lossy().into_owned()
@@ -593,9 +589,9 @@ fn color_name(name: String, typ: &str) -> String {
593589

594590
#[cfg(unix)]
595591
macro_rules! has {
596-
($mode:expr, $perm:expr) => (
592+
($mode:expr, $perm:expr) => {
597593
$mode & ($perm as mode_t) != 0
598-
)
594+
};
599595
}
600596

601597
#[cfg(unix)]
@@ -688,7 +684,7 @@ fn display_file_name(
688684

689685
Cell {
690686
contents: name,
691-
width: width,
687+
width,
692688
}
693689
}
694690

0 commit comments

Comments
 (0)