rust-lang / rust-lang/rust-clippy
Suggest Iterator::by_ref() when take_while and skip_while used on the same iterator
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
I just wrote the following code to split the following list.
let iter = vec![1, 2, 3, 4, 5].into_iter();
let first_part = iter.clone().take_while(|&x| x != 3).collect::<Vec<_>>();
let second_part = iter.skip_while(|&x| x != 3).skip(1).collect::<Vec<_>>();
But this can be done more efficiently by doing:
let mut iter = vec![1, 2, 3, 4, 5].into_iter();
let first_part = iter.by_ref().take_while(|&x| x != 3).collect::<Vec<_>>();
let second_part = iter.collect::<Vec<_>>();
Both produce:
first_part = [1, 2]
second_part = [4, 5]
I have no idea how hard it would be to detect this pattern, but it would be helpful for people who don't know Iterator::by_ref().
Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1b96ba06ac1d3e7105acfe9db9b78907
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the iterator examples from the issue in the linked Rust Playground. Determine the intended behavior for detecting use of take_while and skip_while on the same iterator, and define what diagnostic suggesting Iterator::by_ref() should look like. Done means the behavior and scope are specified well enough to implement and test as a Clippy lint.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100