rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`iter.map(...).any(identity)` into `iter.any(...)`

Open A4-Tacks opened this issue 1 year ago • 1 comments

What it does

Make the code more concise

Advantage

  • Less code
  • simple

Drawbacks

No response

Example

pub fn foo<'a, T: AsRef<str> + 'a>(iter: impl Iterator<Item = &'a T>) -> bool {
    iter.map(AsRef::as_ref)
        .map(|s| s.is_empty())
        .any(|i| i)
}
pub fn bar<'a, T: AsRef<str> + 'a>(iter: impl Iterator<Item = &'a T>) -> bool {
    iter.map(AsRef::as_ref)
        .map(|s| s.is_empty())
        .any(core::convert::identity)
}
pub fn baz<'a, T: AsRef<str> + 'a>(iter: impl Iterator<Item = &'a T>) -> bool {
    iter.map(AsRef::as_ref)
        .map(|s| s.is_empty())
        .all(core::convert::identity)
}

Could be written as:

pub fn foo<'a, T: AsRef<str> + 'a>(iter: impl Iterator<Item = &'a T>) -> bool {
    iter.map(AsRef::as_ref)
        .any(|s| s.is_empty())
}
pub fn bar<'a, T: AsRef<str> + 'a>(iter: impl Iterator<Item = &'a T>) -> bool {
    iter.map(AsRef::as_ref)
        .any(|s| s.is_empty())
}
pub fn baz<'a, T: AsRef<str> + 'a>(iter: impl Iterator<Item = &'a T>) -> bool {
    iter.map(AsRef::as_ref)
        .all(|s| s.is_empty())
}

Iterator::any and Iterator::all

A4-Tacks avatar Aug 14 '24 07:08 A4-Tacks

just like #11567

A4-Tacks avatar Aug 14 '24 08:08 A4-Tacks