Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/uu/comm/src/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fn open_file(name: &str, line_ending: LineEnding) -> io::Result<LineReader> {
Ok(LineReader::new(Input::Stdin(stdin()), line_ending))
} else {
if metadata(name)?.is_dir() {
return Err(io::Error::new(io::ErrorKind::Other, "Is a directory"));
return Err(io::Error::other("Is a directory"));
}
let f = File::open(name)?;
Ok(LineReader::new(
Expand Down
2 changes: 1 addition & 1 deletion src/uu/cp/src/copydir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn get_local_to_root_parent(
/// Given an iterator, return all its items except the last.
fn skip_last<T>(mut iter: impl Iterator<Item = T>) -> impl Iterator<Item = T> {
let last = iter.next();
iter.scan(last, |state, item| std::mem::replace(state, Some(item)))
iter.scan(last, |state, item| state.replace(item))
}

/// Paths that are invariant throughout the traversal when copying a directory.
Expand Down
25 changes: 10 additions & 15 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,20 +580,18 @@ fn read_files_from(file_name: &str) -> Result<Vec<PathBuf>, std::io::Error> {
// First, check if the file_name is a directory
let path = PathBuf::from(file_name);
if path.is_dir() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("{file_name}: read error: Is a directory"),
));
return Err(std::io::Error::other(format!(
"{file_name}: read error: Is a directory"
)));
}

// Attempt to open the file and handle the error if it does not exist
match File::open(file_name) {
Ok(file) => Box::new(BufReader::new(file)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("cannot open '{file_name}' for reading: No such file or directory"),
));
return Err(std::io::Error::other(format!(
"cannot open '{file_name}' for reading: No such file or directory"
)));
}
Err(e) => return Err(e),
}
Expand Down Expand Up @@ -637,13 +635,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let files = if let Some(file_from) = matches.get_one::<String>(options::FILES0_FROM) {
if file_from == "-" && matches.get_one::<String>(options::FILE).is_some() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"extra operand {}\nfile operands cannot be combined with --files0-from",
matches.get_one::<String>(options::FILE).unwrap().quote()
),
)
return Err(std::io::Error::other(format!(
"extra operand {}\nfile operands cannot be combined with --files0-from",
matches.get_one::<String>(options::FILE).unwrap().quote()
))
.into());
}

Expand Down
26 changes: 11 additions & 15 deletions src/uu/echo/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,17 @@ fn execute(
}

fn bytes_from_os_string(input: &OsStr) -> Option<&[u8]> {
let option = {
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;

Some(input.as_bytes())
}

#[cfg(not(target_family = "unix"))]
{
// TODO
// Verify that this works correctly on these platforms
input.to_str().map(|st| st.as_bytes())
}
};
Some(input.as_bytes())
}

