2038: Improve iterator use r=CreepySkeleton a=nnethercote



Co-authored-by: Nicholas Nethercote <nnethercote@mozilla.com>
This commit is contained in:
bors[bot] 2020-07-29 02:14:00 +00:00 committed by GitHub
commit a4bc1f2abc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 25 additions and 64 deletions

View file

@ -140,19 +140,12 @@ pub trait Generator {
.flatten()
.collect();
if shorts_and_visible_aliases
.iter()
.find(|x| **x == 'h')
.is_none()
{
if !shorts_and_visible_aliases.iter().any(|&x| x == 'h') {
shorts_and_visible_aliases.push('h');
}
if !p.is_set(AppSettings::DisableVersion)
&& shorts_and_visible_aliases
.iter()
.find(|x| **x == 'V')
.is_none()
&& !shorts_and_visible_aliases.iter().any(|&x| x == 'V')
{
shorts_and_visible_aliases.push('V');
}
@ -176,13 +169,11 @@ pub trait Generator {
})
.collect();
if longs.iter().find(|x| **x == "help").is_none() {
if !longs.iter().any(|x| *x == "help") {
longs.push(String::from("help"));
}
if !p.is_set(AppSettings::DisableVersion)
&& longs.iter().find(|x| **x == "version").is_none()
{
if !p.is_set(AppSettings::DisableVersion) && !longs.iter().any(|x| *x == "version") {
longs.push(String::from("version"));
}
@ -196,7 +187,7 @@ pub trait Generator {
let mut flags: Vec<_> = p.get_flags_no_heading().cloned().collect();
if flags.iter().find(|x| x.get_name() == "help").is_none() {
if !flags.iter().any(|x| x.get_name() == "help") {
flags.push(
Arg::new("help")
.short('h')
@ -206,7 +197,7 @@ pub trait Generator {
}
if !p.is_set(AppSettings::DisableVersion)
&& flags.iter().find(|x| x.get_name() == "version").is_none()
&& !flags.iter().any(|x| x.get_name() == "version")
{
flags.push(
Arg::new("version")

View file

@ -2549,8 +2549,7 @@ impl<'help> App<'help> {
pub(crate) fn has_visible_subcommands(&self) -> bool {
self.subcommands
.iter()
.filter(|sc| sc.name != "help")
.any(|sc| !sc.is_set(AppSettings::Hidden))
.any(|sc| sc.name != "help" && !sc.is_set(AppSettings::Hidden))
}
/// Check if this subcommand can be referred to as `name`. In other words,

View file

@ -391,28 +391,17 @@ impl<'help, 'app, 'parser> Usage<'help, 'app, 'parser> {
.flat_map(|g| self.p.app.unroll_args_in_group(&g.id))
.collect::<Vec<_>>();
let pmap = if let Some(m) = matcher {
unrolled_reqs
.iter()
.chain(incls.iter())
.filter(|a| self.p.app.get_positionals().any(|p| &&p.id == a))
.filter(|&pos| !m.contains(pos))
.filter_map(|pos| self.p.app.find(pos))
.filter(|&pos| incl_last || !pos.is_set(ArgSettings::Last))
.filter(|pos| !args_in_groups.contains(&pos.id))
.map(|pos| (pos.index.unwrap(), pos))
.collect::<BTreeMap<u64, &Arg>>() // sort by index
} else {
unrolled_reqs
.iter()
.chain(incls.iter())
.filter(|a| self.p.app.get_positionals().any(|p| &&p.id == a))
.filter_map(|pos| self.p.app.find(pos))
.filter(|&pos| incl_last || !pos.is_set(ArgSettings::Last))
.filter(|pos| !args_in_groups.contains(&pos.id))
.map(|pos| (pos.index.unwrap(), pos))
.collect::<BTreeMap<u64, &Arg>>() // sort by index
};
let pmap = unrolled_reqs
.iter()
.chain(incls.iter())
.filter(|a| self.p.app.get_positionals().any(|p| &&p.id == a))
.filter(|&pos| matcher.map_or(true, |m| !m.contains(pos)))
.filter_map(|pos| self.p.app.find(pos))
.filter(|&pos| incl_last || !pos.is_set(ArgSettings::Last))
.filter(|pos| !args_in_groups.contains(&pos.id))
.map(|pos| (pos.index.unwrap(), pos))
.collect::<BTreeMap<u64, &Arg>>(); // sort by index
for p in pmap.values() {
debug!("Usage::get_required_usage_from:iter:{:?}", p.id);
if args_in_groups.is_empty() || !args_in_groups.contains(&p.id) {

View file

@ -35,7 +35,6 @@ impl MatchedArg {
pub(crate) fn contains_val(&self, val: &str) -> bool {
self.vals
.iter()
.map(OsString::as_os_str)
.any(|v| v == OsStr::new(val))
.any(|v| OsString::as_os_str(v) == OsStr::new(val))
}
}

View file

@ -626,8 +626,7 @@ impl<'help, 'app> Parser<'help, 'app> {
.args
.args
.iter()
.filter(|a| a.is_positional())
.find(|p| p.index == Some(pos_counter as u64))
.find(|a| a.is_positional() && a.index == Some(pos_counter as u64))
{
if p.is_set(ArgSettings::Last) && !self.is_set(AS::TrailingValues) {
return Err(ClapError::unknown_argument(

View file

@ -213,8 +213,7 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
.app
.groups
.iter()
.filter(|g| !g.multiple)
.find(|g| &g.id == name)
.find(|g| !g.multiple && &g.id == name)
{
let conf_with_self = self
.p
@ -311,8 +310,7 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
.app
.groups
.iter()
.filter(|g| !g.multiple)
.find(|g| g.id == grp)
.find(|g| !g.multiple && g.id == grp)
{
// for g_arg in self.p.app.unroll_args_in_group(&g.name) {
// if &g_arg != name {
@ -326,8 +324,7 @@ impl<'help, 'app, 'parser> Validator<'help, 'app, 'parser> {
.app
.groups
.iter()
.filter(|g| !g.multiple)
.find(|grp| grp.id == *name)
.find(|g| !g.multiple && g.id == *name)
{
debug!("Validator::gather_conflicts:iter:{:?}:group", name);
self.c.insert(g.id.clone());

View file

@ -76,15 +76,7 @@ impl<'a> ArgStr<'a> {
pub(crate) fn trim_start_n_matches(&self, n: usize, ch: u8) -> ArgStr {
assert!(ch.is_ascii());
let i = self
.0
.iter()
.take(n)
.take_while(|c| **c == ch)
.enumerate()
.last()
.map(|(i, _)| i + 1)
.unwrap_or(0);
let i = self.0.iter().take(n).take_while(|c| **c == ch).count();
self.split_at_unchecked(i).1
}

View file

@ -30,12 +30,7 @@ where
self.0.push(Child::new(req));
idx
} else {
self.0
.iter()
.enumerate()
.find(|(_, e)| e.id == req)
.map(|(i, _)| i)
.unwrap()
self.0.iter().position(|e| e.id == req).unwrap()
}
}