sort + indexing => select_nth_unstable

Open
#14,119 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
devtools

Research direction

Start by checking how rust-clippy currently recognizes a sort or sort_unstable followed by indexing. Compare the median example with select_nth_unstable and define conservative boundaries that avoid false positives. Done means the intended pattern receives a clear suggestion without triggering on uncertain cases.

Written by the indexing model from the issue text.

Description

A-lint
What it does

This pedantic lint is supposed to suggest replacing a sort or sort_unstable followed by an indexing, with the usage of a more efficient select_nth_unstable. I am not sure if this lint is too much specialized, if you think it's it, then close down this issue.

Advantage

Faster, slightly more clear purpose of the code.

Drawbacks

It's hard to spot the cases where this lint should fire. So better to keep it conservative and avoid most false positives.

Example
#[must_use]
pub fn compute_median(data: &[u32]) -> u32 {
    let mut data_sorted = data.to_owned();
    data_sorted.sort_unstable();
    data_sorted[data_sorted.len() / 2]
}

The link should suggest to use instead:

select_nth_unstable()
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

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.

More from rust-lang/rust-clippy

All issues in rust-lang/rust-clippy

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.