option
#[cfg(not(target_family = "unix"))]
{
// TODO
// Verify that this works correctly on these platforms
input.to_str().map(|st| st.as_bytes())
}
}
3 changes: 1 addition & 2 deletions src/uu/env/src/native_int_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ impl<'a> NativeStr<'a> {
match &self.native {
Cow::Borrowed(b) => {
let slice = f_borrow(b);
let os_str = slice.map(|x| from_native_int_representation(Cow::Borrowed(x)));
os_str
slice.map(|x| from_native_int_representation(Cow::Borrowed(x)))
}
Cow::Owned(o) => {
let slice = f_owned(o);
Expand Down
4 changes: 1 addition & 3 deletions src/uu/fmt/src/linebreak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ fn break_knuth_plass<'a, T: Clone + Iterator<Item = &'a WordInfo<'a>>>(

// We find identical breakpoints here by comparing addresses of the references.
// This is OK because the backing vector is not mutating once we are linebreaking.
let winfo_ptr = winfo as *const _;
let next_break_ptr = next_break as *const _;
if winfo_ptr == next_break_ptr {
if std::ptr::eq(winfo, next_break) {
// OK, we found the matching word
if break_before {
write_newline(args.indent_str, args.ostream)?;
Expand Down
13 changes: 6 additions & 7 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ fn handle_two_paths(source: &Path, target: &Path, opts: &Options) -> UResult<()>
OverwriteMode::NoClobber => return Ok(()),
OverwriteMode::Interactive => {
if !prompt_yes!("overwrite {}? ", target.quote()) {
return Err(io::Error::new(io::ErrorKind::Other, "").into());
return Err(io::Error::other("").into());
}
}
OverwriteMode::Force => {}
Expand Down Expand Up @@ -609,7 +609,7 @@ fn rename(

if opts.update == UpdateMode::ReplaceNoneFail {
let err_msg = format!("not replacing {}", to.quote());
return Err(io::Error::new(io::ErrorKind::Other, err_msg));
return Err(io::Error::other(err_msg));
}

match opts.overwrite {
Expand All @@ -621,7 +621,7 @@ fn rename(
}
OverwriteMode::Interactive => {
if !prompt_yes!("overwrite {}?", to.quote()) {
return Err(io::Error::new(io::ErrorKind::Other, ""));
return Err(io::Error::other(""));
}
}
OverwriteMode::Force => {}
Expand All @@ -640,7 +640,7 @@ fn rename(
if is_empty_dir(to) {
fs::remove_dir(to)?;
} else {
return Err(io::Error::new(io::ErrorKind::Other, "Directory not empty"));
return Err(io::Error::other("Directory not empty"));
}
}
}
Expand Down Expand Up @@ -756,7 +756,7 @@ fn rename_with_fallback(
io::ErrorKind::PermissionDenied,
"Permission denied",
)),
_ => Err(io::Error::new(io::ErrorKind::Other, format!("{err:?}"))),
_ => Err(io::Error::other(format!("{err:?}"))),
};
}
} else {
Expand Down Expand Up @@ -811,8 +811,7 @@ fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> {
}
#[cfg(not(any(windows, unix)))]
{
return Err(io::Error::new(
io::ErrorKind::Other,
return Err(io::Error::other(
"your operating system does not support symlinks",
));
}
Expand Down
14 changes: 3 additions & 11 deletions src/uu/split/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// file that was distributed with this source code.
use std::env;
use std::io::Write;
use std::io::{BufWriter, Error, ErrorKind, Result};
use std::io::{BufWriter, Error, Result};
use std::path::Path;
use std::process::{Child, Command, Stdio};
use uucore::error::USimpleError;
Expand Down Expand Up @@ -134,22 +134,14 @@ pub fn instantiate_current_writer(
.create(true)
.truncate(true)
.open(Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,
format!("unable to open '{filename}'; aborting"),
)
})?
.map_err(|_| Error::other(format!("unable to open '{filename}'; aborting")))?
} else {
// re-open file that we previously created to append to it
std::fs::OpenOptions::new()
.append(true)
.open(Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,
format!("unable to re-open '{filename}'; aborting"),
)
Error::other(format!("unable to re-open '{filename}'; aborting"))
})?
};
Ok(BufWriter::new(Box::new(file) as Box<dyn Write>))
Expand Down
16 changes: 3 additions & 13 deletions src/uu/split/src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use std::io::Write;
use std::io::{BufWriter, Error, ErrorKind, Result};
use std::io::{BufWriter, Error, Result};
use std::path::Path;
use uucore::fs;

Expand All @@ -23,23 +23,13 @@ pub fn instantiate_current_writer(
.create(true)
.truncate(true)
.open(Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,
format!("unable to open '{filename}'; aborting"),
)
})?
.map_err(|_| Error::other(format!("unable to open '{filename}'; aborting")))?
} else {
// re-open file that we previously created to append to it
std::fs::OpenOptions::new()
.append(true)
.open(Path::new(&filename))
.map_err(|_| {
Error::new(
ErrorKind::Other,
format!("unable to re-open '{filename}'; aborting"),
)
})?
.map_err(|_| Error::other(format!("unable to re-open '{filename}'; aborting")))?
};
Ok(BufWriter::new(Box::new(file) as Box<dyn Write>))
}
Expand Down
37 changes: 18 additions & 19 deletions src/uu/split/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,9 @@ impl Settings {
is_new: bool,
) -> io::Result<BufWriter<Box<dyn Write>>> {
if platform::paths_refer_to_same_file(&self.input, filename) {
return Err(io::Error::new(
ErrorKind::Other,
format!("'{filename}' would overwrite input; aborting"),
));
return Err(io::Error::other(format!(
"'{filename}' would overwrite input; aborting"
)));
}

platform::instantiate_current_writer(self.filter.as_deref(), filename, is_new)
Expand Down Expand Up @@ -638,10 +637,9 @@ where
} else if input == "-" {
// STDIN stream that did not fit all content into a buffer
// Most likely continuous/infinite input stream
return Err(io::Error::new(
ErrorKind::Other,
format!("{input}: cannot determine input size"),
));
return Err(io::Error::other(format!(
"{input}: cannot determine input size"
)));
} else {
// Could be that file size is larger than set read limit
// Get the file size from filesystem metadata
Expand All @@ -664,10 +662,9 @@ where
// Give up and return an error
// TODO It might be possible to do more here
// to address all possible file types and edge cases
return Err(io::Error::new(
ErrorKind::Other,
format!("{input}: cannot determine file size"),
));
return Err(io::Error::other(format!(
"{input}: cannot determine file size"
)));
}
}
}
Expand Down Expand Up @@ -750,9 +747,10 @@ impl Write for ByteChunkWriter<'_> {
self.num_bytes_remaining_in_current_chunk = self.chunk_size;

