rust-lang / rust-lang/rust-clippy

Detect manual re-implementations of `Ordering::then_with` and similar APIs

Open
#9,184 0 comments 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

Clippy could detect (more) manual re-implementations of built-in functions such as Ordering::then_with. Existing lints such as manual_ok_or, manual_unwrap_or and manual_map already exist and do the same thing.

Lint Name

manual_ordering_then

Category

style, complexity

Advantage
  • Helps new rust users discover new APIs
  • Reduces code complexity for users that are familiar with Rust's APIs
Drawbacks
  • Adding many of these patterns presumably adds complexity/runtime cost to clippy and requires developer time, so for less commonly used functions this may not be worthwhile
Example
impl Ord for SmolOffset {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        match self.y().cmp(&other.y()) {
            std::cmp::Ordering::Equal => self.x().cmp(&other.x()),
            x => x,
        }
    }
}

Could be written as:

impl Ord for SmolOffset {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.y()
            .cmp(&other.y())
            .then_with(|| self.x().cmp(&other.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 reviewing Clippy's existing manual_ok_or, manual_unwrap_or, and manual_map lints to understand how similar manual API re-implementations are detected. Define the manual_ordering_then pattern from the issue's example and verify that the lint recognizes it and recommends Ordering::then_with without changing unrelated code.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 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.