rust-lang / rust-lang/rust-clippy

Suggest Iterator::min_by_key (and similar methods)

Open
#10,305 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.