rust-lang / rust-lang/rust-clippy
Detect manual re-implementations of `Ordering::then_with` and similar APIs
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
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
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 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