feat: add manual_max and manual_min lints - #17206
Conversation
|
r? @llogiq rustbot has assigned @llogiq. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
|
Lintcheck changes for 4992d2c
This comment will be updated if you push new changes |
72b7356 to
a39a158
Compare
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
You'll have to guard this with a MSRV check (1.21). |
a39a158 to
38ea87c
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
38ea87c to
1dd053a
Compare
This comment has been minimized.
This comment has been minimized.
|
@samueltardieu done, merci! |
|
@rustbot ready |
|
Lintcheck - 4 hits on default set (27 crates), 51 hits (22 min + 29 max) on extended set (499 crates), no ICEs. |
1dd053a to
33cdb19
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
33cdb19 to
ee53007
Compare
Add two `complexity` lints that detect a single-sided clamp written as an
`if`/`else` (or a guarding `if`) and suggest `Ord::max` / `Ord::min`:
let _ = if a < b { b } else { a }; // -> a.max(b)
if cores < b { cores = b; } // -> cores = cores.max(b);
This is the sound, generalizable form of a manual clamp that is often
simplified by hand in real code (for example in the `nproc` utility of
uutils/coreutils). Unlike `manual_clamp`, which only fires when both a
lower and an upper bound are applied, these catch the common
single-bound floor/ceiling case.
Restricted to `Ord` types so float `NaN` semantics are not changed, and
emitted as `MaybeIncorrect` since the branching form re-evaluates the
selected operand.
ee53007 to
4992d2c
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
This lint has been nominated for inclusion. |
|
☔ The latest upstream changes (possibly #17552) made this pull request unmergeable. Please resolve the merge conflicts. |
Add two
complexitylints that detect a single-sided clamp written as anif/else(or a guardingif) and suggestOrd::max/Ord::min:This is the sound, generalizable form of the manual clamp simplified by hand in uutils/coreutils#12753 (
nproc). Unlikemanual_clamp, which only fires when both a lower and an upper bound are applied, these catch the common single-bound floor/ceiling case.Restricted to
Ordtypes so floatNaNsemantics are not changed, and emitted asMaybeIncorrectsince the branching form re-evaluates the selected operand.changelog: add
manual_maxandmanual_minlints