rust-lang / rust-lang/rust-clippy

Lint for Min/Max If Statements

Open
#1,006 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-style T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Clippy should be able to detect instances where an if statement is being used to calculate the min or max of two or more values and recommend using the cmp::min/max functions instead. These two examples were taken from real-world code directly translated from C into Rust.

Example 1:

Unnecessary
let mut x = if a < b { a } else { b };
if x > y { x = y; }
Recommended
let x = cmp::min(cmp::min(a ,b), y);

Example 2:

Unnecessary
let mut count = (sum + stride / 2) / stride;
if count < 1 { count = 1; }
if sum == 0 { count = 0; }
Recommended
let count = if sum == 0 { 0 } else { cmp::max((sum + stride / 2) / stride, 1) };

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 with the two Rust examples in the issue and review Clippy's existing lint infrastructure for comparable transformations. Done means detecting equivalent min/max patterns and recommending cmp::min or cmp::max while preserving the conditional behavior shown in both examples; no file or test path is specified.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.