From d67560c37ac6e2a4443920ced20b788249fba45e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Habl=C3=BCtzel?= Date: Sun, 11 Apr 2021 16:34:19 +0200 Subject: [PATCH] fix clippy for unix --- src/uu/chgrp/src/chgrp.rs | 2 +- src/uu/chmod/src/chmod.rs | 8 ++++---- src/uu/chown/src/chown.rs | 4 ++-- src/uu/chroot/src/chroot.rs | 14 +++++++------- src/uu/install/src/install.rs | 18 +++++++++--------- src/uu/ls/src/ls.rs | 1 + src/uu/pinky/src/pinky.rs | 12 +++--------- src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs | 4 ++-- src/uu/stdbuf/src/stdbuf.rs | 6 ++---- src/uu/touch/src/touch.rs | 2 +- src/uu/tty/src/tty.rs | 4 ++-- src/uucore/src/lib/features/perms.rs | 4 ++-- 12 files changed, 36 insertions(+), 43 deletions(-) diff --git a/src/uu/chgrp/src/chgrp.rs b/src/uu/chgrp/src/chgrp.rs index b4c3360c5..592a0a905 100644 --- a/src/uu/chgrp/src/chgrp.rs +++ b/src/uu/chgrp/src/chgrp.rs @@ -286,7 +286,7 @@ impl Chgrper { ret = match wrap_chgrp(path, &meta, self.dest_gid, follow, self.verbosity.clone()) { Ok(n) => { - if n != "" { + if !n.is_empty() { show_info!("{}", n); } 0 diff --git a/src/uu/chmod/src/chmod.rs b/src/uu/chmod/src/chmod.rs index d9d8c8cf2..dc11be7b8 100644 --- a/src/uu/chmod/src/chmod.rs +++ b/src/uu/chmod/src/chmod.rs @@ -171,13 +171,13 @@ pub fn uumain(args: impl uucore::Args) -> i32 { // of a prefix '-' if it's associated with MODE // e.g. "chmod -v -xw -R FILE" -> "chmod -v xw -R FILE" pub fn strip_minus_from_mode(args: &mut Vec) -> bool { - for i in 0..args.len() { - if args[i].starts_with("-") { - if let Some(second) = args[i].chars().nth(1) { + for arg in args { + if arg.starts_with('-') { + if let Some(second) = arg.chars().nth(1) { match second { 'r' | 'w' | 'x' | 'X' | 's' | 't' | 'u' | 'g' | 'o' | '0'..='7' => { // TODO: use strip_prefix() once minimum rust version reaches 1.45.0 - args[i] = args[i][1..args[i].len()].to_string(); + *arg = arg[1..arg.len()].to_string(); return true; } _ => {} diff --git a/src/uu/chown/src/chown.rs b/src/uu/chown/src/chown.rs index 42010de03..0e3273b3b 100644 --- a/src/uu/chown/src/chown.rs +++ b/src/uu/chown/src/chown.rs @@ -391,7 +391,7 @@ impl Chowner { self.verbosity.clone(), ) { Ok(n) => { - if n != "" { + if !n.is_empty() { show_info!("{}", n); } 0 @@ -446,7 +446,7 @@ impl Chowner { self.verbosity.clone(), ) { Ok(n) => { - if n != "" { + if !n.is_empty() { show_info!("{}", n); } 0 diff --git a/src/uu/chroot/src/chroot.rs b/src/uu/chroot/src/chroot.rs index 44c5dfa02..7e672da1e 100644 --- a/src/uu/chroot/src/chroot.rs +++ b/src/uu/chroot/src/chroot.rs @@ -104,7 +104,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { _ => { let mut vector: Vec<&str> = Vec::new(); for (&k, v) in matches.args.iter() { - vector.push(k.clone()); + vector.push(k); vector.push(&v.vals[0].to_str().unwrap()); } vector @@ -133,7 +133,7 @@ fn set_context(root: &Path, options: &clap::ArgMatches) { let userspec = match userspec_str { Some(ref u) => { let s: Vec<&str> = u.split(':').collect(); - if s.len() != 2 || s.iter().any(|&spec| spec == "") { + if s.len() != 2 || s.iter().any(|&spec| spec.is_empty()) { crash!(1, "invalid userspec: `{}`", u) }; s @@ -142,16 +142,16 @@ fn set_context(root: &Path, options: &clap::ArgMatches) { }; let (user, group) = if userspec.is_empty() { - (&user_str[..], &group_str[..]) + (user_str, group_str) } else { - (&userspec[0][..], &userspec[1][..]) + (userspec[0], userspec[1]) }; enter_chroot(root); - set_groups_from_str(&groups_str[..]); - set_main_group(&group[..]); - set_user(&user[..]); + set_groups_from_str(groups_str); + set_main_group(group); + set_user(user); } fn enter_chroot(root: &Path) { diff --git a/src/uu/install/src/install.rs b/src/uu/install/src/install.rs index e902862a8..a75ce45be 100644 --- a/src/uu/install/src/install.rs +++ b/src/uu/install/src/install.rs @@ -302,7 +302,7 @@ fn behavior(matches: &ArgMatches) -> Result { let specified_mode: Option = if matches.is_present(OPT_MODE) { match matches.value_of(OPT_MODE) { - Some(x) => match mode::parse(&x[..], considering_dir) { + Some(x) => match mode::parse(x, considering_dir) { Ok(y) => Some(y), Err(err) => { show_error!("Invalid mode string: {}", err); @@ -429,7 +429,7 @@ fn standard(paths: Vec, b: Behavior) -> i32 { /// _files_ must all exist as non-directories. /// _target_dir_ must be a directory. /// -fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> i32 { +fn copy_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> i32 { if !target_dir.is_dir() { show_error!("target '{}' is not a directory", target_dir.display()); return 1; @@ -453,7 +453,7 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> continue; } - let mut targetpath = target_dir.clone().to_path_buf(); + let mut targetpath = target_dir.to_path_buf(); let filename = sourcepath.components().last().unwrap(); targetpath.push(filename); @@ -478,7 +478,7 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &PathBuf, b: &Behavior) -> /// _file_ must exist as a non-directory. /// _target_ must be a non-directory /// -fn copy_file_to_file(file: &PathBuf, target: &PathBuf, b: &Behavior) -> i32 { +fn copy_file_to_file(file: &Path, target: &Path, b: &Behavior) -> i32 { if copy(file, &target, b).is_err() { 1 } else { @@ -497,7 +497,7 @@ fn copy_file_to_file(file: &PathBuf, target: &PathBuf, b: &Behavior) -> i32 { /// /// If the copy system call fails, we print a verbose error and return an empty error value. /// -fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { +fn copy(from: &Path, to: &Path, b: &Behavior) -> Result<(), ()> { if b.compare && !need_copy(from, to, b) { return Ok(()); } @@ -556,7 +556,7 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { }; let gid = meta.gid(); match wrap_chown( - to.as_path(), + to, &meta, Some(owner_id), Some(gid), @@ -582,7 +582,7 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { Ok(g) => g, _ => crash!(1, "no such group: {}", b.group), }; - match wrap_chgrp(to.as_path(), &meta, group_id, false, Verbosity::Normal) { + match wrap_chgrp(to, &meta, group_id, false, Verbosity::Normal) { Ok(n) => { if !n.is_empty() { show_info!("{}", n); @@ -601,7 +601,7 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { let modified_time = FileTime::from_last_modification_time(&meta); let accessed_time = FileTime::from_last_access_time(&meta); - match set_file_times(to.as_path(), accessed_time, modified_time) { + match set_file_times(to, accessed_time, modified_time) { Ok(_) => {} Err(e) => show_info!("{}", e), } @@ -630,7 +630,7 @@ fn copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> Result<(), ()> { /// /// Crashes the program if a nonexistent owner or group is specified in _b_. /// -fn need_copy(from: &PathBuf, to: &PathBuf, b: &Behavior) -> bool { +fn need_copy(from: &Path, to: &Path, b: &Behavior) -> bool { let from_meta = match fs::metadata(from) { Ok(meta) => meta, Err(_) => return true, diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index d198f1588..aebaa6b44 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -370,6 +370,7 @@ impl Config { }) .or_else(|| termsize::get().map(|s| s.cols)); + #[allow(clippy::needless_bool)] let show_control = if options.is_present(options::HIDE_CONTROL_CHARS) { false } else if options.is_present(options::SHOW_CONTROL_CHARS) { diff --git a/src/uu/pinky/src/pinky.rs b/src/uu/pinky/src/pinky.rs index 772e311d6..851a3cd42 100644 --- a/src/uu/pinky/src/pinky.rs +++ b/src/uu/pinky/src/pinky.rs @@ -15,7 +15,6 @@ use uucore::utmpx::{self, time, Utmpx}; use std::io::prelude::*; use std::io::BufReader; -use std::io::Result as IOResult; use std::fs::File; use std::os::unix::fs::MetadataExt; @@ -136,12 +135,8 @@ The utmp file will be {}", }; if do_short_format { - if let Err(e) = pk.short_pinky() { - show_usage_error!("{}", e); - 1 - } else { - 0 - } + pk.short_pinky(); + 0 } else { pk.long_pinky() } @@ -282,7 +277,7 @@ impl Pinky { println!(); } - fn short_pinky(&self) -> IOResult<()> { + fn short_pinky(&self) { if self.include_heading { self.print_heading(); } @@ -295,7 +290,6 @@ impl Pinky { } } } - Ok(()) } fn long_pinky(&self) -> i32 { diff --git a/src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs b/src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs index fa36d4ab5..d08427d98 100644 --- a/src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs +++ b/src/uu/stdbuf/src/libstdbuf/src/libstdbuf.rs @@ -35,8 +35,8 @@ extern "C" { fn set_buffer(stream: *mut FILE, value: &str) { let (mode, size): (c_int, size_t) = match value { - "0" => (_IONBF, 0 as size_t), - "L" => (_IOLBF, 0 as size_t), + "0" => (_IONBF, 0_usize), + "L" => (_IOLBF, 0_usize), input => { let buff_size: usize = match input.parse() { Ok(num) => num, diff --git a/src/uu/stdbuf/src/stdbuf.rs b/src/uu/stdbuf/src/stdbuf.rs index a6c9f9dc5..8c65a5c7e 100644 --- a/src/uu/stdbuf/src/stdbuf.rs +++ b/src/uu/stdbuf/src/stdbuf.rs @@ -141,12 +141,10 @@ fn parse_size(size: &str) -> Option { fn check_option(matches: &ArgMatches, name: &str) -> Result { match matches.value_of(name) { - Some(value) => match &value[..] { + Some(value) => match value { "L" => { if name == options::INPUT { - Err(ProgramOptionsError(format!( - "line buffering stdin is meaningless" - ))) + Err(ProgramOptionsError("line buffering stdin is meaningless".to_string())) } else { Ok(BufferType::Line) } diff --git a/src/uu/touch/src/touch.rs b/src/uu/touch/src/touch.rs index 39405900e..f0c3c12d2 100644 --- a/src/uu/touch/src/touch.rs +++ b/src/uu/touch/src/touch.rs @@ -137,7 +137,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let (mut atime, mut mtime) = if matches.is_present(options::sources::REFERENCE) { stat( - &matches.value_of(options::sources::REFERENCE).unwrap()[..], + matches.value_of(options::sources::REFERENCE).unwrap(), !matches.is_present(options::NO_DEREF), ) } else if matches.is_present(options::sources::DATE) diff --git a/src/uu/tty/src/tty.rs b/src/uu/tty/src/tty.rs index 18d69db46..815c6f96b 100644 --- a/src/uu/tty/src/tty.rs +++ b/src/uu/tty/src/tty.rs @@ -65,9 +65,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { } } - return if is_stdin_interactive() { + if is_stdin_interactive() { libc::EXIT_SUCCESS } else { libc::EXIT_FAILURE - }; + } } diff --git a/src/uucore/src/lib/features/perms.rs b/src/uucore/src/lib/features/perms.rs index 66db15451..36f56206d 100644 --- a/src/uucore/src/lib/features/perms.rs +++ b/src/uucore/src/lib/features/perms.rs @@ -31,9 +31,9 @@ fn chgrp>(path: P, dgid: gid_t, follow: bool) -> IOResult<()> { let s = CString::new(path.as_os_str().as_bytes()).unwrap(); let ret = unsafe { if follow { - libc::chown(s.as_ptr(), (0 as gid_t).wrapping_sub(1), dgid) + libc::chown(s.as_ptr(), 0_u32.wrapping_sub(1), dgid) } else { - lchown(s.as_ptr(), (0 as gid_t).wrapping_sub(1), dgid) + lchown(s.as_ptr(), 0_u32.wrapping_sub(1), dgid) } }; if ret == 0 {