Fix clippy::implicit_clone

This commit is contained in:
Kostiantyn Hryshchuk 2023-11-04 20:00:53 +01:00
parent 44d105d015
commit 6ac1af6953
25 changed files with 46 additions and 67 deletions

View file

@ -117,7 +117,7 @@ jobs:
run: |
## `cargo clippy` lint testing
unset fault
CLIPPY_FLAGS="-W clippy::default_trait_access -W clippy::manual_string_new -W clippy::cognitive_complexity"
CLIPPY_FLAGS="-W clippy::default_trait_access -W clippy::manual_string_new -W clippy::cognitive_complexity -W clippy::implicit_clone"
fault_type="${{ steps.vars.outputs.FAULT_TYPE }}"
fault_prefix=$(echo "$fault_type" | tr '[:lower:]' '[:upper:]')
# * convert any warnings to GHA UI annotations; ref: <https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-a-warning-message>

View file

@ -54,7 +54,7 @@ impl Config {
format!("{}: No such file or directory", name.maybe_quote()),
));
}
Some(name.to_owned())
Some(name.clone())
}
}
None => None,

View file

@ -213,7 +213,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let squeeze_blank = matches.get_flag(options::SQUEEZE_BLANK);
let files: Vec<String> = match matches.get_many::<String>(options::FILE) {
Some(v) => v.clone().map(|v| v.to_owned()).collect(),
Some(v) => v.cloned().collect(),
None => vec!["-".to_owned()],
};

View file

