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

Iterators: suggest `position()` instead of `enumerate() + find() + .0`

Open devonhollowood opened this issue 7 years ago • 5 comments

This is sort of the opposite of #456. I recently wrote let idx = v.iter().enumerate().find(|&(_, &ch)| ch == '|').unwrap().0, when I should have written let idx = v.iter().position(|&ch| ch == '|').unwrap(). I'd have to think a little on the exact rule that should be followed here, but it would be nice if clippy could catch this pattern.

devonhollowood avatar Dec 20 '17 07:12 devonhollowood

This should be a simple addition to the iterator method chain lint

oli-obk avatar Dec 20 '17 08:12 oli-obk

I'm taking a look at this - looks like an addition to methods.rs.

DarrenTsung avatar Dec 23 '17 18:12 DarrenTsung

It seems that this issue has gone stale. I'd like to implement this if there is still interest. One question though: What about cases like v.iter().enumerate().find(|&(_, &ch)| ch == '|').map(|x| x.0) which is the same as v.iter().position(|&(_, &ch)| ch == '|')?

lukasstevens avatar Oct 10 '18 13:10 lukasstevens

I'm filling to attempt to implement this and submit a PR if it is still relevant.

brightly-salty avatar Dec 31 '20 01:12 brightly-salty

@rustbot claim

GabrielBFern avatar Feb 12 '24 03:02 GabrielBFern