diff --git a/.cargo/config b/.cargo/config index 58e1381b1..0a8fd3d00 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,2 +1,11 @@ [target.x86_64-unknown-redox] linker = "x86_64-unknown-redox-gcc" + +[target.'cfg(feature = "cargo-clippy")'] +rustflags = [ + "-Wclippy::use_self", + "-Wclippy::needless_pass_by_value", + "-Wclippy::semicolon_if_nothing_returned", + "-Wclippy::single_char_pattern", + "-Wclippy::explicit_iter_loop", +] diff --git a/src/bin/coreutils.rs b/src/bin/coreutils.rs index 901139edc..fcd86c93f 100644 --- a/src/bin/coreutils.rs +++ b/src/bin/coreutils.rs @@ -87,7 +87,7 @@ fn main() { }; if util == "completion" { - gen_completions(args, utils); + gen_completions(args, &utils); } match utils.get(util) { @@ -132,7 +132,7 @@ fn main() { /// Prints completions for the utility in the first parameter for the shell in the second parameter to stdout fn gen_completions( args: impl Iterator, - util_map: UtilityMap, + util_map: &UtilityMap, ) -> ! { let all_utilities: Vec<_> = std::iter::once("coreutils") .chain(util_map.keys().copied()) @@ -168,9 +168,9 @@ fn gen_completions( process::exit(0); } -fn gen_coreutils_app(util_map: UtilityMap) -> App<'static> { +fn gen_coreutils_app(util_map: &UtilityMap) -> App<'static> { let mut app = App::new("coreutils"); - for (_, (_, sub_app)) in &util_map { + for (_, (_, sub_app)) in util_map { app = app.subcommand(sub_app()); } app diff --git a/src/uu/chroot/src/chroot.rs b/src/uu/chroot/src/chroot.rs index 179880b14..f656ed77d 100644 --- a/src/uu/chroot/src/chroot.rs +++ b/src/uu/chroot/src/chroot.rs @@ -201,12 +201,12 @@ fn set_main_group(group: &str) -> UResult<()> { } #[cfg(any(target_vendor = "apple", target_os = "freebsd"))] -fn set_groups(groups: Vec) -> libc::c_int { +fn set_groups(groups: &[libc::gid_t]) -> libc::c_int { unsafe { setgroups(groups.len() as libc::c_int, groups.as_ptr()) } } #[cfg(target_os = "linux")] -fn set_groups(groups: Vec) -> libc::c_int { +fn set_groups(groups: &[libc::gid_t]) -> libc::c_int { unsafe { setgroups(groups.len() as libc::size_t, groups.as_ptr()) } } @@ -220,7 +220,7 @@ fn set_groups_from_str(groups: &str) -> UResult<()> { }; groups_vec.push(gid); } - let err = set_groups(groups_vec); + let err = set_groups(&groups_vec); if err != 0 { return Err(ChrootError::SetGroupsFailed(Error::last_os_error()).into()); } diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 7629aba69..e75210ef5 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -144,7 +144,7 @@ impl Stat { #[cfg(windows)] let file_info = get_file_info(&path); #[cfg(windows)] - Ok(Stat { + Ok(Self { path, is_dir: metadata.is_dir(), size: metadata.len(), diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 677ffe1bf..c0e94a578 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -104,6 +104,7 @@ fn load_config_file(opts: &mut Options) -> UResult<()> { } #[cfg(not(windows))] +#[allow(clippy::ptr_arg)] fn build_command<'a, 'b>(args: &'a mut Vec<&'b str>) -> (Cow<'b, str>, &'a [&'b str]) { let progname = Cow::from(args[0]); (progname, &args[1..]) diff --git a/src/uu/kill/src/kill.rs b/src/uu/kill/src/kill.rs index 02dc91dbf..413a183a8 100644 --- a/src/uu/kill/src/kill.rs +++ b/src/uu/kill/src/kill.rs @@ -74,7 +74,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { table(); Ok(()) } - Mode::List => list(pids_or_signals.get(0).cloned()), + Mode::List => list(pids_or_signals.get(0)), } } @@ -168,9 +168,9 @@ fn print_signals() { println!(); } -fn list(arg: Option) -> UResult<()> { +fn list(arg: Option<&String>) -> UResult<()> { match arg { - Some(ref x) => print_signal(x), + Some(x) => print_signal(x), None => { print_signals(); Ok(()) diff --git a/src/uu/split/src/number.rs b/src/uu/split/src/number.rs index b2c402716..ef3ccbc4b 100644 --- a/src/uu/split/src/number.rs +++ b/src/uu/split/src/number.rs @@ -96,8 +96,8 @@ impl Number { #[allow(dead_code)] fn digits(&self) -> &Vec { match self { - Number::FixedWidth(number) => &number.digits, - Number::DynamicWidth(number) => &number.digits, + Self::FixedWidth(number) => &number.digits, + Self::DynamicWidth(number) => &number.digits, } } @@ -136,8 +136,8 @@ impl Number { /// ``` pub fn increment(&mut self) -> Result<(), Overflow> { match self { - Number::FixedWidth(number) => number.increment(), - Number::DynamicWidth(number) => number.increment(), + Self::FixedWidth(number) => number.increment(), + Self::DynamicWidth(number) => number.increment(), } } } @@ -145,8 +145,8 @@ impl Number { impl Display for Number { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { - Number::FixedWidth(number) => number.fmt(f), - Number::DynamicWidth(number) => number.fmt(f), + Self::FixedWidth(number) => number.fmt(f), + Self::DynamicWidth(number) => number.fmt(f), } } } @@ -183,8 +183,8 @@ pub struct FixedWidthNumber { impl FixedWidthNumber { /// Instantiate a number of the given radix and width. - pub fn new(radix: u8, width: usize) -> FixedWidthNumber { - FixedWidthNumber { + pub fn new(radix: u8, width: usize) -> Self { + Self { radix, digits: vec![0; width], } @@ -286,8 +286,8 @@ impl DynamicWidthNumber { /// /// This associated function returns a new instance of the struct /// with the given radix and a width of two digits, both 0. - pub fn new(radix: u8) -> DynamicWidthNumber { - DynamicWidthNumber { + pub fn new(radix: u8) -> Self { + Self { radix, digits: vec![0, 0], } @@ -404,7 +404,7 @@ mod tests { fn num(n: usize) -> Number { let mut number = Number::DynamicWidth(DynamicWidthNumber::new(26)); for _ in 0..n { - number.increment().unwrap() + number.increment().unwrap(); } number } @@ -428,7 +428,7 @@ mod tests { fn num(n: usize) -> Number { let mut number = Number::DynamicWidth(DynamicWidthNumber::new(10)); for _ in 0..n { - number.increment().unwrap() + number.increment().unwrap(); } number } diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index 23eb24768..a05959810 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -62,7 +62,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .override_usage(&usage[..]) .after_help(&long_usage[..]) .get_matches_from(args); - let settings = Settings::from(matches)?; + let settings = Settings::from(&matches)?; split(&settings) } @@ -232,7 +232,7 @@ struct Settings { impl Settings { /// Parse a strategy from the command-line arguments. - fn from(matches: ArgMatches) -> UResult { + fn from(matches: &ArgMatches) -> UResult { let result = Self { suffix_length: matches .value_of(OPT_SUFFIX_LENGTH) @@ -242,7 +242,7 @@ impl Settings { numeric_suffix: matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0, additional_suffix: matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_owned(), verbose: matches.occurrences_of("verbose") > 0, - strategy: Strategy::from(&matches)?, + strategy: Strategy::from(matches)?, input: matches.value_of(ARG_INPUT).unwrap().to_owned(), prefix: matches.value_of(ARG_PREFIX).unwrap().to_owned(), filter: matches.value_of(OPT_FILTER).map(|s| s.to_owned()), diff --git a/src/uu/tail/src/platform/unix.rs b/src/uu/tail/src/platform/unix.rs index e7f75c31e..e01d5e444 100644 --- a/src/uu/tail/src/platform/unix.rs +++ b/src/uu/tail/src/platform/unix.rs @@ -30,6 +30,7 @@ impl ProcessChecker { } // Borrowing mutably to be aligned with Windows implementation + #[allow(clippy::wrong_self_convention)] pub fn is_dead(&mut self) -> bool { unsafe { libc::kill(self.pid, 0) != 0 && get_errno() != libc::EPERM } } diff --git a/src/uu/tail/src/platform/windows.rs b/src/uu/tail/src/platform/windows.rs index 7faa872e6..c63040a2a 100644 --- a/src/uu/tail/src/platform/windows.rs +++ b/src/uu/tail/src/platform/windows.rs @@ -24,11 +24,11 @@ pub struct ProcessChecker { } impl ProcessChecker { - pub fn new(process_id: self::Pid) -> ProcessChecker { + pub fn new(process_id: self::Pid) -> Self { #[allow(non_snake_case)] let FALSE = 0i32; let h = unsafe { OpenProcess(SYNCHRONIZE, FALSE, process_id as DWORD) }; - ProcessChecker { + Self { dead: h.is_null(), handle: h, } diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index d1e623757..a3b05dff8 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -238,7 +238,7 @@ impl MountInfo { } } #[cfg(windows)] - fn new(mut volume_name: String) -> Option { + fn new(mut volume_name: String) -> Option { let mut dev_name_buf = [0u16; MAX_PATH]; volume_name.pop(); unsafe { @@ -289,7 +289,7 @@ impl MountInfo { } else { None }; - let mut mn_info = MountInfo { + let mut mn_info = Self { dev_id: volume_name, dev_name, fs_type: fs_type.unwrap_or_else(|| "".to_string()), @@ -319,7 +319,7 @@ use std::ffi::CStr; ))] impl From for MountInfo { fn from(statfs: StatFs) -> Self { - let mut info = MountInfo { + let mut info = Self { dev_id: "".to_string(), dev_name: unsafe { // spell-checker:disable-next-line @@ -553,7 +553,7 @@ impl FsUsage { } let bytes_per_cluster = sectors_per_cluster as u64 * bytes_per_sector as u64; - FsUsage { + Self { // f_bsize File system block size. blocksize: bytes_per_cluster as u64, // f_blocks - Total number of blocks on the file system, in units of f_frsize. diff --git a/src/uucore/src/lib/features/wide.rs b/src/uucore/src/lib/features/wide.rs index 49ce575a7..6b9368f50 100644 --- a/src/uucore/src/lib/features/wide.rs +++ b/src/uucore/src/lib/features/wide.rs @@ -27,10 +27,10 @@ pub trait FromWide { fn from_wide_null(wide: &[u16]) -> Self; } impl FromWide for String { - fn from_wide(wide: &[u16]) -> String { + fn from_wide(wide: &[u16]) -> Self { OsString::from_wide(wide).to_string_lossy().into_owned() } - fn from_wide_null(wide: &[u16]) -> String { + fn from_wide_null(wide: &[u16]) -> Self { let len = wide.iter().take_while(|&&c| c != 0).count(); OsString::from_wide(&wide[..len]) .to_string_lossy() diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index efc097773..b0506d071 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -179,15 +179,15 @@ fn test_du_hard_link() { #[cfg(target_vendor = "apple")] fn _du_hard_link(s: &str) { - assert_eq!(s, "12\tsubdir/links\n") + assert_eq!(s, "12\tsubdir/links\n"); } #[cfg(target_os = "windows")] fn _du_hard_link(s: &str) { - assert_eq!(s, "8\tsubdir/links\n") + assert_eq!(s, "8\tsubdir/links\n"); } #[cfg(target_os = "freebsd")] fn _du_hard_link(s: &str) { - assert_eq!(s, "16\tsubdir/links\n") + assert_eq!(s, "16\tsubdir/links\n"); } #[cfg(all( not(target_vendor = "apple"), diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index e1950f0df..3559e74cd 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -219,7 +219,7 @@ fn test_change_directory() { .args(&pwd) .succeeds() .stdout_move_str(); - assert_eq!(out.trim(), temporary_path.as_os_str()) + assert_eq!(out.trim(), temporary_path.as_os_str()); } #[test]