Conversation
|
r? @llogiq rustbot has assigned @llogiq. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
Lintcheck changes for 26a71c2
This comment will be updated if you push new changes |
| let b: i32 = 7; | ||
|
|
||
| // op=< → min | ||
| let _ = a.min(b); |
There was a problem hiding this comment.
I think this should be b.min(a) since in the equality case the result of if a < b { a } else { b } is b. Of course this doesn't matter for i32s so maybe it makes sense to add tests with types that have fields that are ignored by Ord? Note that in case of ties Ord::min returns the first argument and Ord::max returns the second.
There was a problem hiding this comment.
@ronnodas can you check now, just made another commit.
There was a problem hiding this comment.
I haven't looked at the code at all, just the tests, but I believe the max cases are flipped now, a.max(b) returns b if a.cmp(b) == Ordering::Equal. The PR description above should also be updated (the second example in the "What the lint catches section") for posterity.
Adds the
manual_min_maxlint (complexity, warn-by-default).Detects manual if-else implementations of
Ord::minandOrd::maxandsuggests using those methods directly.
Closes #16884
What the lint catches
All eight variants are covered: both strict (
>,<) and non-strict(
>=,<=) comparisons, and both orderings of the then/else branches.Compound expressions are correctly parenthesized:
Conservative conditions (no false positives)
The lint is skipped when:
Ord::min/Ord::maxare not yet const-stable)eq_expr_valuewhich usesSpanlessEq::deny_side_effectsOrd— this excludesf32/f64, which only implementPartialOrdand whosemin/maxmethods have different NaN semanticsWhy not extend
manual_clamp?manual_clampusesMaybeIncorrectapplicability (clamp can panic) andrequires const-evaluatable bounds. These two-way patterns never panic and
have no such constraint, so
MachineApplicableis appropriate. A separatelint keeps the two concerns independent.
changelog: [
manual_min_max]: new lint to detect manual implementations ofOrd::minandOrd::maxusing if-else expressions.