Fix some clippy warnings

Fixed with `cargo clippy --features unix  --fix`
and manually
This commit is contained in:
Sylvestre Ledru
2022-10-13 09:07:22 +02:00
parent 32b1fc6420
commit 6e14dea73b
32 changed files with 122 additions and 126 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ const USAGE: &str = "\
fn parse_gid_and_uid(matches: &ArgMatches) -> UResult<(Option<u32>, Option<u32>, IfFrom)> {
let dest_gid = if let Some(file) = matches.get_one::<String>(options::REFERENCE) {
fs::metadata(&file)
fs::metadata(file)
.map(|meta| Some(meta.gid()))
.map_err_context(|| format!("failed to get attributes of {}", file.quote()))?
} else {
+1 -1
View File
@@ -40,7 +40,7 @@ fn parse_gid_uid_and_filter(matches: &ArgMatches) -> UResult<(Option<u32>, Optio
let dest_uid: Option<u32>;
let dest_gid: Option<u32>;
if let Some(file) = matches.get_one::<String>(options::REFERENCE) {
let meta = fs::metadata(&file)
let meta = fs::metadata(file)
.map_err_context(|| format!("failed to get attributes of {}", file.quote()))?;
dest_gid = Some(meta.gid());
dest_uid = Some(meta.uid());
+1 -1
View File
@@ -124,7 +124,7 @@ fn open_file(name: &str) -> io::Result<LineReader> {
match name {
"-" => Ok(LineReader::Stdin(stdin())),
_ => {
let f = File::open(&Path::new(name))?;
let f = File::open(Path::new(name))?;
Ok(LineReader::FileIn(BufReader::new(f)))
}
}
+8 -8
View File
@@ -1115,7 +1115,7 @@ fn copy_directory(
.follow_links(options.dereference)
{
let p = or_continue!(path);
let path = current_dir.join(&p.path());
let path = current_dir.join(p.path());
let local_to_root_parent = match root_parent {
Some(parent) => {
@@ -1131,7 +1131,7 @@ fn copy_directory(
}
#[cfg(not(windows))]
{
or_continue!(path.strip_prefix(&parent)).to_path_buf()
or_continue!(path.strip_prefix(parent)).to_path_buf()
}
}
None => path.clone(),
@@ -1351,7 +1351,7 @@ fn context_for(src: &Path, dest: &Path) -> String {
/// Implements a simple backup copy for the destination file.
/// TODO: for the backup, should this function be replaced by `copy_file(...)`?
fn backup_dest(dest: &Path, backup_path: &Path) -> CopyResult<PathBuf> {
fs::copy(dest, &backup_path)?;
fs::copy(dest, backup_path)?;
Ok(backup_path.into())
}
@@ -1566,7 +1566,7 @@ fn copy_file(
.write(true)
.truncate(false)
.create(true)
.open(&dest)
.open(dest)
.unwrap();
}
};
@@ -1630,7 +1630,7 @@ fn copy_helper(
fn copy_fifo(dest: &Path, overwrite: OverwriteMode) -> CopyResult<()> {
if dest.exists() {
overwrite.verify(dest)?;
fs::remove_file(&dest)?;
fs::remove_file(dest)?;
}
let name = CString::new(dest.as_os_str().as_bytes()).unwrap();
@@ -1647,7 +1647,7 @@ fn copy_link(
symlinked_files: &mut HashSet<FileInformation>,
) -> CopyResult<()> {
// Here, we will copy the symlink itself (actually, just recreate it)
let link = fs::read_link(&source)?;
let link = fs::read_link(source)?;
let dest: Cow<'_, Path> = if dest.is_dir() {
match source.file_name() {
Some(name) => dest.join(name).into(),
@@ -1695,8 +1695,8 @@ pub fn verify_target_type(target: &Path, target_type: &TargetType) -> CopyResult
/// ).unwrap() == Path::new("target/c.txt"))
/// ```
pub fn localize_to_target(root: &Path, source: &Path, target: &Path) -> CopyResult<PathBuf> {
let local_to_root = source.strip_prefix(&root)?;
Ok(target.join(&local_to_root))
let local_to_root = source.strip_prefix(root)?;
Ok(target.join(local_to_root))
}
pub fn path_has_prefix(p1: &Path, p2: &Path) -> io::Result<bool> {
+1 -1
View File
@@ -368,7 +368,7 @@ fn cut_files(mut filenames: Vec<String>, mode: &Mode) -> UResult<()> {
continue;
}
show_if_err!(File::open(&path)
show_if_err!(File::open(path)
.map_err_context(|| filename.maybe_quote().to_string())
.and_then(|file| {
match &mode {
+2 -2
View File
@@ -492,7 +492,7 @@ fn build_exclude_patterns(matches: &ArgMatches) -> UResult<Vec<Pattern>> {
let exclude_from_iterator = matches
.get_many::<String>(options::EXCLUDE_FROM)
.unwrap_or_default()
.flat_map(|f| file_as_vec(&f));
.flat_map(file_as_vec);
let excludes_iterator = matches
.get_many::<String>(options::EXCLUDE)
@@ -913,7 +913,7 @@ impl FromStr for Threshold {
type Err = ParseSizeError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let offset = if s.starts_with(&['-', '+'][..]) { 1 } else { 0 };
let offset = usize::from(s.starts_with(&['-', '+'][..]));
let size = parse_size(&s[offset..])?;
+1 -5
View File
@@ -505,11 +505,7 @@ fn prefix_operator_substr(values: &[String]) -> String {
}
fn bool_as_int(b: bool) -> u8 {
if b {
1
} else {
0
}
u8::from(b)
}
fn bool_as_string(b: bool) -> String {
if b {
+4 -4
View File
@@ -275,7 +275,7 @@ impl Params {
// For example, if `tmpdir` is "a/b" and the template is "c/dXXX",
// then `prefix` is "a/b/c/d".
let tmpdir = options.tmpdir;
let prefix_from_option = tmpdir.clone().unwrap_or_else(|| "".to_string());
let prefix_from_option = tmpdir.clone().unwrap_or_default();
let prefix_from_template = &options.template[..i];
let prefix = Path::new(&prefix_from_option)
.join(prefix_from_template)
@@ -311,7 +311,7 @@ impl Params {
//
// For example, if the suffix command-line argument is ".txt" and
// the template is "XXXabc", then `suffix` is "abc.txt".
let suffix_from_option = options.suffix.unwrap_or_else(|| "".to_string());
let suffix_from_option = options.suffix.unwrap_or_default();
let suffix_from_template = &options.template[j..];
let suffix = format!("{}{}", suffix_from_template, suffix_from_option);
if suffix.contains(MAIN_SEPARATOR) {
@@ -484,7 +484,7 @@ pub fn dry_exec(tmpdir: &str, prefix: &str, rand: usize, suffix: &str) -> UResul
fn make_temp_dir(dir: &str, prefix: &str, rand: usize, suffix: &str) -> UResult<PathBuf> {
let mut builder = Builder::new();
builder.prefix(prefix).rand_bytes(rand).suffix(suffix);
match builder.tempdir_in(&dir) {
match builder.tempdir_in(dir) {
Ok(d) => {
// `into_path` consumes the TempDir without removing it
let path = d.into_path();
@@ -516,7 +516,7 @@ fn make_temp_dir(dir: &str, prefix: &str, rand: usize, suffix: &str) -> UResult<
fn make_temp_file(dir: &str, prefix: &str, rand: usize, suffix: &str) -> UResult<PathBuf> {
let mut builder = Builder::new();
builder.prefix(prefix).rand_bytes(rand).suffix(suffix);
match builder.tempfile_in(&dir) {
match builder.tempfile_in(dir) {
// `keep` ensures that the file is not deleted
Ok(named_tempfile) => match named_tempfile.keep() {
Ok((_, pathbuf)) => Ok(pathbuf),
+1 -1
View File
@@ -453,7 +453,7 @@ fn rename_symlink_fallback(from: &Path, to: &Path) -> io::Result<()> {
let path_symlink_points_to = fs::read_link(from)?;
#[cfg(unix)]
{
unix::fs::symlink(&path_symlink_points_to, &to).and_then(|_| fs::remove_file(&from))?;
unix::fs::symlink(&path_symlink_points_to, to).and_then(|_| fs::remove_file(from))?;
}
#[cfg(windows)]
{
+1 -1
View File
@@ -279,7 +279,7 @@ fn nl<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UResult<()> {
// If we have already seen three groups (corresponding to
// a header) or the current char does not form part of
// a new group, then this line is not a segment indicator.
if matched_groups >= 3 || settings.section_delimiter[if odd { 1 } else { 0 }] != c {
if matched_groups >= 3 || settings.section_delimiter[usize::from(odd)] != c {
matched_groups = 0;
break;
}
+1 -1
View File
@@ -1086,7 +1086,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let mut files = Vec::new();
for path in &files0_from {
let reader = open(&path)?;
let reader = open(path)?;
let buf_reader = BufReader::new(reader);
for line in buf_reader.split(b'\0').flatten() {
files.push(OsString::from(
+1 -1
View File
@@ -80,7 +80,7 @@ impl TmpDirWrapper {
/// Remove the directory at `path` by deleting its child files and then itself.
/// Errors while deleting child files are ignored.
fn remove_tmp_dir(path: &Path) -> std::io::Result<()> {
if let Ok(read_dir) = std::fs::read_dir(&path) {
if let Ok(read_dir) = std::fs::read_dir(path) {
for file in read_dir.flatten() {
// if we fail to delete the file here it was probably deleted by another thread
// in the meantime, but that's ok.
+1 -1
View File
@@ -56,7 +56,7 @@ impl Drop for WithEnvVarSet {
/// Restore previous value now that this is being dropped by context
fn drop(&mut self) {
if let Ok(ref prev_value) = self._previous_var_value {
env::set_var(&self._previous_var_key, &prev_value);
env::set_var(&self._previous_var_key, prev_value);
} else {
env::remove_var(&self._previous_var_key);
}
+3 -3
View File
@@ -207,7 +207,7 @@ pub fn uu_app<'a>() -> Command<'a> {
.short(options::INPUT_SHORT)
.help("adjust standard input stream buffering")
.value_name("MODE")
.required_unless_present_any(&[options::OUTPUT, options::ERROR]),
.required_unless_present_any([options::OUTPUT, options::ERROR]),
)
.arg(
Arg::new(options::OUTPUT)
@@ -215,7 +215,7 @@ pub fn uu_app<'a>() -> Command<'a> {
.short(options::OUTPUT_SHORT)
.help("adjust standard output stream buffering")
.value_name("MODE")
.required_unless_present_any(&[options::INPUT, options::ERROR]),
.required_unless_present_any([options::INPUT, options::ERROR]),
)
.arg(
Arg::new(options::ERROR)
@@ -223,7 +223,7 @@ pub fn uu_app<'a>() -> Command<'a> {
.short(options::ERROR_SHORT)
.help("adjust standard error stream buffering")
.value_name("MODE")
.required_unless_present_any(&[options::INPUT, options::OUTPUT]),
.required_unless_present_any([options::INPUT, options::OUTPUT]),
)
.arg(
Arg::new(options::COMMAND)
+1 -1
View File
@@ -122,7 +122,7 @@ impl FileHandling {
*/
self.get_mut(path)
.reader
.replace(Box::new(BufReader::new(File::open(&path)?)));
.replace(Box::new(BufReader::new(File::open(path)?)));
Ok(())
}
+1 -1
View File
@@ -155,7 +155,7 @@ fn tail_file(
watcher_service.add_bad_path(path, input.display_name.as_str(), false)?;
} else if input.is_tailable() {
let metadata = path.metadata().ok();
match File::open(&path) {
match File::open(path) {
Ok(mut file) => {
input_service.print_header(input);
let mut reader;
+1 -1
View File
@@ -220,7 +220,7 @@ fn open(path: &str) -> BufReader<Box<dyn Read + 'static>> {
if path == "-" {
BufReader::new(Box::new(stdin()) as Box<dyn Read>)
} else {
file_buf = match File::open(&path) {
file_buf = match File::open(path) {
Ok(a) => a,
Err(e) => crash!(1, "{}: {}", path.maybe_quote(), e),
};
+2 -2
View File
@@ -424,7 +424,7 @@ fn open_input_file(in_file_name: &str) -> UResult<BufReader<Box<dyn Read + 'stat
Box::new(stdin()) as Box<dyn Read>
} else {
let path = Path::new(in_file_name);
let in_file = File::open(&path)
let in_file = File::open(path)
.map_err_context(|| format!("Could not open {}", in_file_name.maybe_quote()))?;
Box::new(in_file) as Box<dyn Read>
};
@@ -436,7 +436,7 @@ fn open_output_file(out_file_name: &str) -> UResult<BufWriter<Box<dyn Write + 's
Box::new(stdout()) as Box<dyn Write>
} else {
let path = Path::new(out_file_name);
let out_file = File::create(&path)
let out_file = File::create(path)
.map_err_context(|| format!("Could not create {}", out_file_name.maybe_quote()))?;
Box::new(out_file) as Box<dyn Write>
};
+1 -1
View File
@@ -355,7 +355,7 @@ pub fn canonicalize<P: AsRef<Path>>(
followed_symlinks += 1;
} else {
let file_info =
FileInformation::from_path(&result.parent().unwrap(), false).unwrap();
FileInformation::from_path(result.parent().unwrap(), false).unwrap();
let mut path_to_follow = PathBuf::new();
for part in &parts {
path_to_follow.push(part.as_os_str());
+4 -3
View File
@@ -76,9 +76,10 @@ fn test_wrap() {
#[test]
fn test_wrap_no_arg() {
for wrap_param in ["-w", "--wrap"] {
new_ucmd!().arg(wrap_param).fails().stderr_contains(
&"The argument '--wrap <wrap>' requires a value but none was supplied",
);
new_ucmd!()
.arg(wrap_param)
.fails()
.stderr_contains("The argument '--wrap <wrap>' requires a value but none was supplied");
}
}

Some files were not shown because too many files have changed in this diff Show More