rust-lang / rust-lang/rust-clippy
Suggest Iterator::min_by_key (and similar methods)
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Similarly to unnecessary_sort_by, detect instances of Iterator::min_by() (and Iterator::max_by()) that could be better written with min_by_key() or min().
Lint Name
unnecessary_iter_by
Category
complexity
Advantage
It is clearer to use Iterator::min_by_key (or Iterator::min if possible) than to use Iterator::min_by and a more complicated closure.
Drawbacks
Same as https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_sort_by?
Example
struct Point { x: i32, y: i32 }
let array = [Point { x: 1, y: 0 }, Point { x: 0, y: 1 }];
array.iter().min_by(|a, b| a.x.cmp(&b.x));
Could be written as:
array.iter().min_by_key(|p| &p.x);
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 comparing the proposed unnecessary_iter_by lint with unnecessary_sort_by, then inspect how Iterator::min_by(), max_by(), min_by_key(), and min() are represented in the existing linting logic. Done means detecting the eligible closure forms and providing suggestions for min_by_key() or min(), with coverage for both min_by() and max_by().
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100