Skip to content

New / Updated Lint: Prefer sort_unstable over sort #9970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
CryZe opened this issue Nov 28, 2022 · 3 comments
Open

New / Updated Lint: Prefer sort_unstable over sort #9970

CryZe opened this issue Nov 28, 2022 · 3 comments
Assignees
Labels
A-lint Area: New lints

Comments

@CryZe
Copy link

CryZe commented Nov 28, 2022

What it does

While there already is a similar lint stable_sort_primitive, this should be extended to all types, not just primitives. If you call the plain .sort() (importantly not the other variants), then typically all bits of a type contribute to the sorting. It is therefore impossible to differentiate equal elements after the fact, meaning there was no need for a stable sort in the first place. Only in exceedingly rare situations (which I wouldn't even have any example of) would calling the plain .sort() ever make sense over .sort_unstable(). Linting against all (maybe as an optional lint) and forcing users to explicitly #[allow(...)] the lint in those incredibly rare situations makes the most sense to me.

Lint Name

stable_sort_primitive

Category

perf

Advantage

  • It's faster in most cases.

Drawbacks

  • There are rare situations (of which I don't have an example) where it may be wrong to use sort_unstable.
  • sort_unstable is not always faster.

Example

foo.sort();

Could be written as:

foo.sort_unstable();
@CryZe CryZe added the A-lint Area: New lints label Nov 28, 2022
@chansuke
Copy link
Contributor

chansuke commented Dec 4, 2022

@rustbot claim

@ChrisJefferson
Copy link

ChrisJefferson commented Jun 24, 2023

Are you suggesting people clippy should suggest to always replace sort with sort_unstable?

I often want stable sorts.

If you want an example:

I store some extra data in classes which are not used as part of the comparator. However, I want repeatable results, to make testing easier, so I use stable_sort so this extra data is always sorted to the same place.

@Centri3
Copy link
Member

Centri3 commented Jun 24, 2023

Are you suggesting people clippy should suggest to always replace sort with sort_unstable?

"and forcing users to explicitly #[allow(...)] the lint in those incredibly rare situations makes the most sense to me."

Is there any reason you cannot #[allow] it across the entire test module? Like:

#[cfg(test)]
#[allow(clippy::stable_sort_primitive)]
mod tests {}

In the case it's testing a specific method, that happens to use sort_stable, that wouldn't exactly work (but you could still #[allow] it all the same, it'd just require it on the function instead)

(Maybe the lint should ignore modules annotated with #[cfg(test)].)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants