Skip to content

Commit 11cd0b1

Browse files
committed
replace Error::new(ErrorKind::Other, _) as suggested by clippy
See also: https://rust-lang.github.io/rust-clippy/master/index.html#io_other_error
1 parent e4c7bd0 commit 11cd0b1

File tree

10 files changed

+68
-97
lines changed

10 files changed

+68
-97
lines changed

src/uu/comm/src/comm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn open_file(name: &str, line_ending: LineEnding) -> io::Result<LineReader> {
267267
Ok(LineReader::new(Input::Stdin(stdin()), line_ending))
268268
} else {
269269
if metadata(name)?.is_dir() {
270-
return Err(io::Error::new(io::ErrorKind::Other, "Is a directory"));
270+
return Err(io::Error::other("Is a directory"));
271271
}
272272
let f = File::open(name)?;
273273
Ok(LineReader::new(

src/uu/du/src/du.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -580,20 +580,18 @@ fn read_files_from(file_name: &str) -> Result<Vec<PathBuf>, std::io::Error> {
580580
// First, check if the file_name is a directory
581581
let path = PathBuf::from(file_name);
582582
if path.is_dir() {
583-
return Err(std::io::Error::new(
584-
std::io::ErrorKind::Other,
585-
format!("{file_name}: read error: Is a directory"),
586-
));
583+
return Err(std::io::Error::other(format!(
584+
"{file_name}: read error: Is a directory"
585+
)));
587586
}
588587

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

638636
let files = if let Some(file_from) = matches.get_one::<String>(options::FILES0_FROM) {
639637
if file_from == "-" && matches.get_one::<String>(options::FILE).is_some() {
640-
return Err(std::io::Error::new(
641-
std::io::ErrorKind::Other,
642-
format!(
643-
"extra operand {}\nfile operands cannot be combined with --files0-from",
644-
matches.get_one::<String>(options::FILE).unwrap().quote()
645-
),
646-
)
638+
return Err(std::io::Error::other(format!(
639+
"extra operand {}\nfile operands cannot be combined with --files0-from",
640+
matches.get_one::<String>(options::FILE).unwrap().quote()
641+
))
647642
.into());
648643
}
649644

src/uu/mv/src/mv.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ fn handle_two_paths(source: &Path, target: &Path, opts: &Options) -> UResult<()>
370370
OverwriteMode::NoClobber => return Ok(()),
371371
OverwriteMode::Interactive => {
372372
if !prompt_yes!("overwrite {}? ", target.quote()) {
373-
return Err(io::Error::new(io::ErrorKind::Other, "").into());
373+
return Err(io::Error::other("").into());
374374
}
375375
}
376376
OverwriteMode::Force => {}
@@ -609,7 +609,7 @@ fn rename(
609609

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

615615
match opts.overwrite {
@@ -621,7 +621,7 @@ fn rename(
621621
}
622622
OverwriteMode::Interactive => {
623623
if !prompt_yes!("overwrite {}?", to.quote()) {
624-
return Err(io::Error::new(io::ErrorKind::Other, ""));
624+
return Err(io::Error::other(""));
625625
}
626626
}
627627
OverwriteMode::Force => {}
@@ -640,7 +640,7 @@ fn rename(
640640
if is_empty_dir(to) {
641641
fs::remove_dir(to)?;
642642
} else {
643-
return Err(io::Error::new(io::ErrorKind::Other, "Directory not empty"));
643+
return Err(io::Error::other("Directory not empty"));
644644
}
645645
}
646646
}
@@ -756,7 +756,7 @@ fn rename_with_fallback(
756756
io::ErrorKind::PermissionDenied,
757757
"Permission denied",
758758
)),
759-
_ => Err(io::Error::new(io::ErrorKind::Other, format!("{err:?}"))),
759+
_ => Err(io::Error::other(format!("{err:?}"))),
760760
};
761761
}
762762
} else {
@@ -811,8 +811,7 @@ fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> {
811811
}
812812
#[cfg(not(any(windows, unix)))]
813813
{
814-
return Err(io::Error::new(
815-
io::ErrorKind::Other,
814+
return Err(io::Error::other(
816815
"your operating system does not support symlinks",
817816
));
818817
}

src/uu/split/src/platform/unix.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// file that was distributed with this source code.
55
use std::env;
66
use std::io::Write;
7-
use std::io::{BufWriter, Error, ErrorKind, Result};
7+
use std::io::{BufWriter, Error, Result};
88
use std::path::Path;
99
use std::process::{Child, Command, Stdio};
1010
use uucore::error::USimpleError;
@@ -134,22 +134,14 @@ pub fn instantiate_current_writer(
134134
.create(true)
135135
.truncate(true)
136136
.open(Path::new(&filename))
137-
.map_err(|_| {
138-
Error::new(
139-
ErrorKind::Other,
140-
format!("unable to open '{filename}'; aborting"),
141-
)
142-
})?
137+
.map_err(|_| Error::other(format!("unable to open '{filename}'; aborting")))?
143138
} else {
144139
// re-open file that we previously created to append to it
145140
std::fs::OpenOptions::new()
146141
.append(true)
147142
.open(Path::new(&filename))
148143
.map_err(|_| {
149-
Error::new(
150-
ErrorKind::Other,
151-
format!("unable to re-open '{filename}'; aborting"),
152-
)
144+
Error::other(format!("unable to re-open '{filename}'; aborting"))
153145
})?
154146
};
155147
Ok(BufWriter::new(Box::new(file) as Box<dyn Write>))

src/uu/split/src/platform/windows.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55
use std::io::Write;
6-
use std::io::{BufWriter, Error, ErrorKind, Result};
6+
use std::io::{BufWriter, Error, Result};
77
use std::path::Path;
88
use uucore::fs;
99

@@ -23,23 +23,13 @@ pub fn instantiate_current_writer(
2323
.create(true)
2424
.truncate(true)
2525
.open(Path::new(&filename))
26-
.map_err(|_| {
27-
Error::new(
28-
ErrorKind::Other,
29-
format!("unable to open '{filename}'; aborting"),
30-
)
31-
})?
26+
.map_err(|_| Error::other(format!("unable to open '{filename}'; aborting")))?
3227
} else {
3328
// re-open file that we previously created to append to it
3429
std::fs::OpenOptions::new()
3530
.append(true)
3631
.open(Path::new(&filename))
37-
.map_err(|_| {
38-
Error::new(
39-
ErrorKind::Other,
40-
format!("unable to re-open '{filename}'; aborting"),
41-
)
42-
})?
32+
.map_err(|_| Error::other(format!("unable to re-open '{filename}'; aborting")))?
4333
};
4434
Ok(BufWriter::new(Box::new(file) as Box<dyn Write>))
4535
}

src/uu/split/src/split.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,9 @@ impl Settings {
535535
is_new: bool,
536536
) -> io::Result<BufWriter<Box<dyn Write>>> {
537537
if platform::paths_refer_to_same_file(&self.input, filename) {
538-
return Err(io::Error::new(
539-
ErrorKind::Other,
540-
format!("'{filename}' would overwrite input; aborting"),
541-
));
538+
return Err(io::Error::other(format!(
539+
"'{filename}' would overwrite input; aborting"
540+
)));
542541
}
543542

544543
platform::instantiate_current_writer(self.filter.as_deref(), filename, is_new)
@@ -638,10 +637,9 @@ where
638637
} else if input == "-" {
639638
// STDIN stream that did not fit all content into a buffer
640639
// Most likely continuous/infinite input stream
641-
return Err(io::Error::new(
642-
ErrorKind::Other,
643-
format!("{input}: cannot determine input size"),
644-
));
640+
return Err(io::Error::other(format!(
641+
"{input}: cannot determine input size"
642+
)));
645643
} else {
646644
// Could be that file size is larger than set read limit
647645
// Get the file size from filesystem metadata
@@ -664,10 +662,9 @@ where
664662
// Give up and return an error
665663
// TODO It might be possible to do more here
666664
// to address all possible file types and edge cases
667-
return Err(io::Error::new(
668-
ErrorKind::Other,
669-
format!("{input}: cannot determine file size"),
670-
));
665+
return Err(io::Error::other(format!(
666+
"{input}: cannot determine file size"
667+
)));
671668
}
672669
}
673670
}
@@ -750,9 +747,10 @@ impl Write for ByteChunkWriter<'_> {
750747
self.num_bytes_remaining_in_current_chunk = self.chunk_size;
751748

752749
// Allocate the new file, since at this point we know there are bytes to be written to it.
753-
let filename = self.filename_iterator.next().ok_or_else(|| {
754-
io::Error::new(ErrorKind::Other, "output file suffixes exhausted")
755-
})?;
750+
let filename = self
751+
.filename_iterator
752+
.next()
753+
.ok_or_else(|| io::Error::other("output file suffixes exhausted"))?;
756754
if self.settings.verbose {
757755
println!("creating file {}", filename.quote());
758756
}
@@ -871,9 +869,10 @@ impl Write for LineChunkWriter<'_> {
871869
// corresponding writer.
872870
if self.num_lines_remaining_in_current_chunk == 0 {
873871
self.num_chunks_written += 1;
874-
let filename = self.filename_iterator.next().ok_or_else(|| {
875-
io::Error::new(ErrorKind::Other, "output file suffixes exhausted")
876-
})?;
872+
let filename = self
873+
.filename_iterator
874+
.next()
875+
.ok_or_else(|| io::Error::other("output file suffixes exhausted"))?;
877876
if self.settings.verbose {
878877
println!("creating file {}", filename.quote());
879878
}
@@ -948,7 +947,7 @@ impl ManageOutFiles for OutFiles {
948947
// This object is responsible for creating the filename for each chunk
949948
let mut filename_iterator: FilenameIterator<'_> =
950949
FilenameIterator::new(&settings.prefix, &settings.suffix)
951-
.map_err(|e| io::Error::new(ErrorKind::Other, format!("{e}")))?;
950+
.map_err(|e| io::Error::other(format!("{e}")))?;
952951
let mut out_files: Self = Self::new();
953952
for _ in 0..num_files {
954953
let filename = filename_iterator

src/uu/touch/src/touch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ fn touch_file(
477477
false
478478
};
479479
if is_directory {
480-
let custom_err = Error::new(ErrorKind::Other, "No such file or directory");
480+
let custom_err = Error::other("No such file or directory");
481481
return Err(
482482
custom_err.map_err_context(|| format!("cannot touch {}", filename.quote()))
483483
);

src/uu/wc/src/wc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,7 @@ fn files0_iter<'a>(
794794
// ...Windows does not, we must go through Strings.
795795
#[cfg(not(unix))]
796796
{
797-
let s = String::from_utf8(p)
798-
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
797+
let s = String::from_utf8(p).map_err(io::Error::other)?;
799798
Ok(Input::Path(PathBuf::from(s).into()))
800799
}
801800
}

src/uucore/src/lib/features/checksum.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -780,19 +780,18 @@ fn get_input_file(filename: &OsStr) -> UResult<Box<dyn Read>> {
780780
match File::open(filename) {
781781
Ok(f) => {
782782
if f.metadata()?.is_dir() {
783-
Err(io::Error::new(
784-
io::ErrorKind::Other,
785-
format!("{}: Is a directory", filename.to_string_lossy()),
783+
Err(
784+
io::Error::other(format!("{}: Is a directory", filename.to_string_lossy()))
785+
.into(),
786786
)
787-
.into())
788787
} else {
789788
Ok(Box::new(f))
790789
}
791790
}
792-
Err(_) => Err(io::Error::new(
793-
io::ErrorKind::Other,
794-
format!("{}: No such file or directory", filename.to_string_lossy()),
795-
)
791+
Err(_) => Err(io::Error::other(format!(
792+
"{}: No such file or directory",
793+
filename.to_string_lossy()
794+
))
796795
.into()),
797796
}
798797
}

tests/uutests/src/lib/util.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,10 +2286,10 @@ impl UChild {
22862286
if start.elapsed() < timeout {
22872287
self.delay(10);
22882288
} else {
2289-
return Err(io::Error::new(
2290-
io::ErrorKind::Other,
2291-
format!("kill: Timeout of '{}s' reached", timeout.as_secs_f64()),
2292-
));
2289+
return Err(io::Error::other(format!(
2290+
"kill: Timeout of '{}s' reached",
2291+
timeout.as_secs_f64()
2292+
)));
22932293
}
22942294
hint::spin_loop();
22952295
}
@@ -2354,10 +2354,10 @@ impl UChild {
23542354
if start.elapsed() < timeout {
23552355
self.delay(10);
23562356
} else {
2357-
return Err(io::Error::new(
2358-
io::ErrorKind::Other,
2359-
format!("kill: Timeout of '{}s' reached", timeout.as_secs_f64()),
2360-
));
2357+
return Err(io::Error::other(format!(
2358+
"kill: Timeout of '{}s' reached",
2359+
timeout.as_secs_f64()
2360+
)));
23612361
}
23622362
hint::spin_loop();
23632363
}
@@ -2446,10 +2446,10 @@ impl UChild {
24462446
handle.join().unwrap().unwrap();
24472447
result
24482448
}
2449-
Err(RecvTimeoutError::Timeout) => Err(io::Error::new(
2450-
io::ErrorKind::Other,
2451-
format!("wait: Timeout of '{}s' reached", timeout.as_secs_f64()),
2452-
)),
2449+
Err(RecvTimeoutError::Timeout) => Err(io::Error::other(format!(
2450+
"wait: Timeout of '{}s' reached",
2451+
timeout.as_secs_f64()
2452+
))),
24532453
Err(RecvTimeoutError::Disconnected) => {
24542454
handle.join().expect("Panic caused disconnect").unwrap();
24552455
panic!("Error receiving from waiting thread because of unexpected disconnect");
@@ -2691,10 +2691,9 @@ impl UChild {
26912691
.name("pipe_in".to_string())
26922692
.spawn(
26932693
move || match writer.write_all(&content).and_then(|()| writer.flush()) {
2694-
Err(error) if !ignore_stdin_write_error => Err(io::Error::new(
2695-
io::ErrorKind::Other,
2696-
format!("failed to write to stdin of child: {error}"),
2697-
)),
2694+
Err(error) if !ignore_stdin_write_error => Err(io::Error::other(format!(
2695+
"failed to write to stdin of child: {error}"
2696+
))),
26982697
Ok(()) | Err(_) => Ok(()),
26992698
},
27002699
)
@@ -2736,10 +2735,9 @@ impl UChild {
27362735
let mut writer = self.access_stdin_as_writer();
27372736

27382737
match writer.write_all(&data.into()).and_then(|()| writer.flush()) {
2739-
Err(error) if !ignore_stdin_write_error => Err(io::Error::new(
2740-
io::ErrorKind::Other,
2741-
format!("failed to write to stdin of child: {error}"),
2742-
)),
2738+
Err(error) if !ignore_stdin_write_error => Err(io::Error::other(format!(
2739+
"failed to write to stdin of child: {error}"
2740+
))),
27432741
Ok(()) | Err(_) => Ok(()),
27442742
}
27452743
}

0 commit comments

Comments
 (0)