// 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(|| {
io::Error::new(ErrorKind::Other, "output file suffixes exhausted")
})?;
let filename = self
.filename_iterator
.next()
.ok_or_else(|| io::Error::other("output file suffixes exhausted"))?;
if self.settings.verbose {
println!("creating file {}", filename.quote());
}
Expand Down Expand Up @@ -871,9 +869,10 @@ impl Write for LineChunkWriter<'_> {
// corresponding writer.
if self.num_lines_remaining_in_current_chunk == 0 {
self.num_chunks_written += 1;
let filename = self.filename_iterator.next().ok_or_else(|| {
io::Error::new(ErrorKind::Other, "output file suffixes exhausted")
})?;
let filename = self
.filename_iterator
.next()
.ok_or_else(|| io::Error::other("output file suffixes exhausted"))?;
if self.settings.verbose {
println!("creating file {}", filename.quote());
}
Expand Down Expand Up @@ -948,7 +947,7 @@ impl ManageOutFiles for OutFiles {
// This object is responsible for creating the filename for each chunk
let mut filename_iterator: FilenameIterator<'_> =
FilenameIterator::new(&settings.prefix, &settings.suffix)
.map_err(|e| io::Error::new(ErrorKind::Other, format!("{e}")))?;
.map_err(|e| io::Error::other(format!("{e}")))?;
let mut out_files: Self = Self::new();
for _ in 0..num_files {
let filename = filename_iterator
Expand Down
2 changes: 1 addition & 1 deletion src/uu/touch/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ fn touch_file(
false
};
if is_directory {
let custom_err = Error::new(ErrorKind::Other, "No such file or directory");
let custom_err = Error::other("No such file or directory");
return Err(
custom_err.map_err_context(|| format!("cannot touch {}", filename.quote()))
);
Expand Down
2 changes: 1 addition & 1 deletion src/uu/unexpand/src/unexpand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn tabstops_parse(s: &str) -> Result<Vec<usize>, ParseError> {
}
}

if nums.iter().any(|&n| n == 0) {
if nums.contains(&0) {
return Err(ParseError::TabSizeCannotBeZero);
}

Expand Down
3 changes: 1 addition & 2 deletions src/uu/wc/src/wc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,7 @@ fn files0_iter<'a>(
// ...Windows does not, we must go through Strings.
#[cfg(not(unix))]
{
let s = String::from_utf8(p)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let s = String::from_utf8(p).map_err(io::Error::other)?;
Ok(Input::Path(PathBuf::from(s).into()))
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/uucore/src/lib/features/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,19 +780,18 @@ fn get_input_file(filename: &OsStr) -> UResult<Box<dyn Read>> {
match File::open(filename) {
Ok(f) => {
if f.metadata()?.is_dir() {
Err(io::Error::new(
io::ErrorKind::Other,
format!("{}: Is a directory", filename.to_string_lossy()),
Err(
io::Error::other(format!("{}: Is a directory", filename.to_string_lossy()))
.into(),
)
.into())
} else {
Ok(Box::new(f))
}
}
Err(_) => Err(io::Error::new(
io::ErrorKind::Other,
format!("{}: No such file or directory", filename.to_string_lossy()),
)
Err(_) => Err(io::Error::other(format!(
"{}: No such file or directory",
filename.to_string_lossy()
))
.into()),
}
}
Expand Down
Loading
Loading