@ -62,15 +62,9 @@ impl CsplitOptions {
split_name: crash_if_err!(
1,
SplitName::new(
matches
.get_one::<String>(options::PREFIX)
.map(|s| s.to_owned()),
matches
.get_one::<String>(options::SUFFIX_FORMAT)
.map(|s| s.to_owned()),
matches
.get_one::<String>(options::DIGITS)
.map(|s| s.to_owned())
matches.get_one::<String>(options::PREFIX).cloned(),
matches.get_one::<String>(options::SUFFIX_FORMAT).cloned(),
matches.get_one::<String>(options::DIGITS).cloned()
)
),
keep_files,

View file

@ -400,7 +400,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if s.is_empty() {
Some("\0".to_owned())
} else {
Some(s.to_owned())
Some(s.clone())
}
}
None => None,
@ -491,7 +491,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let files: Vec<String> = matches
.get_many::<String>(options::FILE)
.unwrap_or_default()
.map(|s| s.to_owned())
.cloned()
.collect();
match mode_parse {

View file

@ -30,7 +30,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let dirnames: Vec<String> = matches
.get_many::<String>(options::DIR)
.unwrap_or_default()
.map(|s| s.to_owned())
.cloned()
.collect();
if dirnames.is_empty() {

View file

@ -506,7 +506,7 @@ fn build_exclude_patterns(matches: &ArgMatches) -> UResult<Vec<Pattern>> {
let excludes_iterator = matches
.get_many::<String>(options::EXCLUDE)
.unwrap_or_default()
.map(|v| v.to_owned());
.cloned();
let mut exclude_patterns = Vec::new();
for f in excludes_iterator.chain(exclude_from_iterator) {

View file

@ -35,7 +35,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let bytes = matches.get_flag(options::BYTES);
let spaces = matches.get_flag(options::SPACES);
let poss_width = match matches.get_one::<String>(options::WIDTH) {
Some(v) => Some(v.to_owned()),
Some(v) => Some(v.clone()),
None => obs_width,
};
@ -50,7 +50,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
};
let files = match matches.get_many::<String>(options::FILE) {
Some(v) => v.map(|v| v.to_owned()).collect(),
Some(v) => v.cloned().collect(),
None => vec!["-".to_owned()],
};

View file

@ -205,7 +205,7 @@ impl HeadOptions {
options.mode = Mode::from(matches)?;
options.files = match matches.get_many::<String>(options::FILES_NAME) {
Some(v) => v.map(|s| s.to_owned()).collect(),
Some(v) => v.cloned().collect(),
None => vec!["-".to_owned()],
};
//println!("{:#?}", options);

View file

@ -375,9 +375,7 @@ fn behavior(matches: &ArgMatches) -> UResult<Behavior> {
};
let backup_mode = backup_control::determine_backup_mode(matches)?;
let target_dir = matches
.get_one::<String>(OPT_TARGET_DIRECTORY)
.map(|d| d.to_owned());
let target_dir = matches.get_one::<String>(OPT_TARGET_DIRECTORY).cloned();
let preserve_timestamps = matches.get_flag(OPT_PRESERVE_TIMESTAMPS);
let compare = matches.get_flag(OPT_COMPARE);
@ -593,7 +591,7 @@ fn standard(mut paths: Vec<String>, b: &Behavior) -> UResult<()> {
let source = sources.first().unwrap();
if source.is_dir() {
return Err(InstallError::OmittingDirectory(source.to_path_buf()).into());
return Err(InstallError::OmittingDirectory(source.clone()).into());
}
if target.is_file() || is_new_file_path(&target) {
@ -628,7 +626,7 @@ fn copy_files_into_dir(files: &[PathBuf], target_dir: &Path, b: &Behavior) -> UR
}
if sourcepath.is_dir() {
let err = InstallError::OmittingDirectory(sourcepath.to_path_buf());
let err = InstallError::OmittingDirectory(sourcepath.clone());
show!(err);
continue;
}
@ -701,12 +699,9 @@ fn perform_backup(to: &Path, b: &Behavior) -> UResult<Option<PathBuf>> {
if let Some(ref backup_path) = backup_path {
// TODO!!
if let Err(err) = fs::rename(to, backup_path) {
return Err(InstallError::BackupFailed(
to.to_path_buf(),
backup_path.to_path_buf(),
err,
)
.into());
return Err(
InstallError::BackupFailed(to.to_path_buf(), backup_path.clone(), err).into(),
);
}
}
Ok(backup_path)

View file

@ -577,9 +577,7 @@ fn extract_color(options: &clap::ArgMatches) -> bool {
///
/// A QuotingStyle variant representing the quoting style to use.
fn extract_quoting_style(options: &clap::ArgMatches, show_control: bool) -> QuotingStyle {
let opt_quoting_style = options
.get_one::<String>(options::QUOTING_STYLE)
.map(|cmd_line_qs| cmd_line_qs.to_owned());
let opt_quoting_style = options.get_one::<String>(options::QUOTING_STYLE).cloned();
if let Some(style) = opt_quoting_style {
match style.as_str() {
@ -788,9 +786,7 @@ impl Config {
match parse_size_u64(&raw_bs.to_string_lossy()) {
Ok(size) => Some(size),
Err(_) => {
show!(LsError::BlockSizeParseError(
cmd_line_bs.unwrap().to_owned()
));
show!(LsError::BlockSizeParseError(cmd_line_bs.unwrap().clone()));
None
}
}
@ -3056,7 +3052,7 @@ fn display_file_name(
target_data.must_dereference,
) {
Ok(md) => md,
Err(_) => path.md(out).unwrap().to_owned(),
Err(_) => path.md(out).unwrap().clone(),
};
name.push_str(&color_name(
@ -3073,11 +3069,7 @@ fn display_file_name(
}
}
Err(err) => {
show!(LsError::IOErrorContext(
err,
path.p_buf.to_path_buf(),
false
));
show!(LsError::IOErrorContext(err, path.p_buf.clone(), false));
}
}
}
@ -3087,7 +3079,7 @@ fn display_file_name(
if config.context {
if let Some(pad_count) = prefix_context {
let security_context = if matches!(config.format, Format::Commas) {
path.security_context.to_owned()
path.security_context.clone()
} else {
pad_left(&path.security_context, pad_count)
};

View file

@ -42,7 +42,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
};
let fifos: Vec<String> = match matches.get_many::<String>(options::FIFO) {
Some(v) => v.clone().map(|s| s.to_owned()).collect(),
Some(v) => v.cloned().collect(),
None => return Err(USimpleError::new(1, "missing operand")),
};

View file

@ -146,7 +146,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let files: Vec<OsString> = matches
.get_many::<OsString>(ARG_FILES)
.unwrap_or_default()
.map(|v| v.to_os_string())
.cloned()
.collect();
let overwrite_mode = determine_overwrite_mode(&matches);

View file

@ -19,11 +19,11 @@ pub fn parse_options(settings: &mut crate::Settings, opts: &clap::ArgMatches) ->
settings.section_delimiter = if delimiter.len() == 1 {
format!("{delimiter}:")
} else {
delimiter.to_owned()
delimiter.clone()
};
}
if let Some(val) = opts.get_one::<String>(options::NUMBER_SEPARATOR) {
settings.number_separator = val.to_owned();
settings.number_separator = val.clone();
}
settings.number_format = opts
.get_one::<String>(options::NUMBER_FORMAT)

View file

@ -195,7 +195,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
let files: Vec<String> = match matches.get_many::<String>(options::FILE) {
Some(v) => v.clone().map(|v| v.to_owned()).collect(),
Some(v) => v.cloned().collect(),
None => vec!["-".to_owned()],
};

View file

@ -211,9 +211,7 @@ fn parse_options(args: &ArgMatches) -> Result<NumfmtOptions> {
_ => unreachable!("Should be restricted by clap"),
};
let suffix = args
.get_one::<String>(options::SUFFIX)
.map(|s| s.to_owned());
let suffix = args.get_one::<String>(options::SUFFIX).cloned();
let invalid =
InvalidModes::from_str(args.get_one::<String>(options::INVALID).unwrap()).unwrap();

View file

@ -44,7 +44,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let files = matches
.get_many::<String>(options::FILE)
.unwrap()
.map(|s| s.to_owned())
.cloned()
.collect();
let line_ending = LineEnding::from_zero_flag(matches.get_flag(options::ZERO_TERMINATED));

View file

@ -720,7 +720,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let mut input_files: Vec<String> = match &matches.get_many::<String>(options::FILE) {
Some(v) => v.clone().map(|v| v.to_owned()).collect(),
Some(v) => v.clone().cloned().collect(),
None => vec!["-".to_string()],
};

View file

@ -75,7 +75,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let headcounts = matches
.get_many::<String>(options::HEAD_COUNT)
.unwrap_or_default()
.map(|s| s.to_owned())
.cloned()
.collect();
match parse_head_count(headcounts) {
Ok(val) => val,

View file

@ -394,8 +394,8 @@ mod tests {
let (a_info, a_range) = NumInfo::parse(a, &NumInfoParseSettings::default());
let (b_info, b_range) = NumInfo::parse(b, &NumInfoParseSettings::default());
let ordering = numeric_str_cmp(
(&a[a_range.to_owned()], &a_info),
(&b[b_range.to_owned()], &b_info),
(&a[a_range.clone()], &a_info),
(&b[b_range.clone()], &b_info),
);
assert_eq!(ordering, expected);
let ordering = numeric_str_cmp((&b[b_range], &b_info), (&a[a_range], &a_info));

View file

@ -506,10 +506,10 @@ impl Settings {
};
let result = Self {
prefix: matches.get_one::<String>(ARG_PREFIX).unwrap().to_owned(),
prefix: matches.get_one::<String>(ARG_PREFIX).unwrap().clone(),
suffix,
input: matches.get_one::<String>(ARG_INPUT).unwrap().to_owned(),
filter: matches.get_one::<String>(OPT_FILTER).map(|s| s.to_owned()),
input: matches.get_one::<String>(ARG_INPUT).unwrap().clone(),
filter: matches.get_one::<String>(OPT_FILTER).cloned(),
strategy,
verbose: matches.value_source(OPT_VERBOSE) == Some(ValueSource::CommandLine),
separator,

View file

@ -107,7 +107,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let files: Vec<String> = match matches.get_many::<String>(options::FILE) {
Some(v) => v.clone().map(|v| v.to_owned()).collect(),
Some(v) => v.cloned().collect(),
None => vec!["-".to_owned()],
};

View file

@ -40,7 +40,7 @@ impl FileHandling {
pub fn insert(&mut self, k: &Path, v: PathData, update_last: bool) {
let k = Self::canonicalize_path(k);
if update_last {
self.last = Some(k.to_owned());
self.last = Some(k.clone());
}
let _ = self.map.insert(k, v);
}

View file

@ -279,7 +279,7 @@ impl Observer {
if !path.is_file() {
continue;
}
let mut path = path.to_owned();
let mut path = path.clone();
if path.is_relative() {
path = std::env::current_dir()?.join(path);
}
@ -345,7 +345,7 @@ impl Observer {
show_error!("{}: file truncated", display_name);
self.files.update_reader(event_path)?;
}
paths.push(event_path.to_owned());
paths.push(event_path.clone());
} else if !is_tailable && old_md.is_tailable() {
if pd.reader.is_some() {
self.files.reset_reader(event_path);
@ -359,7 +359,7 @@ impl Observer {
} else if is_tailable {
show_error!( "{} has appeared; following new file", display_name.quote());
self.files.update_reader(event_path)?;
paths.push(event_path.to_owned());
paths.push(event_path.clone());
} else if settings.retry {
if self.follow_descriptor() {
show_error!(
@ -403,7 +403,7 @@ impl Observer {
"{} cannot be used, reverting to polling",
text::BACKEND
);
self.orphans.push(event_path.to_owned());
self.orphans.push(event_path.clone());
let _ = self.watcher_rx.as_mut().unwrap().unwatch(event_path);
}
} else {
@ -451,7 +451,7 @@ impl Observer {
if self.follow_descriptor() {
let new_path = event.paths.last().unwrap();
paths.push(new_path.to_owned());
paths.push(new_path.clone());
let new_data = PathData::from_other_with_path(self.files.remove(event_path), new_path);
self.files.insert(

View file

@ -164,7 +164,7 @@ impl Parser {
/// The stream is unchanged and will return the same Symbol on subsequent
/// calls to `next()` or `peek()`.
fn peek(&mut self) -> Symbol {
Symbol::new(self.tokens.peek().map(|s| s.to_os_string()))
Symbol::new(self.tokens.peek().cloned())
}
/// Test if the next token in the stream is a BOOLOP (-a or -o), without