1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Enable and fix unused_qualifications lint

Improve code readability
This commit is contained in:
Yuri Astrakhan 2025-04-09 12:50:22 -04:00
parent 4559e974ff
commit 9f56bf5f07
44 changed files with 214 additions and 237 deletions

View file

@ -589,7 +589,7 @@ pedantic = { level = "deny", priority = -1 }
# Eventually the clippy settings from the `[lints]` section should be moved here.
# In order to use these, all crates have `[lints] workspace = true` section.
[workspace.lints.rust]
# unused_qualifications = "warn"
unused_qualifications = "warn"
[workspace.lints.clippy]
all = { level = "deny", priority = -1 }

View file

@ -139,7 +139,7 @@ pub fn base_app(about: &'static str, usage: &str) -> Command {
.arg(
Arg::new(options::FILE)
.index(1)
.action(clap::ArgAction::Append)
.action(ArgAction::Append)
.value_hint(clap::ValueHint::FilePath),
)
}

View file

@ -90,7 +90,7 @@ pub fn uu_app() -> Command {
)
.arg(
Arg::new(options::NAME)
.action(clap::ArgAction::Append)
.action(ArgAction::Append)
.value_hint(clap::ValueHint::AnyPath)
.hide(true)
.trailing_var_arg(true),

View file

@ -86,7 +86,7 @@ impl LineNumber {
}
}
fn write(&self, writer: &mut impl Write) -> std::io::Result<()> {
fn write(&self, writer: &mut impl Write) -> io::Result<()> {
writer.write_all(&self.buf)
}
}
@ -288,7 +288,7 @@ pub fn uu_app() -> Command {
.arg(
Arg::new(options::FILE)
.hide(true)
.action(clap::ArgAction::Append)
.action(ArgAction::Append)
.value_hint(clap::ValueHint::FilePath),
)
.arg(
@ -377,7 +377,7 @@ fn cat_handle<R: FdReadable>(
/// Whether this process is appending to stdout.
#[cfg(unix)]
fn is_appending() -> bool {
let stdout = std::io::stdout();
let stdout = io::stdout();
let flags = match fcntl(stdout.as_raw_fd(), FcntlArg::F_GETFL) {
Ok(flags) => flags,
Err(_) => return false,
@ -404,7 +404,7 @@ fn cat_path(
let in_info = FileInformation::from_file(&stdin)?;
let mut handle = InputHandle {
reader: stdin,
is_interactive: std::io::stdin().is_terminal(),
is_interactive: io::stdin().is_terminal(),
};
if let Some(out_info) = out_info {
if in_info == *out_info && is_appending() {
@ -445,7 +445,7 @@ fn cat_path(
}
fn cat_files(files: &[String], options: &OutputOptions) -> UResult<()> {
let out_info = FileInformation::from_file(&std::io::stdout()).ok();
let out_info = FileInformation::from_file(&io::stdout()).ok();
let mut state = OutputState {
line_number: LineNumber::new(),

View file

@ -312,7 +312,7 @@ struct Options {
files: Vec<PathBuf>,
}
fn parse_command_line(config: clap::Command, args: impl uucore::Args) -> Result<Options> {
fn parse_command_line(config: Command, args: impl uucore::Args) -> Result<Options> {
let matches = config.try_get_matches_from(args)?;
let verbose = matches.get_flag(options::VERBOSE);

View file

@ -16,9 +16,9 @@ use crate::os_str_to_c_string;
#[derive(Debug)]
pub(crate) struct FTS {
fts: ptr::NonNull<fts_sys::FTS>,
fts: NonNull<fts_sys::FTS>,
entry: Option<ptr::NonNull<fts_sys::FTSENT>>,
entry: Option<NonNull<fts_sys::FTSENT>>,
_phantom_data: PhantomData<fts_sys::FTSENT>,
}
@ -52,7 +52,7 @@ impl FTS {
// - `compar` is None.
let fts = unsafe { fts_sys::fts_open(path_argv.as_ptr().cast(), options, None) };
let fts = ptr::NonNull::new(fts)
let fts = NonNull::new(fts)
.ok_or_else(|| Error::from_io("fts_open()", io::Error::last_os_error()))?;
Ok(Self {
@ -110,14 +110,14 @@ impl Drop for FTS {
#[derive(Debug)]
pub(crate) struct EntryRef<'fts> {
pub(crate) pointer: ptr::NonNull<fts_sys::FTSENT>,
pub(crate) pointer: NonNull<fts_sys::FTSENT>,
_fts: PhantomData<&'fts FTS>,
_phantom_data: PhantomData<fts_sys::FTSENT>,
}
impl<'fts> EntryRef<'fts> {
fn new(_fts: &'fts FTS, entry: ptr::NonNull<fts_sys::FTSENT>) -> Self {
fn new(_fts: &'fts FTS, entry: NonNull<fts_sys::FTSENT>) -> Self {
Self {
pointer: entry,
_fts: PhantomData,
@ -174,7 +174,7 @@ impl<'fts> EntryRef<'fts> {
}
pub(crate) fn access_path(&self) -> Option<&Path> {
ptr::NonNull::new(self.as_ref().fts_accpath)
NonNull::new(self.as_ref().fts_accpath)
.map(|path_ptr| {
// SAFETY: `entry.fts_accpath` is a non-null pointer that is assumed to be valid.
unsafe { CStr::from_ptr(path_ptr.as_ptr()) }
@ -184,7 +184,7 @@ impl<'fts> EntryRef<'fts> {
}
pub(crate) fn stat(&self) -> Option<&libc::stat> {
ptr::NonNull::new(self.as_ref().fts_statp).map(|stat_ptr| {
NonNull::new(self.as_ref().fts_statp).map(|stat_ptr| {
// SAFETY: `entry.fts_statp` is a non-null pointer that is assumed to be valid.
unsafe { stat_ptr.as_ref() }
})

View file

@ -350,7 +350,7 @@ pub fn uu_app() -> Command {
.arg(
Arg::new(options::FILE)
.hide(true)
.action(clap::ArgAction::Append)
.action(ArgAction::Append)
.value_parser(ValueParser::os_string())
.value_hint(clap::ValueHint::FilePath),
)

View file

@ -118,8 +118,8 @@ impl OrderChecker {
// Check if two files are identical by comparing their contents
pub fn are_files_identical(path1: &str, path2: &str) -> io::Result<bool> {
// First compare file sizes
let metadata1 = std::fs::metadata(path1)?;
let metadata2 = std::fs::metadata(path2)?;
let metadata1 = metadata(path1)?;
let metadata2 = metadata(path2)?;
if metadata1.len() != metadata2.len() {
return Ok(false);

View file

@ -101,7 +101,7 @@ struct Context<'a> {
}
impl<'a> Context<'a> {
fn new(root: &'a Path, target: &'a Path) -> std::io::Result<Self> {
fn new(root: &'a Path, target: &'a Path) -> io::Result<Self> {
let current_dir = env::current_dir()?;
let root_path = current_dir.join(root);
let root_parent = if target.exists() && !root.to_str().unwrap().ends_with("/.") {
@ -181,7 +181,7 @@ impl Entry {
if no_target_dir {
let source_is_dir = source.is_dir();
if path_ends_with_terminator(context.target) && source_is_dir {
if let Err(e) = std::fs::create_dir_all(context.target) {
if let Err(e) = fs::create_dir_all(context.target) {
eprintln!("Failed to create directory: {e}");
}
} else {
@ -305,9 +305,7 @@ fn copy_direntry(
false,
) {
Ok(_) => {}
Err(Error::IoErrContext(e, _))
if e.kind() == std::io::ErrorKind::PermissionDenied =>
{
Err(Error::IoErrContext(e, _)) if e.kind() == io::ErrorKind::PermissionDenied => {
show!(uio_error!(
e,
"cannot open {} for reading",
@ -580,14 +578,13 @@ fn build_dir(
// we need to allow trivial casts here because some systems like linux have u32 constants in
// in libc while others don't.
#[allow(clippy::unnecessary_cast)]
let mut excluded_perms =
if matches!(options.attributes.ownership, crate::Preserve::Yes { .. }) {
libc::S_IRWXG | libc::S_IRWXO // exclude rwx for group and other
} else if matches!(options.attributes.mode, crate::Preserve::Yes { .. }) {
libc::S_IWGRP | libc::S_IWOTH //exclude w for group and other
} else {
0
} as u32;
let mut excluded_perms = if matches!(options.attributes.ownership, Preserve::Yes { .. }) {
libc::S_IRWXG | libc::S_IRWXO // exclude rwx for group and other
} else if matches!(options.attributes.mode, Preserve::Yes { .. }) {
libc::S_IWGRP | libc::S_IWOTH //exclude w for group and other
} else {
0
} as u32;
let umask = if copy_attributes_from.is_some()
&& matches!(options.attributes.mode, Preserve::Yes { .. })

View file

@ -737,7 +737,7 @@ pub fn uu_app() -> Command {
Arg::new(options::PROGRESS_BAR)
.long(options::PROGRESS_BAR)
.short('g')
.action(clap::ArgAction::SetTrue)
.action(ArgAction::SetTrue)
.help(
"Display a progress bar. \n\
Note: this feature is not supported by GNU coreutils.",
@ -2081,7 +2081,7 @@ fn handle_copy_mode(
CopyMode::Update => {
if dest.exists() {
match options.update {
update_control::UpdateMode::ReplaceAll => {
UpdateMode::ReplaceAll => {
copy_helper(
source,
dest,
@ -2094,17 +2094,17 @@ fn handle_copy_mode(
source_is_stream,
)?;
}
update_control::UpdateMode::ReplaceNone => {
UpdateMode::ReplaceNone => {
if options.debug {
println!("skipped {}", dest.quote());
}
return Ok(PerformedAction::Skipped);
}
update_control::UpdateMode::ReplaceNoneFail => {
UpdateMode::ReplaceNoneFail => {
return Err(Error::Error(format!("not replacing '{}'", dest.display())));
}
update_control::UpdateMode::ReplaceIfOlder => {
UpdateMode::ReplaceIfOlder => {
let dest_metadata = fs::symlink_metadata(dest)?;
let src_time = source_metadata.modified()?;
@ -2335,7 +2335,7 @@ fn copy_file(
&FileInformation::from_path(source, options.dereference(source_in_command_line))
.context(format!("cannot stat {}", source.quote()))?,
) {
std::fs::hard_link(new_source, dest)?;
fs::hard_link(new_source, dest)?;
if options.verbose {
print_verbose_output(options.parents, progress_bar, source, dest);

View file

@ -43,7 +43,7 @@ mod options {
/// Command line options for csplit.
pub struct CsplitOptions {
split_name: crate::SplitName,
split_name: SplitName,
keep_files: bool,
quiet: bool,
elide_empty_files: bool,
@ -661,7 +661,7 @@ pub fn uu_app() -> Command {
.arg(
Arg::new(options::PATTERN)
.hide(true)
.action(clap::ArgAction::Append)
.action(ArgAction::Append)
.required(true),
)
.after_help(AFTER_HELP)

View file

@ -352,7 +352,7 @@ fn cut_files(mut filenames: Vec<String>, mode: &Mode) {
filenames.push("-".to_owned());
}
let mut out: Box<dyn Write> = if std::io::stdout().is_terminal() {
let mut out: Box<dyn Write> = if stdout().is_terminal() {
Box::new(stdout())
} else {
Box::new(BufWriter::new(stdout())) as Box<dyn Write>

View file

@ -222,7 +222,7 @@ impl Source {
/// The length of the data source in number of bytes.
///
/// If it cannot be determined, then this function returns 0.
fn len(&self) -> std::io::Result<i64> {
fn len(&self) -> io::Result<i64> {
match self {
Self::File(f) => Ok(f.metadata()?.len().try_into().unwrap_or(i64::MAX)),
_ => Ok(0),
@ -260,7 +260,7 @@ impl Source {
Err(e) => Err(e),
}
}
Self::File(f) => f.seek(io::SeekFrom::Current(n.try_into().unwrap())),
Self::File(f) => f.seek(SeekFrom::Current(n.try_into().unwrap())),
#[cfg(unix)]
Self::Fifo(f) => io::copy(&mut f.take(n), &mut io::sink()),
}
@ -470,7 +470,7 @@ impl Input<'_> {
/// Fills a given buffer.
/// Reads in increments of 'self.ibs'.
/// The start of each ibs-sized read follows the previous one.
fn fill_consecutive(&mut self, buf: &mut Vec<u8>) -> std::io::Result<ReadStat> {
fn fill_consecutive(&mut self, buf: &mut Vec<u8>) -> io::Result<ReadStat> {
let mut reads_complete = 0;
let mut reads_partial = 0;
let mut bytes_total = 0;
@ -501,7 +501,7 @@ impl Input<'_> {
/// Fills a given buffer.
/// Reads in increments of 'self.ibs'.
/// The start of each ibs-sized read is aligned to multiples of ibs; remaining space is filled with the 'pad' byte.
fn fill_blocks(&mut self, buf: &mut Vec<u8>, pad: u8) -> std::io::Result<ReadStat> {
fn fill_blocks(&mut self, buf: &mut Vec<u8>, pad: u8) -> io::Result<ReadStat> {
let mut reads_complete = 0;
let mut reads_partial = 0;
let mut base_idx = 0;
@ -612,7 +612,7 @@ impl Dest {
return Ok(len);
}
}
f.seek(io::SeekFrom::Current(n.try_into().unwrap()))
f.seek(SeekFrom::Current(n.try_into().unwrap()))
}
#[cfg(unix)]
Self::Fifo(f) => {
@ -655,7 +655,7 @@ impl Dest {
/// The length of the data destination in number of bytes.
///
/// If it cannot be determined, then this function returns 0.
fn len(&self) -> std::io::Result<i64> {
fn len(&self) -> io::Result<i64> {
match self {
Self::File(f, _) => Ok(f.metadata()?.len().try_into().unwrap_or(i64::MAX)),
_ => Ok(0),
@ -676,7 +676,7 @@ impl Write for Dest {
.len()
.try_into()
.expect("Internal dd Error: Seek amount greater than signed 64-bit integer");
f.seek(io::SeekFrom::Current(seek_amt))?;
f.seek(SeekFrom::Current(seek_amt))?;
Ok(buf.len())
}
Self::File(f, _) => f.write(buf),
@ -893,7 +893,7 @@ impl<'a> Output<'a> {
}
/// Flush the output to disk, if configured to do so.
fn sync(&mut self) -> std::io::Result<()> {
fn sync(&mut self) -> io::Result<()> {
if self.settings.oconv.fsync {
self.dst.fsync()
} else if self.settings.oconv.fdatasync {
@ -905,7 +905,7 @@ impl<'a> Output<'a> {
}
/// Truncate the underlying file to the current stream position, if possible.
fn truncate(&mut self) -> std::io::Result<()> {
fn truncate(&mut self) -> io::Result<()> {
self.dst.truncate()
}
}
@ -959,7 +959,7 @@ impl BlockWriter<'_> {
};
}
fn write_blocks(&mut self, buf: &[u8]) -> std::io::Result<WriteStat> {
fn write_blocks(&mut self, buf: &[u8]) -> io::Result<WriteStat> {
match self {
Self::Unbuffered(o) => o.write_blocks(buf),
Self::Buffered(o) => o.write_blocks(buf),
@ -969,7 +969,7 @@ impl BlockWriter<'_> {
/// depending on the command line arguments, this function
/// informs the OS to flush/discard the caches for input and/or output file.
fn flush_caches_full_length(i: &Input, o: &Output) -> std::io::Result<()> {
fn flush_caches_full_length(i: &Input, o: &Output) -> io::Result<()> {
// TODO Better error handling for overflowing `len`.
if i.settings.iflags.nocache {
let offset = 0;
@ -1001,7 +1001,7 @@ fn flush_caches_full_length(i: &Input, o: &Output) -> std::io::Result<()> {
///
/// If there is a problem reading from the input or writing to
/// this output.
fn dd_copy(mut i: Input, o: Output) -> std::io::Result<()> {
fn dd_copy(mut i: Input, o: Output) -> io::Result<()> {
// The read and write statistics.
//
// These objects are counters, initialized to zero. After each
@ -1177,7 +1177,7 @@ fn finalize<T>(
prog_tx: &mpsc::Sender<ProgUpdate>,
output_thread: thread::JoinHandle<T>,
truncate: bool,
) -> std::io::Result<()> {
) -> io::Result<()> {
// Flush the output in case a partial write has been buffered but
// not yet written.
let wstat_update = output.flush()?;
@ -1245,7 +1245,7 @@ fn make_linux_oflags(oflags: &OFlags) -> Option<libc::c_int> {
/// `conv=swab` or `conv=block` command-line arguments. This function
/// mutates the `buf` argument in-place. The returned [`ReadStat`]
/// indicates how many blocks were read.
fn read_helper(i: &mut Input, buf: &mut Vec<u8>, bsize: usize) -> std::io::Result<ReadStat> {
fn read_helper(i: &mut Input, buf: &mut Vec<u8>, bsize: usize) -> io::Result<ReadStat> {
// Local Helper Fns -------------------------------------------------
fn perform_swab(buf: &mut [u8]) {
for base in (1..buf.len()).step_by(2) {

View file

@ -654,7 +654,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
files.collect()
} else {
// Deduplicate while preserving order
let mut seen = std::collections::HashSet::new();
let mut seen = HashSet::new();
files
.filter(|path| seen.insert(path.clone()))
.collect::<Vec<_>>()

12
src/uu/env/src/env.rs vendored
View file

@ -345,7 +345,7 @@ fn debug_print_args(args: &[OsString]) {
fn check_and_handle_string_args(
arg: &OsString,
prefix_to_test: &str,
all_args: &mut Vec<std::ffi::OsString>,
all_args: &mut Vec<OsString>,
do_debug_print_args: Option<&Vec<OsString>>,
) -> UResult<bool> {
let native_arg = NCvt::convert(arg);
@ -386,8 +386,8 @@ impl EnvAppData {
fn process_all_string_arguments(
&mut self,
original_args: &Vec<OsString>,
) -> UResult<Vec<std::ffi::OsString>> {
let mut all_args: Vec<std::ffi::OsString> = Vec::new();
) -> UResult<Vec<OsString>> {
let mut all_args: Vec<OsString> = Vec::new();
for arg in original_args {
match arg {
b if check_and_handle_string_args(b, "--split-string", &mut all_args, None)? => {
@ -454,7 +454,7 @@ impl EnvAppData {
uucore::show_error!("{s}");
}
uucore::show_error!("{ERROR_MSG_S_SHEBANG}");
uucore::error::ExitCode::new(125)
ExitCode::new(125)
}
}
})?;
@ -751,7 +751,7 @@ fn apply_ignore_signal(opts: &Options<'_>) -> UResult<()> {
for &sig_value in &opts.ignore_signal {
let sig: Signal = (sig_value as i32)
.try_into()
.map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
.map_err(|e| io::Error::from_raw_os_error(e as i32))?;
ignore_signal(sig)?;
}
@ -786,7 +786,7 @@ mod tests {
#[test]
fn test_split_string_environment_vars_test() {
unsafe { std::env::set_var("FOO", "BAR") };
unsafe { env::set_var("FOO", "BAR") };
assert_eq!(
NCvt::convert(vec!["FOO=bar", "sh", "-c", "echo xBARx =$FOO="]),
parse_args_from_str(&NCvt::convert(r#"FOO=bar sh -c "echo x${FOO}x =\$FOO=""#))

View file

@ -27,10 +27,10 @@ mod options {
fn print_factors_str(
num_str: &str,
w: &mut io::BufWriter<impl io::Write>,
w: &mut io::BufWriter<impl Write>,
print_exponents: bool,
) -> UResult<()> {
let rx = num_str.trim().parse::<num_bigint::BigUint>();
let rx = num_str.trim().parse::<BigUint>();
let Ok(x) = rx else {
// return Ok(). it's non-fatal and we should try the next number.
show_warning!("{}: {}", num_str.maybe_quote(), rx.unwrap_err());

View file

@ -7,7 +7,6 @@
use clap::{Arg, ArgAction, ArgMatches, Command};
use std::ffi::OsString;
#[cfg(unix)]
use std::fs::File;
use std::io::{self, BufWriter, Read, Seek, SeekFrom, Write};
use std::num::TryFromIntError;
@ -224,7 +223,7 @@ struct HeadOptions {
impl HeadOptions {
///Construct options from matches
pub fn get_from(matches: &clap::ArgMatches) -> Result<Self, String> {
pub fn get_from(matches: &ArgMatches) -> Result<Self, String> {
let mut options = Self::default();
options.quiet = matches.get_flag(options::QUIET_NAME);
@ -251,12 +250,12 @@ fn wrap_in_stdout_error(err: io::Error) -> io::Error {
)
}
fn read_n_bytes(input: impl Read, n: u64) -> std::io::Result<u64> {
fn read_n_bytes(input: impl Read, n: u64) -> io::Result<u64> {
// Read the first `n` bytes from the `input` reader.
let mut reader = input.take(n);
// Write those bytes to `stdout`.
let stdout = std::io::stdout();
let stdout = io::stdout();
let mut stdout = stdout.lock();
let bytes_written = io::copy(&mut reader, &mut stdout).map_err(wrap_in_stdout_error)?;
@ -269,12 +268,12 @@ fn read_n_bytes(input: impl Read, n: u64) -> std::io::Result<u64> {
Ok(bytes_written)
}
fn read_n_lines(input: &mut impl std::io::BufRead, n: u64, separator: u8) -> std::io::Result<u64> {
fn read_n_lines(input: &mut impl io::BufRead, n: u64, separator: u8) -> io::Result<u64> {
// Read the first `n` lines from the `input` reader.
let mut reader = take_lines(input, n, separator);
// Write those bytes to `stdout`.
let stdout = std::io::stdout();
let stdout = io::stdout();
let stdout = stdout.lock();
let mut writer = BufWriter::with_capacity(BUF_SIZE, stdout);
@ -298,10 +297,10 @@ fn catch_too_large_numbers_in_backwards_bytes_or_lines(n: u64) -> Option<usize>
}
}
fn read_but_last_n_bytes(mut input: impl Read, n: u64) -> std::io::Result<u64> {
fn read_but_last_n_bytes(mut input: impl Read, n: u64) -> io::Result<u64> {
let mut bytes_written: u64 = 0;
if let Some(n) = catch_too_large_numbers_in_backwards_bytes_or_lines(n) {
let stdout = std::io::stdout();
let stdout = io::stdout();
let mut stdout = stdout.lock();
bytes_written = copy_all_but_n_bytes(&mut input, &mut stdout, n)
@ -317,8 +316,8 @@ fn read_but_last_n_bytes(mut input: impl Read, n: u64) -> std::io::Result<u64> {
Ok(bytes_written)
}
fn read_but_last_n_lines(mut input: impl Read, n: u64, separator: u8) -> std::io::Result<u64> {
let stdout = std::io::stdout();
fn read_but_last_n_lines(mut input: impl Read, n: u64, separator: u8) -> io::Result<u64> {
let stdout = io::stdout();
let mut stdout = stdout.lock();
if n == 0 {
return io::copy(&mut input, &mut stdout).map_err(wrap_in_stdout_error);
@ -370,7 +369,7 @@ fn read_but_last_n_lines(mut input: impl Read, n: u64, separator: u8) -> std::io
/// assert_eq!(find_nth_line_from_end(&mut input, 4, false).unwrap(), 0);
/// assert_eq!(find_nth_line_from_end(&mut input, 1000, false).unwrap(), 0);
/// ```
fn find_nth_line_from_end<R>(input: &mut R, n: u64, separator: u8) -> std::io::Result<u64>
fn find_nth_line_from_end<R>(input: &mut R, n: u64, separator: u8) -> io::Result<u64>
where
R: Read + Seek,
{
@ -408,14 +407,14 @@ where
}
}
fn is_seekable(input: &mut std::fs::File) -> bool {
fn is_seekable(input: &mut File) -> bool {
let current_pos = input.stream_position();
current_pos.is_ok()
&& input.seek(SeekFrom::End(0)).is_ok()
&& input.seek(SeekFrom::Start(current_pos.unwrap())).is_ok()
}
fn head_backwards_file(input: &mut std::fs::File, options: &HeadOptions) -> std::io::Result<u64> {
fn head_backwards_file(input: &mut File, options: &HeadOptions) -> io::Result<u64> {
let st = input.metadata()?;
let seekable = is_seekable(input);
let blksize_limit = uucore::fs::sane_blksize::sane_blksize_from_metadata(&st);
@ -426,10 +425,7 @@ fn head_backwards_file(input: &mut std::fs::File, options: &HeadOptions) -> std:
}
}
fn head_backwards_without_seek_file(
input: &mut std::fs::File,
options: &HeadOptions,
) -> std::io::Result<u64> {
fn head_backwards_without_seek_file(input: &mut File, options: &HeadOptions) -> io::Result<u64> {
match options.mode {
Mode::AllButLastBytes(n) => read_but_last_n_bytes(input, n),
Mode::AllButLastLines(n) => read_but_last_n_lines(input, n, options.line_ending.into()),
@ -437,10 +433,7 @@ fn head_backwards_without_seek_file(
}
}
fn head_backwards_on_seekable_file(
input: &mut std::fs::File,
options: &HeadOptions,
) -> std::io::Result<u64> {
fn head_backwards_on_seekable_file(input: &mut File, options: &HeadOptions) -> io::Result<u64> {
match options.mode {
Mode::AllButLastBytes(n) => {
let size = input.metadata()?.len();
@ -458,11 +451,11 @@ fn head_backwards_on_seekable_file(
}
}
fn head_file(input: &mut std::fs::File, options: &HeadOptions) -> std::io::Result<u64> {
fn head_file(input: &mut File, options: &HeadOptions) -> io::Result<u64> {
match options.mode {
Mode::FirstBytes(n) => read_n_bytes(input, n),
Mode::FirstLines(n) => read_n_lines(
&mut std::io::BufReader::with_capacity(BUF_SIZE, input),
&mut io::BufReader::with_capacity(BUF_SIZE, input),
n,
options.line_ending.into(),
),
@ -482,7 +475,7 @@ fn uu_head(options: &HeadOptions) -> UResult<()> {
}
println!("==> standard input <==");
}
let stdin = std::io::stdin();
let stdin = io::stdin();
#[cfg(unix)]
{
@ -520,7 +513,7 @@ fn uu_head(options: &HeadOptions) -> UResult<()> {
Ok(())
}
(name, false) => {
let mut file = match std::fs::File::open(name) {
let mut file = match File::open(name) {
Ok(f) => f,
Err(err) => {
show!(err.map_err_context(|| format!(
@ -575,8 +568,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
#[cfg(test)]
mod tests {
use io::Cursor;
use std::ffi::OsString;
use std::io::Cursor;
use super::*;
@ -691,7 +684,7 @@ mod tests {
#[test]
fn read_early_exit() {
let mut empty = std::io::BufReader::new(std::io::Cursor::new(Vec::new()));
let mut empty = io::BufReader::new(Cursor::new(Vec::new()));
assert!(read_n_bytes(&mut empty, 0).is_ok());
assert!(read_n_lines(&mut empty, 0, b'\n').is_ok());
}

View file

@ -675,7 +675,7 @@ fn chown_optional_user_group(path: &Path, b: &Behavior) -> UResult<()> {
return Ok(());
};
let meta = match fs::metadata(path) {
let meta = match metadata(path) {
Ok(meta) => meta,
Err(e) => return Err(InstallError::MetadataFailed(e).into()),
};
@ -859,7 +859,7 @@ fn set_ownership_and_permissions(to: &Path, b: &Behavior) -> UResult<()> {
/// Returns an empty Result or an error in case of failure.
///
fn preserve_timestamps(from: &Path, to: &Path) -> UResult<()> {
let meta = match fs::metadata(from) {
let meta = match metadata(from) {
Ok(meta) => meta,
Err(e) => return Err(InstallError::MetadataFailed(e).into()),
};
@ -940,14 +940,14 @@ fn copy(from: &Path, to: &Path, b: &Behavior) -> UResult<()> {
fn need_copy(from: &Path, to: &Path, b: &Behavior) -> UResult<bool> {
// Attempt to retrieve metadata for the source file.
// If this fails, assume the file needs to be copied.
let from_meta = match fs::metadata(from) {
let from_meta = match metadata(from) {
Ok(meta) => meta,
Err(_) => return Ok(true),
};
// Attempt to retrieve metadata for the destination file.
// If this fails, assume the file needs to be copied.
let to_meta = match fs::metadata(to) {
let to_meta = match metadata(to) {
Ok(meta) => meta,
Err(_) => return Ok(true),
};

View file

@ -74,7 +74,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
} else {
let sig = (sig as i32)
.try_into()
.map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
.map_err(|e| Error::from_raw_os_error(e as i32))?;
Some(sig)
};

View file

@ -439,7 +439,7 @@ fn extract_format(options: &clap::ArgMatches) -> (Format, Option<&'static str>)
(Format::Commas, Some(options::format::COMMAS))
} else if options.get_flag(options::format::COLUMNS) {
(Format::Columns, Some(options::format::COLUMNS))
} else if std::io::stdout().is_terminal() {
} else if stdout().is_terminal() {
(Format::Columns, None)
} else {
(Format::OneLine, None)
@ -559,7 +559,7 @@ fn extract_color(options: &clap::ArgMatches) -> bool {
None => options.contains_id(options::COLOR),
Some(val) => match val.as_str() {
"" | "always" | "yes" | "force" => true,
"auto" | "tty" | "if-tty" => std::io::stdout().is_terminal(),
"auto" | "tty" | "if-tty" => stdout().is_terminal(),
/* "never" | "no" | "none" | */ _ => false,
},
}
@ -578,7 +578,7 @@ fn extract_hyperlink(options: &clap::ArgMatches) -> bool {
match hyperlink {
"always" | "yes" | "force" => true,
"auto" | "tty" | "if-tty" => std::io::stdout().is_terminal(),
"auto" | "tty" | "if-tty" => stdout().is_terminal(),
"never" | "no" | "none" => false,
_ => unreachable!("should be handled by clap"),
}
@ -673,7 +673,7 @@ fn extract_quoting_style(options: &clap::ArgMatches, show_control: bool) -> Quot
// By default, `ls` uses Shell escape quoting style when writing to a terminal file
// descriptor and Literal otherwise.
if std::io::stdout().is_terminal() {
if stdout().is_terminal() {
QuotingStyle::Shell {
escape: true,
always_quote: false,
@ -704,7 +704,7 @@ fn extract_indicator_style(options: &clap::ArgMatches) -> IndicatorStyle {
"never" | "no" | "none" => IndicatorStyle::None,
"always" | "yes" | "force" => IndicatorStyle::Classify,
"auto" | "tty" | "if-tty" => {
if std::io::stdout().is_terminal() {
if stdout().is_terminal() {
IndicatorStyle::Classify
} else {
IndicatorStyle::None
@ -933,7 +933,7 @@ impl Config {
} else if options.get_flag(options::SHOW_CONTROL_CHARS) {
true
} else {
!std::io::stdout().is_terminal()
!stdout().is_terminal()
};
let mut quoting_style = extract_quoting_style(options, show_control);
@ -2386,7 +2386,7 @@ fn get_metadata_with_deref_opt(p_buf: &Path, dereference: bool) -> std::io::Resu
fn display_dir_entry_size(
entry: &PathData,
config: &Config,
out: &mut BufWriter<std::io::Stdout>,
out: &mut BufWriter<Stdout>,
) -> (usize, usize, usize, usize, usize, usize) {
// TODO: Cache/memorize the display_* results so we don't have to recalculate them.
if let Some(md) = entry.get_metadata(out) {
@ -3070,7 +3070,7 @@ fn get_system_time(md: &Metadata, config: &Config) -> Option<SystemTime> {
}
}
fn get_time(md: &Metadata, config: &Config) -> Option<chrono::DateTime<chrono::Local>> {
fn get_time(md: &Metadata, config: &Config) -> Option<DateTime<Local>> {
let time = get_system_time(md, config)?;
Some(time.into())
}

View file

@ -266,7 +266,7 @@ pub fn uu_app() -> Command {
}
#[cfg(not(target_os = "fuchsia"))]
fn setup_term() -> std::io::Stdout {
fn setup_term() -> Stdout {
let stdout = stdout();
terminal::enable_raw_mode().unwrap();
stdout
@ -279,10 +279,10 @@ fn setup_term() -> usize {
}
#[cfg(not(target_os = "fuchsia"))]
fn reset_term(stdout: &mut std::io::Stdout) {
fn reset_term(stdout: &mut Stdout) {
terminal::disable_raw_mode().unwrap();
// Clear the prompt
queue!(stdout, terminal::Clear(ClearType::CurrentLine)).unwrap();
queue!(stdout, Clear(ClearType::CurrentLine)).unwrap();
// Move cursor to the beginning without printing new line
print!("\r");
stdout.flush().unwrap();
@ -313,7 +313,7 @@ fn more(
match search_pattern_in_file(&pager.lines, pat) {
Some(number) => pager.upper_mark = number,
None => {
execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine))?;
execute!(stdout, Clear(ClearType::CurrentLine))?;
stdout.write_all("\rPattern not found\n".as_bytes())?;
pager.content_rows -= 1;
}
@ -321,7 +321,7 @@ fn more(
}
if multiple_file {
execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap();
execute!(stdout, Clear(ClearType::CurrentLine)).unwrap();
stdout.write_all(
MULTI_FILE_TOP_PROMPT
.replace("{}", file.unwrap_or_default())
@ -516,7 +516,7 @@ impl<'a> Pager<'a> {
};
}
fn draw(&mut self, stdout: &mut std::io::Stdout, wrong_key: Option<char>) {
fn draw(&mut self, stdout: &mut Stdout, wrong_key: Option<char>) {
self.draw_lines(stdout);
let lower_mark = self
.line_count
@ -525,8 +525,8 @@ impl<'a> Pager<'a> {
stdout.flush().unwrap();
}
fn draw_lines(&mut self, stdout: &mut std::io::Stdout) {
execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap();
fn draw_lines(&mut self, stdout: &mut Stdout) {
execute!(stdout, Clear(ClearType::CurrentLine)).unwrap();
self.line_squeezed = 0;
let mut previous_line_blank = false;
@ -611,7 +611,7 @@ fn search_pattern_in_file(lines: &[&str], pattern: &str) -> Option<usize> {
None
}
fn paging_add_back_message(options: &Options, stdout: &mut std::io::Stdout) -> UResult<()> {
fn paging_add_back_message(options: &Options, stdout: &mut Stdout) -> UResult<()> {
if options.lines.is_some() {
execute!(stdout, MoveUp(1))?;
stdout.write_all("\n\r...back 1 page\n".as_bytes())?;

View file

@ -131,7 +131,7 @@ fn replace_fds() -> UResult<()> {
}
fn find_stdout() -> UResult<File> {
let internal_failure_code = match std::env::var("POSIXLY_CORRECT") {
let internal_failure_code = match env::var("POSIXLY_CORRECT") {
Ok(_) => POSIX_NOHUP_FAILURE,
Err(_) => EXIT_CANCELED,
};

View file

@ -153,10 +153,10 @@ fn parse_unit_size_suffix(s: &str) -> Option<usize> {
}
fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
let from = parse_unit(args.get_one::<String>(options::FROM).unwrap())?;
let to = parse_unit(args.get_one::<String>(options::TO).unwrap())?;
let from_unit = parse_unit_size(args.get_one::<String>(options::FROM_UNIT).unwrap())?;
let to_unit = parse_unit_size(args.get_one::<String>(options::TO_UNIT).unwrap())?;
let from = parse_unit(args.get_one::<String>(FROM).unwrap())?;
let to = parse_unit(args.get_one::<String>(TO).unwrap())?;
let from_unit = parse_unit_size(args.get_one::<String>(FROM_UNIT).unwrap())?;
let to_unit = parse_unit_size(args.get_one::<String>(TO_UNIT).unwrap())?;
let transform = TransformOptions {
from,
@ -165,7 +165,7 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
to_unit,
};
let padding = match args.get_one::<String>(options::PADDING) {
let padding = match args.get_one::<String>(PADDING) {
Some(s) => s
.parse::<isize>()
.map_err(|_| s)
@ -177,8 +177,8 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
None => Ok(0),
}?;
let header = if args.value_source(options::HEADER) == Some(ValueSource::CommandLine) {
let value = args.get_one::<String>(options::HEADER).unwrap();
let header = if args.value_source(HEADER) == Some(ValueSource::CommandLine) {
let value = args.get_one::<String>(HEADER).unwrap();
value
.parse::<usize>()
@ -192,7 +192,7 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
Ok(0)
}?;
let fields = args.get_one::<String>(options::FIELD).unwrap().as_str();
let fields = args.get_one::<String>(FIELD).unwrap().as_str();
// a lone "-" means "all fields", even as part of a list of fields
let fields = if fields.split(&[',', ' ']).any(|x| x == "-") {
vec![Range {
@ -203,7 +203,7 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
Range::from_list(fields)?
};
let format = match args.get_one::<String>(options::FORMAT) {
let format = match args.get_one::<String>(FORMAT) {
Some(s) => s.parse()?,
None => FormatOptions::default(),
};
@ -212,18 +212,16 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
return Err("grouping cannot be combined with --to".to_string());
}
let delimiter = args
.get_one::<String>(options::DELIMITER)
.map_or(Ok(None), |arg| {
if arg.len() == 1 {
Ok(Some(arg.to_string()))
} else {
Err("the delimiter must be a single character".to_string())
}
})?;
let delimiter = args.get_one::<String>(DELIMITER).map_or(Ok(None), |arg| {
if arg.len() == 1 {
Ok(Some(arg.to_string()))
} else {
Err("the delimiter must be a single character".to_string())
}
})?;
// unwrap is fine because the argument has a default value
let round = match args.get_one::<String>(options::ROUND).unwrap().as_str() {
let round = match args.get_one::<String>(ROUND).unwrap().as_str() {
"up" => RoundMethod::Up,
"down" => RoundMethod::Down,
"from-zero" => RoundMethod::FromZero,
@ -232,10 +230,9 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
_ => unreachable!("Should be restricted by clap"),
};
let suffix = args.get_one::<String>(options::SUFFIX).cloned();
let suffix = args.get_one::<String>(SUFFIX).cloned();
let invalid =
InvalidModes::from_str(args.get_one::<String>(options::INVALID).unwrap()).unwrap();
let invalid = InvalidModes::from_str(args.get_one::<String>(INVALID).unwrap()).unwrap();
let zero_terminated = args.get_flag(options::ZERO_TERMINATED);
@ -259,7 +256,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let options = parse_options(&matches).map_err(NumfmtError::IllegalArgument)?;
let result = match matches.get_many::<String>(options::NUMBER) {
let result = match matches.get_many::<String>(NUMBER) {
Some(values) => handle_args(values.map(|s| s.as_str()), &options),
None => {
let stdin = std::io::stdin();
@ -286,58 +283,58 @@ pub fn uu_app() -> Command {
.allow_negative_numbers(true)
.infer_long_args(true)
.arg(
Arg::new(options::DELIMITER)
Arg::new(DELIMITER)
.short('d')
.long(options::DELIMITER)
.long(DELIMITER)
.value_name("X")
.help("use X instead of whitespace for field delimiter"),
)
.arg(
Arg::new(options::FIELD)
.long(options::FIELD)
Arg::new(FIELD)
.long(FIELD)
.help("replace the numbers in these input fields; see FIELDS below")
.value_name("FIELDS")
.allow_hyphen_values(true)
.default_value(options::FIELD_DEFAULT),
.default_value(FIELD_DEFAULT),
)
.arg(
Arg::new(options::FORMAT)
.long(options::FORMAT)
Arg::new(FORMAT)
.long(FORMAT)
.help("use printf style floating-point FORMAT; see FORMAT below for details")
.value_name("FORMAT")
.allow_hyphen_values(true),
)
.arg(
Arg::new(options::FROM)
.long(options::FROM)
Arg::new(FROM)
.long(FROM)
.help("auto-scale input numbers to UNITs; see UNIT below")
.value_name("UNIT")
.default_value(options::FROM_DEFAULT),
.default_value(FROM_DEFAULT),
)
.arg(
Arg::new(options::FROM_UNIT)
.long(options::FROM_UNIT)
Arg::new(FROM_UNIT)
.long(FROM_UNIT)
.help("specify the input unit size")
.value_name("N")
.default_value(options::FROM_UNIT_DEFAULT),
.default_value(FROM_UNIT_DEFAULT),
)
.arg(
Arg::new(options::TO)
.long(options::TO)
Arg::new(TO)
.long(TO)
.help("auto-scale output numbers to UNITs; see UNIT below")
.value_name("UNIT")
.default_value(options::TO_DEFAULT),
.default_value(TO_DEFAULT),
)
.arg(
Arg::new(options::TO_UNIT)
.long(options::TO_UNIT)
Arg::new(TO_UNIT)
.long(TO_UNIT)
.help("the output unit size")
.value_name("N")
.default_value(options::TO_UNIT_DEFAULT),
.default_value(TO_UNIT_DEFAULT),
)
.arg(
Arg::new(options::PADDING)
.long(options::PADDING)
Arg::new(PADDING)
.long(PADDING)
.help(
"pad the output to N characters; positive N will \
right-align; negative N will left-align; padding is \
@ -347,20 +344,20 @@ pub fn uu_app() -> Command {
.value_name("N"),
)
.arg(
Arg::new(options::HEADER)
.long(options::HEADER)
Arg::new(HEADER)
.long(HEADER)
.help(
"print (without converting) the first N header lines; \
N defaults to 1 if not specified",
)
.num_args(..=1)
.value_name("N")
.default_missing_value(options::HEADER_DEFAULT)
.default_missing_value(HEADER_DEFAULT)
.hide_default_value(true),
)
.arg(
Arg::new(options::ROUND)
.long(options::ROUND)
Arg::new(ROUND)
.long(ROUND)
.help("use METHOD for rounding when scaling")
.value_name("METHOD")
.default_value("from-zero")
@ -373,8 +370,8 @@ pub fn uu_app() -> Command {
])),
)
.arg(
Arg::new(options::SUFFIX)
.long(options::SUFFIX)
Arg::new(SUFFIX)
.long(SUFFIX)
.help(
"print SUFFIX after each formatted number, and accept \
inputs optionally ending with SUFFIX",
@ -382,8 +379,8 @@ pub fn uu_app() -> Command {
.value_name("SUFFIX"),
)
.arg(
Arg::new(options::INVALID)
.long(options::INVALID)
Arg::new(INVALID)
.long(INVALID)
.help("set the failure mode for invalid input")
.default_value("abort")
.value_parser(["abort", "fail", "warn", "ignore"])
@ -396,11 +393,7 @@ pub fn uu_app() -> Command {
.help("line delimiter is NUL, not newline")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::NUMBER)
.hide(true)
.action(ArgAction::Append),
)
.arg(Arg::new(NUMBER).hide(true).action(ArgAction::Append))
}
#[cfg(test)]

View file

@ -48,7 +48,7 @@ impl MultifileReader<'_> {
}
match self.ni.remove(0) {
InputSource::Stdin => {
self.curr_file = Some(Box::new(BufReader::new(std::io::stdin())));
self.curr_file = Some(Box::new(BufReader::new(io::stdin())));
break;
}
InputSource::FileName(fname) => {

View file

@ -333,7 +333,7 @@ pub fn remove(files: &[&OsStr], options: &Options) -> bool {
/// `path` must be a directory. If there is an error reading the
/// contents of the directory, this returns `false`.
fn is_dir_empty(path: &Path) -> bool {
match std::fs::read_dir(path) {
match fs::read_dir(path) {
Err(_) => false,
Ok(iter) => iter.count() == 0,
}
@ -348,7 +348,7 @@ fn is_readable_metadata(metadata: &Metadata) -> bool {
/// Whether the given file or directory is readable.
#[cfg(unix)]
fn is_readable(path: &Path) -> bool {
match std::fs::metadata(path) {
match fs::metadata(path) {
Err(_) => false,
Ok(metadata) => is_readable_metadata(&metadata),
}
@ -369,7 +369,7 @@ fn is_writable_metadata(metadata: &Metadata) -> bool {
/// Whether the given file or directory is writable.
#[cfg(unix)]
fn is_writable(path: &Path) -> bool {
match std::fs::metadata(path) {
match fs::metadata(path) {
Err(_) => false,
Ok(metadata) => is_writable_metadata(&metadata),
}
@ -391,7 +391,7 @@ fn is_writable(_path: &Path) -> bool {
fn remove_dir_recursive(path: &Path, options: &Options) -> bool {
// Special case: if we cannot access the metadata because the
// filename is too long, fall back to try
// `std::fs::remove_dir_all()`.
// `fs::remove_dir_all()`.
//
// TODO This is a temporary bandage; we shouldn't need to do this
// at all. Instead of using the full path like "x/y/z", which
@ -400,7 +400,7 @@ fn remove_dir_recursive(path: &Path, options: &Options) -> bool {
// path, "z", and know that it is relative to the parent, "x/y".
if let Some(s) = path.to_str() {
if s.len() > 1000 {
match std::fs::remove_dir_all(path) {
match fs::remove_dir_all(path) {
Ok(_) => return false,
Err(e) => {
let e = e.map_err_context(|| format!("cannot remove {}", path.quote()));
@ -432,7 +432,7 @@ fn remove_dir_recursive(path: &Path, options: &Options) -> bool {
// Recursive case: this is a directory.
let mut error = false;
match std::fs::read_dir(path) {
match fs::read_dir(path) {
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {
// This is not considered an error.
}
@ -456,7 +456,7 @@ fn remove_dir_recursive(path: &Path, options: &Options) -> bool {
}
// Try removing the directory itself.
match std::fs::remove_dir(path) {
match fs::remove_dir(path) {
Err(_) if !error && !is_readable(path) => {
// For compatibility with GNU test case
// `tests/rm/unread2.sh`, show "Permission denied" in this

View file

@ -163,7 +163,7 @@ struct Opts {
}
pub fn uu_app() -> Command {
Command::new(uucore::util_name())
Command::new(util_name())
.version(uucore::crate_version!())
.about(ABOUT)
.override_usage(format_usage(USAGE))

View file

@ -152,7 +152,7 @@ pub fn uu_app() -> Command {
.short('e')
.long(options::ECHO)
.help("treat each ARG as an input line")
.action(clap::ArgAction::SetTrue)
.action(ArgAction::SetTrue)
.overrides_with(options::ECHO)
.conflicts_with(options::INPUT_RANGE),
)
@ -170,7 +170,7 @@ pub fn uu_app() -> Command {
.short('n')
.long(options::HEAD_COUNT)
.value_name("COUNT")
.action(clap::ArgAction::Append)
.action(ArgAction::Append)
.help("output at most COUNT lines")
.value_parser(usize::from_str),
)
@ -209,7 +209,7 @@ pub fn uu_app() -> Command {
)
.arg(
Arg::new(options::FILE_OR_ARGS)
.action(clap::ArgAction::Append)
.action(ArgAction::Append)
.value_parser(ValueParser::os_string())
.value_hint(clap::ValueHint::FilePath),
)

View file

@ -90,7 +90,7 @@ fn sleep(args: &[&str]) -> UResult<()> {
}
})
.fold(Duration::ZERO, |acc, n| {
acc.saturating_add(SaturatingInto::<std::time::Duration>::saturating_into(n))
acc.saturating_add(SaturatingInto::<Duration>::saturating_into(n))
});
if arg_error {

View file

@ -50,7 +50,7 @@ fn replace_output_file_in_input_files(
*file = copy.clone().into_os_string();
} else {
let (_file, copy_path) = tmp_dir.next_file()?;
std::fs::copy(file_path, &copy_path)
fs::copy(file_path, &copy_path)
.map_err(|error| SortError::OpenTmpFileFailed { error })?;
*file = copy_path.clone().into_os_string();
copy = Some(copy_path);

View file

@ -1152,7 +1152,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
settings.merge_batch_size = parsed_value;
}
Err(e) => {
let error_message = if *e.kind() == std::num::IntErrorKind::PosOverflow {
let error_message = if *e.kind() == IntErrorKind::PosOverflow {
#[cfg(target_os = "linux")]
{
show_error!("--batch-size argument {} too large", n_merge.quote());
@ -1987,7 +1987,7 @@ mod tests {
fn test_line_size() {
// We should make sure to not regress the size of the Line struct because
// it is unconditional overhead for every line we sort.
assert_eq!(std::mem::size_of::<Line>(), 24);
assert_eq!(size_of::<Line>(), 24);
}
#[test]

View file

@ -21,8 +21,8 @@ use std::fmt::{self, Display, Formatter};
#[derive(Debug)]
pub struct Overflow;
impl fmt::Display for Overflow {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl Display for Overflow {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Overflow")
}
}

View file

@ -133,7 +133,7 @@ pub fn instantiate_current_writer(
.write(true)
.create(true)
.truncate(true)
.open(std::path::Path::new(&filename))
.open(Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,
@ -144,7 +144,7 @@ pub fn instantiate_current_writer(
// re-open file that we previously created to append to it
std::fs::OpenOptions::new()
.append(true)
.open(std::path::Path::new(&filename))
.open(Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,

View file

@ -546,7 +546,7 @@ impl Settings {
/// When using `--filter` option, writing to child command process stdin
/// could fail with BrokenPipe error
/// It can be safely ignored
fn ignorable_io_error(error: &std::io::Error, settings: &Settings) -> bool {
fn ignorable_io_error(error: &io::Error, settings: &Settings) -> bool {
error.kind() == ErrorKind::BrokenPipe && settings.filter.is_some()
}
@ -555,11 +555,7 @@ fn ignorable_io_error(error: &std::io::Error, settings: &Settings) -> bool {
/// If ignorable io error occurs, return number of bytes as if all bytes written
/// Should not be used for Kth chunk number sub-strategies
/// as those do not work with `--filter` option
fn custom_write<T: Write>(
bytes: &[u8],
writer: &mut T,
settings: &Settings,
) -> std::io::Result<usize> {
fn custom_write<T: Write>(bytes: &[u8], writer: &mut T, settings: &Settings) -> io::Result<usize> {
match writer.write(bytes) {
Ok(n) => Ok(n),
Err(e) if ignorable_io_error(&e, settings) => Ok(bytes.len()),
@ -576,7 +572,7 @@ fn custom_write_all<T: Write>(
bytes: &[u8],
writer: &mut T,
settings: &Settings,
) -> std::io::Result<bool> {
) -> io::Result<bool> {
match writer.write_all(bytes) {
Ok(()) => Ok(true),
Err(e) if ignorable_io_error(&e, settings) => Ok(false),
@ -610,7 +606,7 @@ fn get_input_size<R>(
reader: &mut R,
buf: &mut Vec<u8>,
io_blksize: &Option<u64>,
) -> std::io::Result<u64>
) -> io::Result<u64>
where
R: BufRead,
{
@ -734,7 +730,7 @@ impl<'a> ByteChunkWriter<'a> {
impl Write for ByteChunkWriter<'_> {
/// Implements `--bytes=SIZE`
fn write(&mut self, mut buf: &[u8]) -> std::io::Result<usize> {
fn write(&mut self, mut buf: &[u8]) -> io::Result<usize> {
// If the length of `buf` exceeds the number of bytes remaining
// in the current chunk, we will need to write to multiple
// different underlying writers. In that case, each iteration of
@ -753,7 +749,7 @@ impl Write for ByteChunkWriter<'_> {
// Allocate the new file, since at this point we know there are bytes to be written to it.
let filename = self.filename_iterator.next().ok_or_else(|| {
std::io::Error::new(ErrorKind::Other, "output file suffixes exhausted")
io::Error::new(ErrorKind::Other, "output file suffixes exhausted")
})?;
if self.settings.verbose {
println!("creating file {}", filename.quote());
@ -794,7 +790,7 @@ impl Write for ByteChunkWriter<'_> {
}
}
}
fn flush(&mut self) -> std::io::Result<()> {
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
@ -858,7 +854,7 @@ impl<'a> LineChunkWriter<'a> {
impl Write for LineChunkWriter<'_> {
/// Implements `--lines=NUMBER`
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
// If the number of lines in `buf` exceeds the number of lines
// remaining in the current chunk, we will need to write to
// multiple different underlying writers. In that case, each
@ -874,7 +870,7 @@ impl Write for LineChunkWriter<'_> {
if self.num_lines_remaining_in_current_chunk == 0 {
self.num_chunks_written += 1;
let filename = self.filename_iterator.next().ok_or_else(|| {
std::io::Error::new(ErrorKind::Other, "output file suffixes exhausted")
io::Error::new(ErrorKind::Other, "output file suffixes exhausted")
})?;
if self.settings.verbose {
println!("creating file {}", filename.quote());
@ -898,7 +894,7 @@ impl Write for LineChunkWriter<'_> {
Ok(total_bytes_written)
}
fn flush(&mut self) -> std::io::Result<()> {
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
@ -1121,7 +1117,7 @@ where
}
// In Kth chunk of N mode - we will write to stdout instead of to a file.
let mut stdout_writer = std::io::stdout().lock();
let mut stdout_writer = io::stdout().lock();
// In N chunks mode - we will write to `num_chunks` files
let mut out_files: OutFiles = OutFiles::new();
@ -1247,7 +1243,7 @@ where
}
// In Kth chunk of N mode - we will write to stdout instead of to a file.
let mut stdout_writer = std::io::stdout().lock();
let mut stdout_writer = io::stdout().lock();
// In N chunks mode - we will write to `num_chunks` files
let mut out_files: OutFiles = OutFiles::new();
@ -1367,7 +1363,7 @@ where
R: BufRead,
{
// In Kth chunk of N mode - we will write to stdout instead of to a file.
let mut stdout_writer = std::io::stdout().lock();
let mut stdout_writer = io::stdout().lock();
// In N chunks mode - we will write to `num_chunks` files
let mut out_files: OutFiles = OutFiles::new();
@ -1414,7 +1410,7 @@ where
Ok(())
}
/// Like `std::io::Lines`, but includes the line ending character.
/// Like `io::Lines`, but includes the line ending character.
///
/// This struct is generally created by calling `lines_with_sep` on a
/// reader.
@ -1427,7 +1423,7 @@ impl<R> Iterator for LinesWithSep<R>
where
R: BufRead,
{
type Item = std::io::Result<Vec<u8>>;
type Item = io::Result<Vec<u8>>;
/// Read bytes from a buffer up to the requested number of lines.
fn next(&mut self) -> Option<Self::Item> {
@ -1464,8 +1460,7 @@ where
// to be overwritten for sure at the beginning of the loop below
// because we start with `remaining == 0`, indicating that a new
// chunk should start.
let mut writer: BufWriter<Box<dyn Write>> =
BufWriter::new(Box::new(std::io::Cursor::new(vec![])));
let mut writer: BufWriter<Box<dyn Write>> = BufWriter::new(Box::new(io::Cursor::new(vec![])));
let mut remaining = 0;
for line in lines_with_sep(reader, settings.separator) {
@ -1560,11 +1555,11 @@ fn split(settings: &Settings) -> UResult<()> {
}
Strategy::Lines(chunk_size) => {
let mut writer = LineChunkWriter::new(chunk_size, settings)?;
match std::io::copy(&mut reader, &mut writer) {
match io::copy(&mut reader, &mut writer) {
Ok(_) => Ok(()),
Err(e) => match e.kind() {
// TODO Since the writer object controls the creation of
// new files, we need to rely on the `std::io::Result`
// new files, we need to rely on the `io::Result`
// returned by its `write()` method to communicate any
// errors to this calling scope. If a new file cannot be
// created because we have exceeded the number of
@ -1578,11 +1573,11 @@ fn split(settings: &Settings) -> UResult<()> {
}
Strategy::Bytes(chunk_size) => {
let mut writer = ByteChunkWriter::new(chunk_size, settings)?;
match std::io::copy(&mut reader, &mut writer) {
match io::copy(&mut reader, &mut writer) {
Ok(_) => Ok(()),
Err(e) => match e.kind() {
// TODO Since the writer object controls the creation of
// new files, we need to rely on the `std::io::Result`
// new files, we need to rely on the `io::Result`
// returned by its `write()` method to communicate any
// errors to this calling scope. If a new file cannot be
// created because we have exceeded the number of

View file

@ -972,8 +972,7 @@ impl Stater {
'Y' => {
let sec = meta.mtime();
let nsec = meta.mtime_nsec();
let tm =
chrono::DateTime::from_timestamp(sec, nsec as u32).unwrap_or_default();
let tm = DateTime::from_timestamp(sec, nsec as u32).unwrap_or_default();
let tm: DateTime<Local> = tm.into();
match tm.timestamp_nanos_opt() {
None => {
@ -1186,7 +1185,7 @@ const PRETTY_DATETIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S.%f %z";
fn pretty_time(sec: i64, nsec: i64) -> String {
// Return the date in UTC
let tm = chrono::DateTime::from_timestamp(sec, nsec as u32).unwrap_or_default();
let tm = DateTime::from_timestamp(sec, nsec as u32).unwrap_or_default();
let tm: DateTime<Local> = tm.into();
tm.format(PRETTY_DATETIME_FORMAT).to_string()

View file

@ -181,7 +181,7 @@ impl Settings {
settings
}
pub fn from(matches: &clap::ArgMatches) -> UResult<Self> {
pub fn from(matches: &ArgMatches) -> UResult<Self> {
// We're parsing --follow, -F and --retry under the following conditions:
// * -F sets --retry and --follow=name
// * plain --follow or short -f is the same like specifying --follow=descriptor
@ -236,7 +236,7 @@ impl Settings {
// * not applied here but it supports customizable time units and provides better error
// messages
settings.sleep_sec = match DurationParser::without_time_units().parse(source) {
Ok(duration) => SaturatingInto::<std::time::Duration>::saturating_into(duration),
Ok(duration) => SaturatingInto::<Duration>::saturating_into(duration),
Err(_) => {
return Err(UUsageError::new(
1,

View file

@ -229,7 +229,7 @@ impl Observer {
watcher = Box::new(notify::PollWatcher::new(tx, watcher_config).unwrap());
} else {
let tx_clone = tx.clone();
match notify::RecommendedWatcher::new(tx, notify::Config::default()) {
match RecommendedWatcher::new(tx, notify::Config::default()) {
Ok(w) => watcher = Box::new(w),
Err(e) if e.to_string().starts_with("Too many open files") => {
/*

View file

@ -11,11 +11,11 @@ use std::io::Error;
pub type Pid = libc::pid_t;
pub struct ProcessChecker {
pid: self::Pid,
pid: Pid,
}
impl ProcessChecker {
pub fn new(process_id: self::Pid) -> Self {
pub fn new(process_id: Pid) -> Self {
Self { pid: process_id }
}
@ -30,7 +30,7 @@ impl Drop for ProcessChecker {
fn drop(&mut self) {}
}
pub fn supports_pid_checks(pid: self::Pid) -> bool {
pub fn supports_pid_checks(pid: Pid) -> bool {
unsafe { !(libc::kill(pid, 0) != 0 && get_errno() == libc::ENOSYS) }
}

View file

@ -16,7 +16,7 @@ pub struct ProcessChecker {
}
impl ProcessChecker {
pub fn new(process_id: self::Pid) -> Self {
pub fn new(process_id: Pid) -> Self {
#[allow(non_snake_case)]
let FALSE: BOOL = 0;
let h = unsafe { OpenProcess(PROCESS_SYNCHRONIZE, FALSE, process_id) };
@ -47,6 +47,6 @@ impl Drop for ProcessChecker {
}
}
pub fn supports_pid_checks(_pid: self::Pid) -> bool {
pub fn supports_pid_checks(_pid: Pid) -> bool {
true
}

View file

@ -163,7 +163,7 @@ fn tail_file(
true,
)?;
}
Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {
Err(e) if e.kind() == ErrorKind::PermissionDenied => {
observer.add_bad_path(path, input.display_name.as_str(), false)?;
show!(e.map_err_context(|| {
format!("cannot open '{}' for reading", input.display_name)
@ -290,7 +290,7 @@ fn forwards_thru_file(
reader: &mut impl Read,
num_delimiters: u64,
delimiter: u8,
) -> std::io::Result<usize> {
) -> io::Result<usize> {
// If num_delimiters == 0, always return 0.
if num_delimiters == 0 {
return Ok(0);
@ -405,7 +405,7 @@ fn bounded_tail(file: &mut File, settings: &Settings) {
// Print the target section of the file.
let stdout = stdout();
let mut stdout = stdout.lock();
std::io::copy(file, &mut stdout).unwrap();
io::copy(file, &mut stdout).unwrap();
}
fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UResult<()> {

View file

@ -66,7 +66,7 @@ impl Config {
Some(signal_value) => signal_value,
}
}
_ => uucore::signals::signal_by_name_or_value("TERM").unwrap(),
_ => signal_by_name_or_value("TERM").unwrap(),
};
let kill_after = match options.get_one::<String>(options::KILL_AFTER) {

View file

@ -443,7 +443,7 @@ fn touch_file(
};
if let Err(e) = metadata_result {
if e.kind() != std::io::ErrorKind::NotFound {
if e.kind() != ErrorKind::NotFound {
return Err(e.map_err_context(|| format!("setting times of {}", filename.quote())));
}
@ -687,7 +687,7 @@ fn parse_timestamp(s: &str) -> UResult<FileTime> {
let local = NaiveDateTime::parse_from_str(&ts, format)
.map_err(|_| USimpleError::new(1, format!("invalid date ts format {}", ts.quote())))?;
let LocalResult::Single(mut local) = chrono::Local.from_local_datetime(&local) else {
let LocalResult::Single(mut local) = Local.from_local_datetime(&local) else {
return Err(USimpleError::new(
1,
format!("invalid date ts format {}", ts.quote()),

View file

@ -180,7 +180,7 @@ pub fn uu_app() -> Command {
/// size of the file.
fn file_truncate(filename: &str, create: bool, size: u64) -> UResult<()> {
#[cfg(unix)]
if let Ok(metadata) = std::fs::metadata(filename) {
if let Ok(metadata) = metadata(filename) {
if metadata.file_type().is_fifo() {
return Err(USimpleError::new(
1,

View file

@ -311,7 +311,7 @@ fn next_char_info(uflag: bool, buf: &[u8], byte: usize) -> (CharType, usize, usi
#[allow(clippy::cognitive_complexity)]
fn unexpand_line(
buf: &mut Vec<u8>,
output: &mut BufWriter<std::io::Stdout>,
output: &mut BufWriter<Stdout>,
options: &Options,
lastcol: usize,
ts: &[usize],

View file

@ -805,7 +805,7 @@ fn files0_iter<'a>(
}),
);
// Loop until there is an error; yield that error and then nothing else.
std::iter::from_fn(move || {
iter::from_fn(move || {
let next = i.as_mut().and_then(Iterator::next);
if matches!(next, Some(Err(_)) | None) {
i = None;