diff --git a/bitflags-mini/src/lib.rs b/bitflags-mini/src/lib.rs index 79a4a0b..85032e7 100644 --- a/bitflags-mini/src/lib.rs +++ b/bitflags-mini/src/lib.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] //! Minimal bitflags type implementation allowing to check if a set of flags //! contains a specific flag. The type must implement Copy and Bitand with u32. use core::ops::BitAnd; diff --git a/corebox/commands/base32/mod.rs b/corebox/commands/base32/mod.rs index 8936354..4f53bea 100644 --- a/corebox/commands/base32/mod.rs +++ b/corebox/commands/base32/mod.rs @@ -47,7 +47,7 @@ impl Cmd for Base32 { } else if index > 0 { println!(); } - let contents = get_contents(&file)?; + let contents = get_contents(file)?; if matches.get_flag("DECODE") { decode_base32(contents, matches.get_flag("IGNORE"))?; } else { diff --git a/corebox/commands/base64/mod.rs b/corebox/commands/base64/mod.rs index 95b01e7..8a60454 100644 --- a/corebox/commands/base64/mod.rs +++ b/corebox/commands/base64/mod.rs @@ -46,7 +46,7 @@ impl Cmd for Base64 { } else if index > 0 { println!(); } - let contents = get_contents(&file)?; + let contents = get_contents(file)?; if matches.get_flag("DECODE") { decode_base64(contents, matches.get_flag("IGNORE"))?; } else { diff --git a/corebox/commands/bootstrap/mod.rs b/corebox/commands/bootstrap/mod.rs index 2901191..3f688ef 100644 --- a/corebox/commands/bootstrap/mod.rs +++ b/corebox/commands/bootstrap/mod.rs @@ -348,7 +348,7 @@ fn completions(prefix: &str, matches: &ArgMatches, usr: bool) -> Result<(), io:: })?; } if matches.get_flag("zsh") || matches.get_flag("all") { - let mut outdir = basedir.clone(); + let mut outdir = basedir; outdir.push("zsh"); outdir.push("site-functions"); COMMANDS.iter().try_for_each(|cmd| { diff --git a/corebox/commands/chmod/mod.rs b/corebox/commands/chmod/mod.rs index 2c4d799..23835fe 100644 --- a/corebox/commands/chmod/mod.rs +++ b/corebox/commands/chmod/mod.rs @@ -59,15 +59,13 @@ impl Cmd for Chmod { }; if let Err(e) = action.apply() { if !matches.get_flag("quiet") { - return Err(e.into()); + return Err(e); } } - if matches.get_flag("recursive") { - if action.path.is_dir() { - if let Err(e) = action.recurse() { - if !matches.get_flag("quiet") { - return Err(e.into()); - } + if matches.get_flag("recursive") && action.path.is_dir() { + if let Err(e) = action.recurse() { + if !matches.get_flag("quiet") { + return Err(e); } } } @@ -136,7 +134,7 @@ impl Action<'_> { Ok(()) } - fn into_child(&self, entry: DirEntry) -> Self { + fn get_child(&self, entry: DirEntry) -> Self { Self { path: entry.path().to_path_buf(), feedback: self.feedback, @@ -148,7 +146,7 @@ impl Action<'_> { let walker = WalkDir::new(&self.path).max_open(1); for entry in walker { let entry = entry?; - let action = self.into_child(entry); + let action = self.get_child(entry); action.apply()?; } Ok(()) diff --git a/corebox/commands/chown/chgrp.rs b/corebox/commands/chown/chgrp.rs index ba4543c..5e32d9e 100644 --- a/corebox/commands/chown/chgrp.rs +++ b/corebox/commands/chown/chgrp.rs @@ -56,12 +56,11 @@ impl Cmd for Chgrp { feedback, }; if let Some(r) = recurse { - if action.path.is_dir() { + if action.path.is_dir() + && action.path.is_symlink() + && r.traversal != Traversal::NoLinks + { action.recurse(recurse)?; - } else if action.path.is_symlink() { - if r.traversal != Traversal::NoLinks { - action.recurse(recurse)?; - } } } action.apply()?; @@ -127,7 +126,7 @@ impl Action<'_> { println!("{} retained as {}", &self.path.display(), &self.group.name); } - fn into_child(&self, entry: DirEntry) -> Result> { + fn as_child(&self, entry: DirEntry) -> Result> { let path = entry.path().to_path_buf(); Ok(Self { path, @@ -140,13 +139,10 @@ impl Action<'_> { let walker = WalkDir::new(&self.path) .max_open(1) .same_file_system(recurse.map_or(false, |x| x.same_filesystem)) - .follow_links(recurse.map_or(false, |x| match x.traversal { - Traversal::FullLinks => true, - _ => false, - })); + .follow_links(recurse.map_or(false, |x| matches!(x.traversal, Traversal::FullLinks))); for entry in walker { let entry = entry?; - let action = self.into_child(entry)?; + let action = self.as_child(entry)?; action.apply()?; } Ok(()) diff --git a/corebox/commands/chown/mod.rs b/corebox/commands/chown/mod.rs index 84c666b..fd7859b 100644 --- a/corebox/commands/chown/mod.rs +++ b/corebox/commands/chown/mod.rs @@ -68,12 +68,11 @@ impl Cmd for Chown { feedback, }; if let Some(r) = recurse { - if action.path.is_dir() { + if action.path.is_dir() + && action.path.is_symlink() + && r.traversal != Traversal::NoLinks + { action.recurse(recurse)?; - } else if action.path.is_symlink() { - if r.traversal != Traversal::NoLinks { - action.recurse(recurse)?; - } } } action.apply()?; @@ -119,6 +118,7 @@ fn args() -> [Arg; 8] { } #[derive(Clone, Copy, Debug, PartialEq)] +#[allow(clippy::enum_variant_names)] enum Traversal { CliLinks, FullLinks, @@ -245,7 +245,7 @@ impl Action<'_> { } } - fn into_child(&self, entry: DirEntry) -> Result> { + fn as_child(&self, entry: DirEntry) -> Result> { let path = entry.path().to_path_buf(); Ok(Self { path, @@ -259,13 +259,10 @@ impl Action<'_> { let walker = WalkDir::new(&self.path) .max_open(1) .same_file_system(recurse.map_or(false, |x| x.same_filesystem)) - .follow_links(recurse.map_or(false, |x| match x.traversal { - Traversal::FullLinks => true, - _ => false, - })); + .follow_links(recurse.map_or(false, |x| matches!(x.traversal, Traversal::FullLinks))); for entry in walker { let entry = entry?; - let action = self.into_child(entry)?; + let action = self.as_child(entry)?; action.apply()?; } Ok(()) diff --git a/corebox/commands/chroot/mod.rs b/corebox/commands/chroot/mod.rs index 4a19b12..b875b60 100644 --- a/corebox/commands/chroot/mod.rs +++ b/corebox/commands/chroot/mod.rs @@ -59,7 +59,7 @@ impl Cmd for Chroot { } else { env::set_current_dir("/")?; } - return Err(command.exec().into()); + Err(command.exec().into()) } fn path(&self) -> Option { diff --git a/corebox/commands/df/mod.rs b/corebox/commands/df/mod.rs index 2875e0e..ad2d481 100644 --- a/corebox/commands/df/mod.rs +++ b/corebox/commands/df/mod.rs @@ -44,8 +44,7 @@ impl Cmd for Df { if let Some(files) = matches.get_many::("file") { for f in files { let entry = MntEntries::new("/proc/mounts")? - .filter(|e| e.dir.as_str() == f) - .next() + .find(|e| e.dir.as_str() == f) .ok_or(io::Error::new(io::ErrorKind::Other, "no such filesystem"))?; print_stats(&entry.fsname, &entry.dir, units)?; } @@ -93,8 +92,8 @@ enum Units { impl fmt::Display for Units { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::Posix => write!(f, "512-blocks"), - Self::Kilo => write!(f, " 1K-blocks"), + Self::Posix => write!(f, "512-blocks"), + Self::Kilo => write!(f, " 1K-blocks"), Self::Natural => write!(f, " Size"), } } @@ -109,8 +108,8 @@ fn print_stats(fs: &str, mntpt: &str, units: Units) -> Result<(), Box Units::Kilo => st.f_frsize / 1024, Units::Natural => st.f_frsize, }; - let total = st.f_blocks * bs as u64; - let avail = st.f_bfree * bs as u64; + let total = st.f_blocks * bs; + let avail = st.f_bfree * bs; let used = total - avail; let mut capacity = if total > 0 { (used * 100) / total } else { 0 }; if used * 100 != capacity * (used + avail) { diff --git a/corebox/commands/expand/mod.rs b/corebox/commands/expand/mod.rs index 0da3847..e0a3a8e 100644 --- a/corebox/commands/expand/mod.rs +++ b/corebox/commands/expand/mod.rs @@ -38,7 +38,7 @@ impl Cmd for Expand { ]) } - fn run(&self, matches: &clap::ArgMatches) -> Result<(), Box> { + fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box> { Ok(()) } diff --git a/corebox/commands/ln/mod.rs b/corebox/commands/ln/mod.rs index c4cb558..d94cc56 100644 --- a/corebox/commands/ln/mod.rs +++ b/corebox/commands/ln/mod.rs @@ -64,14 +64,10 @@ impl Cmd for Ln { } else { SymTarget::ToLink }; - if files.len() > 1 { - if !dest.is_dir() { - return Err(io::Error::new( - io::ErrorKind::Other, - "destination must be a directory", - ) - .into()); - } + if files.len() > 1 && !dest.is_dir() { + return Err( + io::Error::new(io::ErrorKind::Other, "destination must be a directory").into(), + ); } for f in files { let source = PathBuf::from(f); @@ -115,13 +111,11 @@ fn do_link( let name = source.file_name().unwrap(); dest.push(name); } - if dest.exists() { - if force { - unistd::unlink( - dest.to_str() - .ok_or(io::Error::new(io::ErrorKind::Other, "invalid utf8"))?, - )?; - } + if dest.exists() && force { + unistd::unlink( + dest.to_str() + .ok_or(io::Error::new(io::ErrorKind::Other, "invalid utf8"))?, + )?; } if symbolic { os::unix::fs::symlink(&source, &dest)?; diff --git a/corebox/commands/mktemp/mod.rs b/corebox/commands/mktemp/mod.rs index b6fb032..e206cb4 100644 --- a/corebox/commands/mktemp/mod.rs +++ b/corebox/commands/mktemp/mod.rs @@ -62,7 +62,7 @@ impl Cmd for MkTemp { let fname = cname.to_str()?; println!("{fname}"); if matches.get_flag("dryrun") { - fs::remove_dir(&fname)?; + fs::remove_dir(fname)?; } } else { let raw_fd = unsafe { libc::mkstemp(ptr) }; @@ -73,7 +73,7 @@ impl Cmd for MkTemp { let fname = cname.to_str()?; println!("{fname}"); if matches.get_flag("dryrun") { - fs::remove_file(&fname)?; + fs::remove_file(fname)?; } } Ok(()) diff --git a/corebox/commands/realpath/mod.rs b/corebox/commands/realpath/mod.rs index 7ac681a..9c4d5fd 100644 --- a/corebox/commands/realpath/mod.rs +++ b/corebox/commands/realpath/mod.rs @@ -43,7 +43,7 @@ impl Cmd for Realpath { process::exit(1); } } else { - match env::current_dir().and_then(|d| fs::canonicalize(d)) { + match env::current_dir().and_then(fs::canonicalize) { Ok(p) => println!("{}", p.display()), Err(e) => { if matches.get_flag("quiet") { diff --git a/corebox/commands/rev/mod.rs b/corebox/commands/rev/mod.rs index cad1265..c5245c1 100644 --- a/corebox/commands/rev/mod.rs +++ b/corebox/commands/rev/mod.rs @@ -42,7 +42,7 @@ impl Cmd for Rev { }?; stdout.reset()?; } - rev_file(&file)?; + rev_file(file)?; } } Ok(()) diff --git a/corebox/commands/rm/mod.rs b/corebox/commands/rm/mod.rs index 1c0215a..5da4582 100644 --- a/corebox/commands/rm/mod.rs +++ b/corebox/commands/rm/mod.rs @@ -83,7 +83,7 @@ impl Cmd for Rm { for act in actions.items { if let Err(e) = act.apply() { if !act.force { - return Err(e.into()); + return Err(e); } } } @@ -147,7 +147,7 @@ impl Action { Ok(None) => return Ok(()), Err(e) => { if !self.force { - return Err(e.into()); + return Err(e); } else { return Ok(()); } @@ -156,7 +156,7 @@ impl Action { } else { match File::open(&self.path) .and_then(|fd| fd.metadata()) - .and_then(|meta| Ok(FileType::from(meta))) + .map(FileType::from) { Ok(ft) => ft, Err(e) => { @@ -220,7 +220,7 @@ impl Action { } else { File::open(path) .and_then(|fd| fd.metadata()) - .and_then(|meta| Ok(FileType::from(meta)))? + .map(FileType::from)? }; eprint!("rm: remove {ft} '{}'? ", &self.path); let mut reply = String::new(); @@ -277,10 +277,7 @@ impl AllActions { let mut reply = String::new(); let stdin = io::stdin(); let _read = stdin.read_line(&mut reply); - match reply.trim_end() { - "y" | "Y" | "yes" | "Yes" => true, - _ => false, - } + matches!(reply.trim_end(), "y" | "Y" | "yes" | "Yes") } else { true } diff --git a/corebox/commands/wc/mod.rs b/corebox/commands/wc/mod.rs index df45fba..b8e5c8a 100644 --- a/corebox/commands/wc/mod.rs +++ b/corebox/commands/wc/mod.rs @@ -114,7 +114,7 @@ fn get_values<'a>( } if flags.contains(Flags::Max) { f.max = 0; - contents.lines().into_iter().for_each(|line| { + contents.lines().for_each(|line| { let max = line.chars().count(); f.max = cmp::max(max, f.max); }); diff --git a/errno/src/lib.rs b/errno/src/lib.rs index 88e7ebc..5d34901 100644 --- a/errno/src/lib.rs +++ b/errno/src/lib.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] +#![allow(clippy::missing_errors_doc, clippy::too_many_lines)] use core::fmt; use std::{error, ffi::c_long}; diff --git a/hashbox/commands/b2sum/mod.rs b/hashbox/commands/b2sum/mod.rs index 4231252..34f7721 100644 --- a/hashbox/commands/b2sum/mod.rs +++ b/hashbox/commands/b2sum/mod.rs @@ -54,10 +54,10 @@ impl Cmd for B2sum { let line = line?; let mut split = line.split_whitespace(); let sum = split.next().ok_or::( - io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(), + io::Error::new(io::ErrorKind::Other, "invalid checksum file"), )?; let file = split.next().ok_or::( - io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(), + io::Error::new(io::ErrorKind::Other, "invalid checksum file"), )?; let mut buf = vec![]; let mut fd = File::open(file)?; diff --git a/hashbox/commands/bootstrap/mod.rs b/hashbox/commands/bootstrap/mod.rs index 2901191..3f688ef 100644 --- a/hashbox/commands/bootstrap/mod.rs +++ b/hashbox/commands/bootstrap/mod.rs @@ -348,7 +348,7 @@ fn completions(prefix: &str, matches: &ArgMatches, usr: bool) -> Result<(), io:: })?; } if matches.get_flag("zsh") || matches.get_flag("all") { - let mut outdir = basedir.clone(); + let mut outdir = basedir; outdir.push("zsh"); outdir.push("site-functions"); COMMANDS.iter().try_for_each(|cmd| { diff --git a/hashbox/hash/mod.rs b/hashbox/hash/mod.rs index fd203aa..4384298 100644 --- a/hashbox/hash/mod.rs +++ b/hashbox/hash/mod.rs @@ -47,10 +47,10 @@ pub fn check_sums(file: &str, hashtype: HashType, erred: &mut usize) -> Result<( let line = line?; let mut split = line.split_whitespace(); let sum = split.next().ok_or::( - io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(), + io::Error::new(io::ErrorKind::Other, "invalid checksum file"), )?; let file = split.next().ok_or::( - io::Error::new(io::ErrorKind::Other, "invalid checksum file").into(), + io::Error::new(io::ErrorKind::Other, "invalid checksum file"), )?; let s = match hashtype { HashType::Md5 => compute_hash(file, Md5::new())?, diff --git a/mount/src/lib.rs b/mount/src/lib.rs index 812d7c8..b586f31 100644 --- a/mount/src/lib.rs +++ b/mount/src/lib.rs @@ -1,5 +1,7 @@ +#![warn(clippy::all, clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] use errno::Errno; -use sc::*; +use sc::syscall; use std::{error::Error, ffi::CString}; mod mntent; @@ -12,7 +14,7 @@ pub use self::{ mntflags::{Flags as MntFlags, ParseFlagsError}, }; -/// Wraps the Linux SYS_mount syscall in a nicer interface +/// Wraps the Linux `SYS_mount` syscall in a nicer interface pub fn mount( dev: &str, mountpoint: &str, diff --git a/mount/src/mntent.rs b/mount/src/mntent.rs index 6191581..08218ac 100644 --- a/mount/src/mntent.rs +++ b/mount/src/mntent.rs @@ -1,3 +1,4 @@ +#![allow(clippy::must_use_candidate)] //! A Rust reimplementation of libc's mntent, for parsing /etc/fstab or //! /proc/mounts use super::{MntEntries, MntFlags}; @@ -52,7 +53,7 @@ impl MntEntry { if self.fstype != "swap" { return false; } - let dev = self.device().map(|x| x.to_str().map(|x| x.to_string())); + let dev = self.device().map(|x| x.to_str().map(ToString::to_string)); let Ok(Some(dev)) = dev else { return false }; let Ok(fd) = File::open("/proc/swaps") else { return false; @@ -64,7 +65,7 @@ impl MntEntry { }; let swap = line.split_whitespace().next(); if let Some(swap) = swap { - if swap == &dev { + if swap == dev { return true; } } @@ -179,13 +180,10 @@ fn dev_from_uuid(s: &str) -> io::Result { for dev in devs { let tags = dev.tags(); for tag in tags { - match tag.typ() { - TagType::Superblock(SuperblockTag::Uuid) => { - if uuid.to_lowercase().as_str().trim() == tag.value().trim() { - return dev.name().canonicalize(); - } + if let TagType::Superblock(SuperblockTag::Uuid) = tag.typ() { + if uuid.to_lowercase().as_str().trim() == tag.value().trim() { + return dev.name().canonicalize(); } - _ => {} } } } @@ -202,13 +200,10 @@ fn dev_from_label(s: &str) -> io::Result { for dev in devs { let tags = dev.tags(); for tag in tags { - match tag.typ() { - TagType::Superblock(SuperblockTag::Label) => { - if label == tag.value() { - return dev.name().canonicalize(); - } + if let TagType::Superblock(SuperblockTag::Label) = tag.typ() { + if label == tag.value() { + return dev.name().canonicalize(); } - _ => {} } } } @@ -225,13 +220,10 @@ fn dev_from_partuuid(s: &str) -> io::Result { for dev in devs { let tags = dev.tags(); for tag in tags { - match tag.typ() { - TagType::Partition(PartitionTag::PartEntryUuid) => { - if partlabel == tag.value() { - return dev.name().canonicalize(); - } + if let TagType::Partition(PartitionTag::PartEntryUuid) = tag.typ() { + if partlabel == tag.value() { + return dev.name().canonicalize(); } - _ => {} } } } @@ -248,13 +240,10 @@ fn dev_from_partlabel(s: &str) -> io::Result { for dev in devs { let tags = dev.tags(); for tag in tags { - match tag.typ() { - TagType::Partition(PartitionTag::PartEntryName) => { - if partlabel == tag.value() { - return dev.name().canonicalize(); - } + if let TagType::Partition(PartitionTag::PartEntryName) = tag.typ() { + if partlabel == tag.value() { + return dev.name().canonicalize(); } - _ => {} } } } diff --git a/mount/src/mntflags.rs b/mount/src/mntflags.rs index 23d41a8..a95877c 100644 --- a/mount/src/mntflags.rs +++ b/mount/src/mntflags.rs @@ -5,6 +5,7 @@ use std::{ str::FromStr, }; +#[repr(u32)] pub enum Flags { ReadOnly = 1, NoSuid = 2, diff --git a/pw/src/lib.rs b/pw/src/lib.rs index eff1a7a..7531da1 100644 --- a/pw/src/lib.rs +++ b/pw/src/lib.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] +#![allow(clippy::missing_errors_doc, clippy::must_use_candidate)] //! Wraps certain libc functions around groups and users use std::{ error::Error, diff --git a/src/args/mod.rs b/src/args/mod.rs index 63f9a17..b0fb6b0 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -1,5 +1,6 @@ //! Common arguments to be re-used across multiple commands. Using these args //! instead of implementing from scratch increases consistency across applets +#![allow(clippy::must_use_candidate)] use clap::{Arg, ArgAction}; pub fn verbose() -> Arg { diff --git a/src/lib.rs b/src/lib.rs index 01b6360..1ff133f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![warn(clippy::all, clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] use std::{env, path::PathBuf, string::ToString}; pub mod args; diff --git a/src/stat/mod.rs b/src/stat/mod.rs index 946bf54..24aec29 100644 --- a/src/stat/mod.rs +++ b/src/stat/mod.rs @@ -1,14 +1,13 @@ use errno::Errno; -use sc::*; +use sc::syscall; use std::{ error::Error, - ffi::{c_long, CString}, + ffi::CString, fs::File, mem, os::fd::AsRawFd, }; -#[inline(always)] pub fn mknod(path: &str, mode: u32, dev: u64) -> Result<(), Box> { let ret = unsafe { syscall!( @@ -26,7 +25,6 @@ pub fn mknod(path: &str, mode: u32, dev: u64) -> Result<(), Box> { } } -#[inline(always)] pub fn mkfifo(path: &str, mode: u32) -> Result<(), Box> { let ret = unsafe { syscall!( @@ -44,7 +42,6 @@ pub fn mkfifo(path: &str, mode: u32) -> Result<(), Box> { } } -#[inline(always)] pub fn statfs(dir: &str) -> Result> { let mut buf = mem::MaybeUninit::::uninit(); let fd = File::open(dir)?; @@ -54,6 +51,6 @@ pub fn statfs(dir: &str) -> Result> { Ok(buf) } else { let e = unsafe { *libc::__errno_location() }; - Err(Errno::from(e as c_long).into()) + Err(Errno::from(i64::from(e)).into()) } } diff --git a/unistd/src/lib.rs b/unistd/src/lib.rs index 11e2354..75da1de 100644 --- a/unistd/src/lib.rs +++ b/unistd/src/lib.rs @@ -1,9 +1,10 @@ +#![warn(clippy::all, clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] use errno::Errno; use libc::utsname; use sc::syscall; -use std::{error::Error, ffi::CString, fs::File, os::fd::AsRawFd, u8}; +use std::{error::Error, ffi::CString, fs::File, os::fd::AsRawFd, ptr, u8}; -#[inline(always)] fn new_utsname() -> utsname { utsname { sysname: [0; 65], @@ -15,11 +16,11 @@ fn new_utsname() -> utsname { } } -#[inline(always)] +#[allow(clippy::cast_sign_loss)] pub fn gethostname() -> Result> { let mut uts = new_utsname(); unsafe { - let ret = syscall!(UNAME, &mut uts as *mut utsname); + let ret = syscall!(UNAME, ptr::addr_of_mut!(uts)); if ret != 0 { return Err(Errno::from(ret).into()); } @@ -30,10 +31,9 @@ pub fn gethostname() -> Result> { .map(|x| *x as u8) .take_while(|x| *x != 0) .collect(); - String::from_utf8(name).map_err(|e| e.into()) + String::from_utf8(name).map_err(Into::into) } -#[inline(always)] pub fn sethostname(name: &str) -> Result<(), Errno> { let ret = unsafe { syscall!(SETHOSTNAME, name.as_ptr(), name.len()) }; if ret == 0 { @@ -43,7 +43,6 @@ pub fn sethostname(name: &str) -> Result<(), Errno> { } } -#[inline(always)] pub fn link(source: &str, dest: &str) -> Result<(), Box> { let ret = unsafe { syscall!( @@ -62,7 +61,6 @@ pub fn link(source: &str, dest: &str) -> Result<(), Box> { } } -#[inline(always)] pub fn unlink(name: &str) -> Result<(), Box> { let ret = unsafe { syscall!(UNLINKAT, libc::AT_FDCWD, CString::new(name)?.as_ptr(), 0) }; if ret == 0 { @@ -72,7 +70,7 @@ pub fn unlink(name: &str) -> Result<(), Box> { } } -#[inline(always)] +#[allow(clippy::cast_sign_loss)] pub fn readlink(name: &str) -> Result> { let mut buf = Vec::::with_capacity(1024); let ret = unsafe { @@ -90,13 +88,12 @@ pub fn readlink(name: &str) -> Result> { .map(|x| *x as u8) .take_while(|x| *x != 0) .collect(); - String::from_utf8(path).map_err(|e| e.into()) + String::from_utf8(path).map_err(Into::into) } else { Err(Errno::from(ret).into()) } } -#[inline(always)] pub fn fdatasync(fd: &File) -> Result<(), Errno> { let ret = unsafe { syscall!(FDATASYNC, fd.as_raw_fd()) }; if ret == 0 { @@ -106,7 +103,6 @@ pub fn fdatasync(fd: &File) -> Result<(), Errno> { } } -#[inline(always)] pub fn syncfs(fd: &File) -> Result<(), Errno> { let ret = unsafe { syscall!(SYNCFS, fd.as_raw_fd()) }; if ret == 0 { @@ -116,7 +112,6 @@ pub fn syncfs(fd: &File) -> Result<(), Errno> { } } -#[inline(always)] pub fn fsync(fd: &File) -> Result<(), Errno> { let ret = unsafe { syscall!(FSYNC, fd.as_raw_fd()) }; if ret == 0 { @@ -126,12 +121,10 @@ pub fn fsync(fd: &File) -> Result<(), Errno> { } } -#[inline(always)] pub fn sync() { unsafe { syscall!(SYNC) }; } -#[inline(always)] pub fn symlink(source: &str, dest: &str) -> Result<(), Box> { let ret = unsafe { syscall!( diff --git a/unix-mode/src/bit.rs b/unix-mode/src/bit.rs index a365c25..4448346 100644 --- a/unix-mode/src/bit.rs +++ b/unix-mode/src/bit.rs @@ -1,3 +1,4 @@ +#![allow(clippy::must_use_candidate)] use bitflags::BitFlags; use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign}; @@ -80,7 +81,9 @@ impl BitOrAssign for u32 { impl Bit { pub fn as_char(&self, mode: u32) -> char { - if mode & *self != 0 { + if mode & *self == 0 { + '-' + } else { match self { Self::Suid | Self::Sgid => 's', Self::Sticky => 't', @@ -91,8 +94,6 @@ impl Bit { Self::OExec if mode.contains(Self::Sticky) => 't', Self::UExec | Self::GExec | Self::OExec => 'x', } - } else { - '-' } } } diff --git a/unix-mode/src/lib.rs b/unix-mode/src/lib.rs index c5b6697..aa876c2 100644 --- a/unix-mode/src/lib.rs +++ b/unix-mode/src/lib.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] //! Functions for parsing and managing permissions mod bit; mod parser; diff --git a/utilbox/commands/bootstrap/mod.rs b/utilbox/commands/bootstrap/mod.rs index 2901191..3f688ef 100644 --- a/utilbox/commands/bootstrap/mod.rs +++ b/utilbox/commands/bootstrap/mod.rs @@ -348,7 +348,7 @@ fn completions(prefix: &str, matches: &ArgMatches, usr: bool) -> Result<(), io:: })?; } if matches.get_flag("zsh") || matches.get_flag("all") { - let mut outdir = basedir.clone(); + let mut outdir = basedir; outdir.push("zsh"); outdir.push("site-functions"); COMMANDS.iter().try_for_each(|cmd| { diff --git a/utilbox/commands/mount/mod.rs b/utilbox/commands/mount/mod.rs index 008dee4..d525413 100644 --- a/utilbox/commands/mount/mod.rs +++ b/utilbox/commands/mount/mod.rs @@ -1,4 +1,4 @@ -use clap::{Arg, ArgAction, ArgMatches, Command}; +use clap::{ArgMatches, Command}; use shitbox::Cmd; #[derive(Debug, Default)] @@ -9,7 +9,7 @@ impl Cmd for Mount { Command::new("mount") } - fn run(&self, matches: &ArgMatches) -> Result<(), Box> { + fn run(&self, _matches: &ArgMatches) -> Result<(), Box> { Ok(()) } diff --git a/utilbox/commands/swaplabel/mod.rs b/utilbox/commands/swaplabel/mod.rs index 16850d0..fbd817b 100644 --- a/utilbox/commands/swaplabel/mod.rs +++ b/utilbox/commands/swaplabel/mod.rs @@ -8,8 +8,8 @@ use std::{ io::{self, Read, Seek, SeekFrom, Write}, }; -const SWAP_MAGIC1: &'static str = "SWAPSPACE2"; -const SWAP_MAGIC2: &'static str = "SWAP_SPACE"; +const SWAP_MAGIC1: &str = "SWAPSPACE2"; +const SWAP_MAGIC2: &str = "SWAP_SPACE"; const SWAP_MAGIC_LENGTH: u64 = 10; const SWAP_LABEL_LENGTH: u64 = 16; const SWAP_LABEL_OFFSET: u64 = 1024 + 4 + 4 + 4 + 16; @@ -61,7 +61,7 @@ impl Cmd for Swaplabel { return Err(io::Error::new(io::ErrorKind::Other, "label is too long").into()); } let label = CString::new(label.as_str())?; - fd.write(label.as_bytes())?; + fd.write_all(label.as_bytes())?; } else { let mut label = [0; SWAP_LABEL_LENGTH as usize]; fd.read_exact(&mut label)?; diff --git a/utilbox/commands/swapoff/mod.rs b/utilbox/commands/swapoff/mod.rs index e9d0a2a..d6173f2 100644 --- a/utilbox/commands/swapoff/mod.rs +++ b/utilbox/commands/swapoff/mod.rs @@ -47,7 +47,7 @@ impl Cmd for Swapoff { } } } else if let Some(devices) = matches.get_many::("device") { - for d in devices { + 'devloop: for d in devices { let fd = File::open("/proc/swaps")?; let reader = BufReader::new(fd); for line in reader.lines() { @@ -61,7 +61,7 @@ impl Cmd for Swapoff { if matches.get_flag("verbose") { println!("swapoff {d}"); } - return Ok(()); + continue 'devloop; } } let msg = format!("{d} is not swapped"); diff --git a/utilbox/commands/umount/mod.rs b/utilbox/commands/umount/mod.rs index 115a9e2..eb24591 100644 --- a/utilbox/commands/umount/mod.rs +++ b/utilbox/commands/umount/mod.rs @@ -100,10 +100,8 @@ impl Cmd for Umount { if x.fstype == t { ret = false; } - } else { - if &x.fstype != t { - ret = false; - } + } else if &x.fstype != t { + ret = false; } } } @@ -126,14 +124,12 @@ impl Cmd for Umount { println!("umount: {} unmounted", &p.dir); } } - } else { - if let Some(spec) = matches.get_many::("spec") { - for s in spec { - let mntpt = get_mntpt(s)?; - mount::umount(&mntpt, flags as u32)?; - if matches.get_flag("verbose") { - println!("umount: {mntpt} unmounted"); - } + } else if let Some(spec) = matches.get_many::("spec") { + for s in spec { + let mntpt = get_mntpt(s)?; + mount::umount(&mntpt, flags as u32)?; + if matches.get_flag("verbose") { + println!("umount: {mntpt} unmounted"); } } } @@ -149,7 +145,7 @@ fn get_mntpt(spec: &str) -> io::Result { let entries = MntEntries::new("/proc/mounts")?; for e in entries { if e.fsname == spec { - return Ok(e.dir.clone()); + return Ok(e.dir); } else if e.dir == spec { return Ok(spec.to_string()); }