rust-clippy
rust-clippy copied to clipboard
`iter.map(...).any(identity)` into `iter.any(...)`
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
just like #11567